Code seemed to use all my data overnight


#1

Hi
i wrote some code which simply displays the state of an input on ubidots. I put a cause that the state must have changed before the data is sent however when i woke up in the morning all my data was gone even though no inputs had changed (they are manual switches on my test rig). can anyone see where i have gone wrong?

I currently don’t use the analog value as yet

Thanks…Ben

// This #include statement was automatically added by the Particle IDE.
#include <Ubidots.h>

// This #include statement was automatically added by the Particle IDE.
#include <SegmentDriver.h>

// This #include statement was automatically added by the Particle IDE.
#include <NCD2Relay.h>


#define TOKEN "????????????????????????????"  // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);



SegmentLEDDriver display; // create an instance of the LED Display


NCD2Relay relayController; // create an instance of the board
int input1 = 0;
int lastinput1 = 0;
int input2 = 0;
int lastinput2 = 0;
int input3 = 0;
int lastinput3 = 0;
int input4 = 0;
int lastinput4 = 0;
float analog1 = 0.0;
int testvar = 0; // used to set the LED display to 0




void setup()
{ 
   Serial.begin(115200);
    relayController.setAddress(0,0,0);// set the address of the relay board 
    relayController.turnOffRelay(1); // turn off relay 1 on the board if it is on
     relayController.turnOffRelay(2); // turn off relay 2 on the board if it is on
    display.init();
}

void loop() {
    
    //---------------------------------Read all the inputs----------------------
    input1 = relayController.readInputStatus(1);
    input2 = relayController.readInputStatus(2);
    input3 = relayController.readInputStatus(3);
    input4 = relayController.readInputStatus(4);
    analog1 = analogRead(A0);
    analog1 = map(analog1, 0, 4095, 4, 20);
       
   
display.displayWriteInt(testvar);// display on lcd testvar variable


//---------------------------------------Check if anything is diffrent then update web
if (input1 != lastinput1)
{
    lastinput1 = input1;  
    ubidots.add("input1", input1);
    ubidots.sendAll();
    testvar ++;
    delay(1000);
}

if (input2 != lastinput2)
{
    lastinput2 = input2;  
    ubidots.add("input2", input2);
    ubidots.sendAll();
    testvar ++;
    delay(1000);
} 

if (input3 != lastinput3)
{
    lastinput3 = input3;  
    ubidots.add("input3", input3);
    ubidots.sendAll();
    testvar ++;
    delay(1000);
} 

if (input4 != lastinput4)
{
    lastinput4 = input4;  
    ubidots.add("input4", input4);
    ubidots.sendAll();
    testvar ++;
    delay(1000);
} 
}
 

#2


#3

Talk your way back through this logic and you’ll find your issue.


#4

I still can’t see it can you give me a clue?


#5

@benjiiiiuk, do you have a pull-up or pull-down resistor for each of your buttons? The GPIO inputs on the ControlEverything board don’t have these, allowing the pins to float and causing random data to appear, causing “ghost” changes.


#6

Clearly wrote that too early in the morning and didn’t read your post properly, sorry!
As @peekay123 suggests, floating inputs are a primary suspect.

Unrelated - I would look at removing the delay(1000) logic, as it stands your program is comparing each switch in turn with 1 second between them, by the time it gets to the 4th switch its looking at a switch status from 3 seconds ago. In addition that delay is affecting how quickly your loop runs which could cause problems as complexity increases. Look at using millis() timers instead for periodic events.

Why not compare all the inputs to lastinput in one go and then use one sendAll() at the end, if they have all changed you then only post once to Ubidots.


#7

I’d also consider using arrays and loops instead of copy/pasting code blocks with numbered variables.


#8

No I don’t have pull down resistors so it must be that. I assumed I was still in direct contact with the input pins on the electron.
As for using loops and arrays I will get to that point as I get a little better at programming I need to be able to see what’s going on Thanks for all the replies.


#9

A simple if crude way of seeing whats going on is to write little status messages for yourself to come out of the serial port, this can be very helpful if you aren’t quite sure just where a piece of code is coming unstuck.