Variable Storing Large Number

Hi all!

I've been using a Circuit Playground in conjunction with the Arduino IDE and there's an issue I've been running into I can't seem to figure out.

I'm trying to use the onboard accelerometer of the CPB to activate different lights. Specifically I want it to store the time when the accelerometer recorded a reading less than -12 across the X-axis, add 1000 to it (or some other predetermined time), and compare that number with the current time. If the difference is greater than -1 I want it to set the neopixels white. Later I'm going to incorporate a loop function that will continually diminish the brightness and then return to a different function.

The issue I'm running into is that it records the time I flicked the device, but almost immediately returns this massive number that must be an error code or place holder for something and in the process continually sets the neopixels white. A picture is below.

Any help would be greatly appreciated! Please let me know if anything is confusing about my post!

#include <Adafruit_CircuitPlayground.h>

void setup() {

  Serial.begin(115200);
  Serial.println("Circuit Playground Connected!");
  CircuitPlayground.begin();
}

void loop() {

  float X = CircuitPlayground.motionX();  //store X-axis accelerometer data as float X
  unsigned long waitTime = 1000;          //used as easily changeable value; added to stored time
  unsigned long myTime;                   // stores controller's time
  unsigned long deviceTime = millis();    // time since program started
  CircuitPlayground.setBrightness(50);    // neopixel brightness level

  if (X < -12) {                     // if X-axis accelerometer is less than -12
    myTime = deviceTime + waitTime;  // store current controller time and add waitTime
  }

  Serial.println(myTime);  //print value stored in myTime every 50ms; just for checking purposes
  delay(50);

  if (myTime - deviceTime >= 0) {  // if the stored time is greater than the device's time set device neopixels to white until timer runs out
    CircuitPlayground.setPixelColor(0, 255, 255, 255);
    CircuitPlayground.setPixelColor(1, 255, 255, 255);
    CircuitPlayground.setPixelColor(2, 255, 255, 255);
    CircuitPlayground.setPixelColor(3, 255, 255, 255);
    CircuitPlayground.setPixelColor(4, 255, 255, 255);
    CircuitPlayground.setPixelColor(5, 255, 255, 255);
    CircuitPlayground.setPixelColor(6, 255, 255, 255);
    CircuitPlayground.setPixelColor(7, 255, 255, 255);
    CircuitPlayground.setPixelColor(8, 255, 255, 255);
    CircuitPlayground.setPixelColor(9, 255, 255, 255);
  } else {  //if stored time is less than device time set neopixels to blue
    CircuitPlayground.setPixelColor(0, 0, 255, 255);
    CircuitPlayground.setPixelColor(1, 0, 255, 255);
    CircuitPlayground.setPixelColor(2, 0, 255, 255);
    CircuitPlayground.setPixelColor(3, 0, 255, 255);
    CircuitPlayground.setPixelColor(4, 0, 255, 255);
    CircuitPlayground.setPixelColor(5, 0, 255, 255);
    CircuitPlayground.setPixelColor(6, 0, 255, 255);
    CircuitPlayground.setPixelColor(7, 0, 255, 255);
    CircuitPlayground.setPixelColor(8, 0, 255, 255);
    CircuitPlayground.setPixelColor(9, 0, 255, 255);
  }
}

You defined unsigned int so you can not use negative numbers (-1 or -12). The massive numbers are "negative numbers" expressed in positive form. Sorry, I totally skipped over the float x

1 Like

Because unsigned longs wrap around, this is always true:

If you want to treat the wrapped-around unsigned longs as negative numbers try:

if ( signed long (myTime - deviceTime) >= 0)

Thanks! I'll try that when I get home for work! The odd thing is that when I print out the current state of myTime it always reverts back to that large number even though it should never go into the negative since it's always counting up. myTime - deviceTime never stores as a variable so why would myTime independently alter as it does in the photo? Thanks for your help and I apologize if I misunderstand something!

That number is 0xA5A5A5A5 (in hex). Funny number...

Can you confirm that the screen print you posted correspond to the program in Post #1?

When X is greater than or equal to -12, the "myTime" local variable is never initialized and will contain whatever garbage was left in the stack. You can fix that by initializing the variable:
unsigned long myTime = 0; // stores controller's time

That does get rid of the large number, but now the moment the X-axis reading rises above -12 (X >= -12) it starts returning zeroes. I want the variable myTime to store the last millis reading when X < -12, add 1000, and compare that against the current millis reading. If I initialize myTime as 0 it instantly returns to 0, but if I don't it returns that odd reading

It most certainly is!

You should make 'myTime' a 'static' variable:
static unsigned long myTime = 0; // stores controller's time

2 Likes

Thank you so much! This completely fixed the problem! I didn't realize that variable were recreated with each call of the function and in cases like this needed to be declared static to retain previous information. Thank you so much!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.