0

Increment increases then switches to negative
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Increment increases then switches to negative

by zara2355 on Mon Dec 11, 2017 8:17 pm

Hey all-

I recently noticed something strange (to me) that i figured i would ask about why this is happening. I am using increments as a way of controlling Pins so i dont have to resort to delays. The funny thing i noticed is that the increment seems to have a limit of 32767, but then begins counting in negatives (-32768, -32767, -32766, etc.). From a purely mathematical perspective, i guess this makes sense (its still counting "up"), but because of this, the code seems to think that the condition i have has been met, and therefore switches from LOW to HIGH. (Logically, this makes some sense.... regardless of the negative sign or not, the Arduino is probably looking to see if the increment number i chose is there....if it is, it does what i told it to do.)

Could someone explain to me if the incremental counter has a hard and fast limit? And perhaps more importantly, how do i fix it? Inculded is a screen shot of the serial monitor and my code. Any insight is helpful! Thanks in advance!

Capture.JPG
Capture.JPG (60.5 KiB) Viewed 576 times


Code: Select all | TOGGLE FULL SIZE
const int button = 2;     // the number of the LIGHT BUTTON pin
const int stylusEngaged =  13;      // the number of the STYLUS ENGAGED pin
const int motor =  8;      // the number of the MOTOR TRANSISTOR pin

int increment1 = 0;
int increment2 = 0;

int buttonState = 0;

void setup() {

  Serial.begin(9600);

  pinMode(stylusEngaged, OUTPUT);
  pinMode(motor, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
  buttonState = digitalRead(button);

  if (buttonState == HIGH) {
    digitalWrite(stylusEngaged, HIGH);
    increment1++;
  } else {
    digitalWrite(stylusEngaged, LOW);
    digitalWrite(motor, LOW);
    increment1 = 0;
    increment2 = 0;
  }
        Serial.println(increment1);
//  Serial.println(increment2);

  if (increment1 > 400) {
    digitalWrite(motor, HIGH);
    increment2++;
  }

    if (increment2 > 3500) {
    digitalWrite(motor, LOW);
  }


}

zara2355
 
Posts: 101
Joined: Tue Jun 18, 2013 6:43 pm

Re: Increment increases then switches to negative

by btreichel on Mon Dec 11, 2017 8:22 pm

you would need to use an unsigned int (not sure of that type exists in sketch), or another data type.

btreichel
 
Posts: 79
Joined: Tue Jul 25, 2017 3:00 pm

Re: Increment increases then switches to negative

by adafruit_support_bill on Mon Dec 11, 2017 9:14 pm

The default integer type on the Arduino is a 16 bit signed integer. That has a range of -32768 to +32767. When you exceed the maximum extent of the range, it will wrap around to the minimum.

If you use a 16 bit unsigned integer (uint16_t) the range will be 0 to 65,535.

If you use a 32 bit unsigned integer (uint32_t), the range will be 0 to 4,294,967,295

adafruit_support_bill
 
Posts: 74058
Joined: Sat Feb 07, 2009 10:11 am

Re: Increment increases then switches to negative

by zara2355 on Mon Dec 11, 2017 9:29 pm

The default integer type on the Arduino is a 16 bit signed integer. That has a range of -32768 to +32767. When you exceed the maximum extent of the range, it will wrap around to the minimum.

If you use a 16 bit unsigned integer (uint16_t) the range will be 0 to 65,535.

If you use a 32 bit unsigned integer (uint32_t), the range will be 0 to 4,294,967,295


Thank you btreichel and adafruit_support_bill!

Funny, theres a great deal of elegance in simplicity... I fully admit, coding is not my strong area at all, so this was extremely useful. That said, I added a tiny bit more code to my sketch and it works the way i want.....Ok, so, sure its might not be the most elegant of sketches, but it works. (included below)

Code: Select all | TOGGLE FULL SIZE
const int button = 2;     // the number of the LIGHT BUTTON pin
const int stylusEngaged =  13;      // the number of the STYLUS ENGAGED pin
const int motor =  8;      // the number of the MOTOR TRANSISTOR pin

unsigned int increment1 = 0;
unsigned int increment2 = 0;

int buttonState = 0;

void setup() {

  Serial.begin(9600);

  pinMode(stylusEngaged, OUTPUT);
  pinMode(motor, OUTPUT);
  pinMode(button, INPUT);
}

void loop() {
  buttonState = digitalRead(button);

  if (buttonState == HIGH) {
    digitalWrite(stylusEngaged, HIGH);
    increment1++;
  } else {
    digitalWrite(stylusEngaged, LOW);
    digitalWrite(motor, LOW);
    increment1 = 0;
    increment2 = 0;
  }
        Serial.println(increment1);
//  Serial.println(increment2);

  if (increment1 > 400) {
    digitalWrite(motor, HIGH);
    increment2++;
  }

    if (increment2 > 3500) {
    digitalWrite(motor, LOW);
  }

    if (increment2 >= 32000) {  //this is to prevent counting down
  increment2 = 3501;
  }
}

zara2355
 
Posts: 101
Joined: Tue Jun 18, 2013 6:43 pm

Re: Increment increases then switches to negative

by btreichel on Mon Dec 11, 2017 10:07 pm

keep pushing forward with that attitude and you'll get it.

btreichel
 
Posts: 79
Joined: Tue Jul 25, 2017 3:00 pm

Re: Increment increases then switches to negative

by zara2355 on Wed Dec 20, 2017 1:00 pm

Hey there btreichel and adafruit_support_bill, I just wanted to give you a followup on the project. its done, and I want to say again thank you for all the help. In case you would like to see it, here's the video:

https://www.youtube.com/watch?v=seLb1bBQDF0

Again, thank you very much! Your input helped immensely!!!

zara2355
 
Posts: 101
Joined: Tue Jun 18, 2013 6:43 pm

Re: Increment increases then switches to negative

by adafruit_support_bill on Wed Dec 20, 2017 1:24 pm

Nice project! Thanks for the follow-up & video.

adafruit_support_bill
 
Posts: 74058
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.