Helo, I'm trying to change 3 variables on a constructor named
Flasher led1(6,tiempoPrendida,tiempoApagada,intensidad);
I managed to change intensidad with a global variable, but when I try the same with both tiempoPrendida and tiempoApagada ,I can't. The only difference I can see is that intensidad is of type int and the other two are of type float.
the three variables are parsed from a HTTPREAD method, the recognition is working fine because I printed the value of the variables and they are exactly what they are supposed to be. THe problem is that when using the constructor, I successfully made the led attached to pin 6 of the Arduino change its intensity, but I cant change the blinking time.
If i manually enter a number, in the constructor or at the variable definition it works great, but really don't know why the other two aren't changing.
I'm thinking there's something about classes,or constructors in C that I may be not considering.
If you know something that I can do to make this thing work It would be great, I've been tryin to solve this issue with no success for hours really.
Here's the code and a short part of the log.
Thanks.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
#define rxPin 10
#define txPin 11
SoftwareSerial sim800L(rxPin,txPin);
//GPS Module RX pin to Arduino 9
//GPS Module TX pin to Arduino 8
AltSoftSerial neogps;
TinyGPSPlus gps;
int readPin=A2; // lectura de voltaje de la bateria
int BLUELed = 6; //pin our blue LED is connected to
int ledState = LOW; //used to control blue LED state, we're starting with it OFF
unsigned long currentMillis = 0; //stores the current time
unsigned long previousMillis = 0; //stores last time blue LED was updated
int intensidad=50; //cantidad de candelas
long tiempoPrendida=10000; //cuanto tiempo esta encendida la baliza
long tiempoApagada=10000; // cuanto tiempo esta apagada la baliza
//--------------------------ISR-------------------------------------------------------
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
// Constructor - creates a Flasher
// and initializes the member variables and state
public:
Flasher(int pin, long on, long off,int intensidad)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
ledState = LOW;
previousMillis = 0;
}
void Update(unsigned long currentMillis)
{
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
analogWrite(ledPin, 0); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
analogWrite(ledPin, intensidad); // Update the actual LED
}
}
};
Flasher led1(6,tiempoPrendida,tiempoApagada,intensidad); //(6,123,400)
//--------------------------FIN ISR-------------------------------------------------------
void setup(){
pinMode(readPin,INPUT);
pinMode(BLUELed, OUTPUT);
//-----------------ISR------------------------------------------------------------------
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function below
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
//--------FIN ISR ----------------------------------------------------------------------
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and SIM800L
sim800L.begin(9600);
//Begin serial communication with Arduino and SIM800L
neogps.begin(9600);
Serial.println("Initializing...");
}
void loop(){
while(sim800L.available()){
Serial.write(sim800L.read());
}
while(Serial.available()) {
sim800L.write(Serial.read());
}
unsigned long currentMillis = millis();
controlLedsyGPS();
Serial.println("el valor de la variable tiempoPrendida es");
Serial.println(tiempoPrendida);
Serial.println("el valor de la variable tiempoApagada es");
Serial.println(tiempoApagada);
Serial.println("el valor de la variable intensidad es");
Serial.println(intensidad);
delay(1000);
}
void controlLedsyGPS(){
//Can take up to 60 seconds
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 10000;){
while (neogps.available()){
if (gps.encode(neogps.read())){
newData = true;
break;
}
}
}
//If newData is true
if(true){
newData = false;
//Control de carga--------------------------------------------------------------------------------------------
int v1=0; //Lectura de voltaje en el pin A2
float voltaje1_temp=0;
v1=analogRead(readPin);
voltaje1_temp=(5./1023.)*v1 ; //el punto es porque preciso que el valor sean un float con decimales
char buffer[6]; //convertimos el float a string para ser enviado por el metodo GET
dtostrf(voltaje1_temp,4,2,buffer); //paso intermedio conviertiendo a un array de char[]
String voltaje1=String(buffer); //lo convierto a string
//Control de carga----------------------------------------------------------------------------------------------
String latitude, longitude ;
latitude = String(gps.location.lat(), 6); // Latitude in degrees (double)
longitude = String(gps.location.lng(), 6); // Longitude in degrees (double)
Serial.print("Latitude= ");
Serial.print(latitude);
Serial.print(" Longitude= ");
Serial.print(longitude);
Serial.print(" Voltaje1= ");
Serial.println(voltaje1);
//if (latitude == 0) {return 0;}
String url,tmp;
url = "http://balizas2.000webhostapp.com/gpsdata.php?lat=";
url += latitude;
url += "&lng=";
url += longitude;
url += "&voltaje1=";
url+= voltaje1;
url += "&estado=1";
tmp = "http://balizas2.000webhostapp.com/getstate.php?color=All";
Serial.println(tmp);
delay(300);
Serial.println(url);
delay(300);
//-----------------Lectura de valores de la interfaz de usuario----------------------------------------------
sim800L.println("AT+CSQ"); // Signal quality check
delay(100);
sim800L.println("AT+CBC"); // Signal quality check
delay(100);
ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.
sim800L.println("AT+CGATT?"); //Attach or Detach from GPRS Support
delay(100);
ShowSerialData();
sim800L.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");//setting the SAPBR, the connection type is using gprs
delay(1000);
ShowSerialData();
sim800L.println("AT+SAPBR=3,1,\"APN\",\"antel.lte\"");//setting the APN, Access point name string
delay(2000);
ShowSerialData();
sim800L.println("AT+SAPBR=0,1");//setting the SAPBR
delay(1000);
ShowSerialData();
sim800L.println("AT+SAPBR=1,1");//setting the SAPBR
delay(1000);
ShowSerialData();
sim800L.println("AT+HTTPINIT"); //init the HTTP request
delay(1000);
ShowSerialData();
sim800L.println("AT+HTTPPARA=\"URL\",\"balizas2.000webhostapp.com/getstate.php?color=All\"");// setting the httppara, the second parameter is the website you want to access
delay(1000);
ShowSerialData();
sim800L.println("AT+HTTPACTION=0");//submit the request
delay(2000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
//while(!sim800L.available());
ShowSerialData();
sim800L.println("AT+HTTPREAD");// read the data from the website you access
delay(300);
dim();
sim800L.println("");
delay(100);
sim800L.println("AT+HTTPTERM");//setting the SAPBR
delay(1000);
ShowSerialData();
//--------------------------- GPS - Con multitarea simulada para no perder oraciones ---------------------
sendATcommand("AT+CGATT?", "OK", 2000);
//Connection type: GPRS - bearer profile 1
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
//sets the APN settings for your network provider.
sendATcommand("AT+SAPBR=3,1,\"APN\",\"antel.lte\"", "OK", 2000);
sendATcommand("AT+SAPBR=0,1", "OK", 2000);
//enable the GPRS - enable bearer 1
sendATcommand("AT+SAPBR=1,1", "OK", 2000);
//sendATcommand("AT+HTTPTERM", "OK", 1000);
sendATcommand("AT+HTTPINIT", "OK", 2000);
sim800L.print("AT+HTTPPARA=\"URL\",\"");
sim800L.print(url);
sendATcommand("\"", "OK", 1000);
//Set up the HTTP action
sendATcommand("AT+HTTPACTION=0", "0,200", 1000);
sendATcommand("AT+HTTPTERM", "OK", 1000);
}
return 1;
}
// -------------------funcion para enviar comandos AT simulando multitarea (con millis())----------------
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
//Initialice the string
memset(response, '\0', 100);
delay(100);
//Clean the input buffer
while( sim800L.available() > 0) sim800L.read();
if (ATcommand[0] != '\0'){
//Send the AT command
sim800L.println(ATcommand);
}
x = 0;
previous = millis();
//this loop waits for the answer with time out
do{
//if there are data in the UART input buffer, reads it and checks for the asnwer
if(sim800L.available() != 0){
response[x] = sim800L.read();
//Serial.print(response[x]);
x++;
// check if the desired answer (OK) is in the response of the module
if(strstr(response, expected_answer) != NULL){
answer = 1;
}
}
}while((answer == 0) && ((millis() - previous) < timeout));
Serial.println(response);
return answer;
}
// -------------------------Mostrar datos por el puerto serie-----------------
void ShowSerialData(){
while(sim800L.available()!=0)
Serial.write(char (sim800L.read()));
}
// -----------------DIM--------------------------------------------------------------
void dim(){
String content = "";
// String RedState = content.substring();
while(sim800L.available()!=0)
{
//Serial.write(sim800L.read());
content = content + String(char (sim800L.read())); //almaceno el contenido de los caracteres del array
}
Serial.println(content); //verificacion de que se recibio la trama
Serial.println(content[1]);
for (int i = 0; i < 32; i++){
Serial.println(content[i]);
}
//----------Decodificacion de intensidad-----------------------------------------
if(content.substring(28,29)== "5"){
intensidad=255;
}
else if (content.substring(28,29)== "4"){
intensidad=200;
}
else if (content.substring(28,29)== "3"){
intensidad=150;
}
else if (content.substring(28,29)== "2"){
intensidad=100;
}
else if (content.substring(28,29)== "1"){
intensidad=50;
}
else if (content.substring(28,29)== "0"){
intensidad=0;
}
//--------------Decodificacion de tiempoPrendida---------------------------------------
if(content.substring(29,30)== "5"){
tiempoPrendida=2500;
}
else if (content.substring(29,30)== "4"){
tiempoPrendida=2000;
}
else if (content.substring(29,30)== "3"){
tiempoPrendida=1500;
}
else if (content.substring(29,30)== "2"){
tiempoPrendida=1000;
}
else if (content.substring(29,30)== "1"){
tiempoPrendida=500;
}
else if (content.substring(29,30)== "0"){
tiempoPrendida=0;
}
//----------------------Decodificacion de tiempoApagada---------------------------------
if(content.substring(30,31)== "5"){
tiempoApagada=2500;
}
else if (content.substring(30,31)== "4"){
tiempoApagada=2000;
}
else if (content.substring(30,31)== "3"){
tiempoApagada=1500;
}
else if (content.substring(30,31)== "2"){
tiempoApagada=1000;
}
else if (content.substring(30,31)== "1"){
tiempoApagada=500;
}
else if (content.substring(30,31)== "0"){
tiempoApagada=0;
}
content = "";
}
//-------------------ISR-------------------------------------------------------
// Interrupt is called once a millisecond, to update the LEDs
SIGNAL(TIMER0_COMPA_vect) {
unsigned long currentMillis = millis();
led1.Update(currentMillis);
}
//------------------FIN ISR-----------------------------------------------------
The log..., notice how the variables change correctly apparently but nothing happens...
05:04:30.091 -> Latitude= 0.000000 Longitude= 0.000000 Voltaje1= 4.79
05:04:30.185 -> http://balizas2.000webhostapp.com/getstate.php?color=All
05:04:30.466 -> http://balizas2.000webhostapp.com/gpsdata.php?lat=0.000000&lng=0.000000&voltaje1=4.79&estado=1
05:04:31.016 -> AT+CBC
05:04:31.063 -> +CBC: 0,100,4219
05:04:31.063 ->
05:04:31.063 -> OK
05:04:31.110 -> AT+CGATT?
05:04:31.110 -> +CGATT: 1
05:04:31.157 ->
05:04:31.157 -> OK
05:04:32.141 -> AT+SAPBR=3,1,"CONTYPE","GPRS"
05:04:32.188 -> OK
05:04:34.186 -> AT+SAPBR=3,1,"APN","antel.lte"
05:04:34.233 -> OK
05:04:35.217 -> AT+SAPBR=0,1
05:04:35.217 -> OK
05:04:36.248 -> AT+SAPBR=1,1
05:04:36.248 -> OK
05:04:37.233 -> AT+HTTPINIT
05:04:37.280 -> OK
05:04:38.311 -> AT+HTTPPARA="URL","balizas2.000webhostapp.com/getstate.php?coloAT+HTTPACTION=0
05:04:40.373 -> OK
05:04:40.654 -> AT+HTTPREAD
05:04:40.701 -> OK
05:04:40.701 ->
05:04:40.701 -> T
05:04:40.701 -> A
05:04:40.701 -> T
05:04:40.701 -> +
05:04:40.701 -> H
05:04:40.701 -> T
05:04:40.701 -> T
05:04:40.701 -> P
05:04:40.701 -> R
05:04:40.701 -> E
05:04:40.701 -> A
05:04:40.701 -> D
05:04:40.701 ->
05:04:40.748 ->
05:04:40.748 ->
05:04:40.748 ->
05:04:40.748 -> O
05:04:40.748 -> K
05:04:40.748 ->
05:04:40.748 ->
05:04:40.748 ->
05:04:40.748 ->
+HTTPACTION: 0,200,135
05:04:47.591 ->
05:04:47.591 -> el valor de la variable tiempoPrendida es
05:04:47.638 -> 10000
05:04:47.638 -> el valor de la variable tiempoApagada es
05:04:47.685 -> 10000
05:04:47.685 -> el valor de la variable intensidad es
05:04:47.732 -> 50
05:04:58.699 -> Latitude= 0.000000 Longitude= 0.000000 Voltaje1= 4.78
05:04:58.746 -> http://balizas2.000webhostapp.com/getstate.php?color=All
05:04:59.074 -> http://balizas2.000webhostapp.com/gpsdata.php?lat=0.000000&lng=0.000000&voltaje1=4.78&estado=1
05:04:59.590 -> AT+CSQ
05:04:59.590 -> +CSQ: 18,0
05:04:59.637 ->
05:04:59.637 -> OK
05:04:59.637 -> AT+CBC
05:04:59.637 -> +CBC: 0,100,4224
05:04:59.683 ->
05:04:59.683 -> OK
05:04:59.730 -> AT+CGATT?
05:04:59.730 -> +CGATT: 1
05:04:59.730 ->
05:04:59.730 -> OK
05:05:00.761 -> AT+SAPBR=3,1,"CONTYPE","GPRS"
05:05:00.808 -> OK
05:05:02.777 -> AT+SAPBR=3,1,"APN","antel.lte"
05:05:02.824 -> OK
05:05:03.800 -> AT+SAPBR=0,1
05:05:03.847 -> OK
05:05:04.834 -> AT+SAPBR=1,1
05:05:04.834 -> OK
05:05:05.866 -> AT+HTTPINIT
05:05:05.866 -> +CME ERROR: 3
05:05:06.897 -> AT+HTTPPARA="URL","balizas2.000webhostapp.com/getstate.php?coloAT+HTTPACTION=0
05:05:08.948 -> OK
05:05:08.948 ->
05:05:08.948 -> +HTTPACTION: 0,200,4
05:05:09.276 -> AT+HTTPREAD
05:05:09.276 -> +HTTPREAD: 4
05:05:09.276 -> 555
05:05:09.276 -> OK
05:05:09.276 ->
05:05:09.323 -> T
05:05:09.323 -> A
05:05:09.323 -> T
05:05:09.323 -> +
05:05:09.323 -> H
05:05:09.323 -> T
05:05:09.323 -> T
05:05:09.323 -> P
05:05:09.323 -> R
05:05:09.323 -> E
05:05:09.323 -> A
05:05:09.323 -> D
05:05:09.323 ->
05:05:09.323 ->
05:05:09.323 ->
05:05:09.323 ->
05:05:09.323 -> +
05:05:09.370 -> H
05:05:09.370 -> T
05:05:09.370 -> T
05:05:09.370 -> P
05:05:09.370 -> R
05:05:09.370 -> E
05:05:09.370 -> A
05:05:09.370 -> D
05:05:09.370 -> :
05:05:09.370 ->
05:05:09.370 -> 4
05:05:09.370 ->
05:05:09.370 ->
05:05:09.370 ->
05:05:09.370 -> 5
05:05:09.370 -> 5
05:05:09.417 -> 5
05:05:09.417 ->
05:05:10.448 ->
AT+HTTPTERM
05:05:10.495 -> OK
05:05:10.589 -> AT+CGATT?
05:05:10.636 -> +CGATT: 1
05:05:10.636 ->
05:05:10.636 -> OK
05:05:10.776 -> AT+SAPBR=3,1,"Contype","GPRS"
05:05:10.823 -> OK
05:05:10.964 -> AT+SAPBR=3,1,"APN","antel.lte"
05:05:11.011 -> OK
05:05:11.387 -> AT+SAPBR=0,1
05:05:11.387 -> OK
05:05:11.715 -> AT+SAPBR=1,1
05:05:11.762 -> OK
05:05:11.855 -> AT+HTTPINIT
05:05:11.855 -> OK
05:05:12.091 -> =.p&estado=1"
05:05:12.137 -> OK
05:05:13.170 -> AT+HTTPACTION=0
05:05:13.216 -> OK
05:05:13.216 ->
05:05:13.216 -> +HTTPACTION: 0,200
05:05:13.310 -> AT+HTTPTERM
05:05:13.357 -> OK
05:05:13.357 -> el valor de la variable tiempoPrendida es
05:05:13.404 -> 2500
05:05:13.404 -> el valor de la variable tiempoApagada es
05:05:13.451 -> 2500
05:05:13.451 -> el valor de la variable intensidad es
05:05:13.498 -> 255
05:05:14.435 ->
05:05:24.425 -> Latitude= 0.000000 Longitude= 0.000000 Voltaje1= 4.61
05:05:24.472 -> http://balizas2.000webhostapp.com/getstate.php?color=All
05:05:24.800 -> http://balizas2.000webhostapp.com/gpsdata.php?lat=0.000000&lng=0.000000&voltaje1=4.61&estado=1
05:05:25.363 -> AT+CSQ
05:05:25.363 -> +CSQ: 18,0
05:05:25.363 ->
05:05:25.363 -> OK
05:05:25.363 -> AT+CBC
05:05:25.363 -> +CBC: 0,90,4125
05:05:25.409 ->
05:05:25.409 -> OK
05:05:25.456 -> AT+CGATT?
05:05:25.456 -> +CGATT: 1
05:05:25.456 ->
05:05:25.503 -> OK
05:05:26.501 -> AT+SAPBR=3,1,"CONTYPE","GPRS"
05:05:26.501 -> OK