TODOPIC
Otros Microcontroladores / Dispositivos programables => Microcontroladores 8 bits => Mensaje iniciado por: micro_pepe en 17 de Mayo de 2012, 07:06:50
-
Estoy intentando compilar con Arduino el siguiente código, y me lanza el error: 'Serial' was not declared in this scope
El código lo descargué de aquí (http://arduinoelettronica.wordpress.com/2012/04/20/caricabatterie-intelligente-nimh-con-attiny85-e-arduino-smart-nimh-battery-charger/) es del cargador de baterias, segun eso el puerto serie funciona, pero no consigo compilarlo. ¿Alguna idea?
Saludos.
Edito: ya me compila, hera por las lineas: Serial.xxxx que tenia que ponerlas mySerial.xxxx, además de unos include. Ahora me falta probarlo en real.
/*
NiMh Charger 0.9
with AtTiny85 @ 1Mhz
by Luca Soltoggio
10/03/2012 - 20/04/2012
Use negative deltaV to determine end of charge.
Suitable for NiMh and NiCD battery pack.
Default is for 6 cells 2500mAh.
Need some hardware/software adjustment for changing current / cells number
See http://arduinoelettronica.wordpress.com/
*/
//#include "Arduino.h"
#include <stdlib.h>
#include <math.h>
#include "wiring.h"
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
#include "WProgram.h"
const int inputPin = B1;
const int outputPin = 0;
const int numReadings = 30; // number of analog read before checking battery status
const int multi = 1614; // multi coefficent for obtaining millivolts from analogread
long interval = 1000; // interval for pulse charging and analog read - don't change this
long interval2 = 250; // pulse off interval - you can adjust power with this. Use 100 for 2-4Ah battery packs, 500 for 1-2Ah battery pack
long interval2b=interval2;
long previousMillis,currentMillis,currentMillis2,trickleMillis = 0;
unsigned int readingtemp;
unsigned int total = 0;
unsigned int average,medium,maxmedium = 0;
boolean executed,endcharge,trickle=false; // booleans for controlling various activities (for example "end of charge")
unsigned int myarray [7]; // array for keeping last 7 readings
int index,i = 0;
void setup()
{
mySerial.begin(9600);
pinMode(0,OUTPUT);
// Some readings for initial check
for (i=0;i<10;i++) {
readingtemp = analogRead(inputPin);
total=total+readingtemp;
}
average = (((float)total / 1023.0) * (float)multi) / 10.0 + 0.5;
if (average<=70) endcharge=true; // If there is no battery, end charge
mySerial.println((int)average);
total=0;
average=0;
}
void pusharray() {
// push the array
for (i=0;i<=5;++i) {
myarray[i]=myarray[i+1];
}
myarray[6]=average;
}
void voltread() {
readingtemp = analogRead(inputPin); // read analog input
total= total + readingtemp;
index++;
// if numReadings reached, calculate the average
if (index==numReadings) {
index=0;
average = (((float)total / 1023.0) * (float)multi) / numReadings + 0.5;
if (average<=70) endcharge=true; // stop charge if battery is detached
total=0;
pusharray(); // insert new average in array
medium=(float)(myarray[6]+myarray[5]+myarray[4]+myarray[3]+myarray[2]+myarray[1]+myarray[0])/7.0+0.5; // calculate the average of the last 7 readings
if (medium>maxmedium) maxmedium=medium; // save the value of highest medium in "maxmedium"
mySerial.print(medium);
mySerial.print(",");
mySerial.print(maxmedium);
mySerial.print(",");
mySerial.println((byte)myarray[6]);
if ( ((medium+1) < maxmedium) && ((millis()/60000)>=11) ) { // if battery charged (average voltage is dropped 0.02v), but not in the firsts 11 mintues
if (!trickle) trickleMillis=millis(); // start trickle timer
trickle=true; // enter final trickle charging mode
if ((millis()/60000)<=15) endcharge=true; // if battery is charged in the firts 15 minutes, don't apply trickle charge (maybe was yet charged)
}
}
}
void loop() {
currentMillis = millis();
// executed every "interval" millis
if(currentMillis - previousMillis > interval) {
voltread(); // call reading and check volts function
digitalWrite(outputPin,LOW); // temporaly stop charging
previousMillis = currentMillis;
executed=false; // boolean for setting and checking if has been yet turned ON charge
// in the firsts 10 minutes and in the endings 15 minutes do a trickle charge (change OFF interval)
if ( ( (trickle) && (((millis()-trickleMillis)/60000)<15) ) || ((millis()/60000)<10) ) {
interval2=(interval-(interval-interval2b)/5);
} else if ((millis()/60000)>=10) interval2=interval2b; // after initial trickle charghe set back right time
if ( (trickle) && (((millis()-trickleMillis)/60000)>=15) ) endcharge=true; // if final trickle charge end, end charge
}
currentMillis2 = millis();
// executed "interval2" millis after turning OFF charge
if ((currentMillis2 - previousMillis > interval2) && (!executed)) {
executed=true;
if (!endcharge) {
digitalWrite(outputPin,HIGH); // if battery is not charged, re-enable charging
}
}
}