Buenas noches inicialmente cree este post:
http://forum.arduino.cc/index.php?PHPSESSID=78amoaull8af9bduid21go01v2&topic=272621.0
Donde fueron aclaradas tres de mis dudas.
Agradecido con Jose y Surbyte.
Les comento rapidamente de que trata el proyecto.
Una regleta inteligente autónoma para eliminar el consumo en standby. De 5 tomas 110V AC.
El codigo principal para UN SOLO SENSOR ES ESTE.
#define SENSOR_0 A0 // Analog input pin that sensor is attached to
unsigned long current; //amplitude current
int led1 = 8 ;
int valor = 0 ;
int flag = 0 ;
unsigned long previousMillis = 0;
void setup()
{
Serial.begin(9600);
digitalWrite(led1,HIGH);
pins_init();
}
void loop()
{
int sensor_max;
sensor_max = getMaxValue();
current=(float)sensor_max/1024*5/800*2000000;
//Serial.println("sensor_max = ");
//Serial.println(sensor_max);
Serial.println("The amplitude of the current is(in mA)");
Serial.println(current);
if ((current < valor*0.4))
{
if (flag == 1) // Si la bandera esta en 1 apaga el LED
{
digitalWrite(led1, LOW);
flag = 0;
valor = 0;
}
else
{
uint32_t start_time = millis();
Serial.println("Comenzo Millis");
if ((millis()- previousMillis) >= 5000)
{
flag = 1;
Serial.println("Termino Millis");
}
}
}
else
{
valor = current;
flag = 0 ;
}
}
void pins_init(){
pinMode(SENSOR_0, INPUT);
pinMode(led1, OUTPUT);
}
/*Function: Sample for 1000ms and get the maximum value from the SIG pin*/
int getMaxValue()
{
int sensorValue; //value read from the sensor
int sensorMax = 0;
uint32_t previousMillis = millis();
while((millis()- previousMillis) < 2000)//sample for 1000ms
{
sensorValue = analogRead(SENSOR_0);
if (sensorValue > sensorMax)
{
/*record the maximum sensor value*/
sensorMax = sensorValue;
}
}
return sensorMax;
}
El problema esta en esta comparacion:
if ((current < valor*0.4))
{
if (flag == 1) // Si la bandera esta en 1 apaga el LED
{
digitalWrite(led1, LOW);
flag = 0;
valor = 0;
}
else
{
uint32_t start_time = millis();
Serial.println("Comenzo Millis");
if ((millis()- previousMillis) >= 5000)
{
flag = 1;
Serial.println("Termino Millis");
}
}
El millis en esa etapa se queda pegado, contando y contando y nunca llega a poner 1 en la variable flag.
O aveces si lo hace, pero cuenta hasta 20 seg, 40 seg, aveces solo 0 seg.
Segun mi analisis lo que puede estar dandome problemas es que para obtener los valores del sensor ya estoy utilizando un millis. Como pueden ver aqui.
int getMaxValue()
{
int sensorValue; //value read from the sensor
int sensorMax = 0;
uint32_t previousMillis = millis();
while((millis()- previousMillis) < 2000)//sample for 1000ms
{
sensorValue = analogRead(SENSOR_0);
if (sensorValue > sensorMax)
{
/*record the maximum sensor value*/
sensorMax = sensorValue;
}
}
return sensorMax;
}
Entonces al estar funcionando dos millis al mismo tiempo pueden estar presentando ese problema.
Como puedo solventar esto ? .
Es muy importante para mi poder resolver este problema y tenerlo claro, porque luego debo aplicar esa programacion para 5 sensores. y que conjuntamente funcionen de manera correcta.