N + 1

Hello!
As I am learning the language, I am trying some simple programs to learn the system. I am looking for a simple serial communications script that does nothing more than add numbers. I tried to create one but it failed ( I'm extremely new ).
I understand how to initialize serial comm using Serial.begin(baud rate), and how to output a value via the Serial.println() function.
My program failed to add integers in a loop. Does anyone know where I can find an example of a code like this?
Thanks!
MonorailOrange

To increment the integer N:

N++;

I tried to create one but it failed

I can't see it.
If you post it here, we can help you better.

I got it :slight_smile: n++ was the key. The program worked out this way:

int start = 0;
int N = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println(N++);
  delay(1);
}

I know it's a silly little thing, but learning is learning!

From your thread title, I'm guessing that you just wrote n + 1; or similar.
This does add 1 to n, and tests to see if the result is non-zero, but doesn't assign the incremented value back to n, and throws away the result of the test.

n = n + 1;
n++;  //post-increment
++n;  //pre-increment
n += 1;

all increment n, but be careful how you use the post- and prefix versions.

all increment n, but be careful how you use the post- and prefix versions.

Thanks AWOL! I appreciate it. I was unsure of how to repeat the value... In my head I kept thinking "I've defined N = 0;.... So N + 1 will always be 1 since N = 0...

I appreciate the help :slight_smile: Thank you!

I just noticed that after a certain number of loops, the output integers become negative... How could this happen if the only function in the entire application is addition??
Just curious. :slight_smile:

Look at the range of values that an int can hold. What do you think should happen if the value being held is the upper limit, and you add one to it?

"int" can hold numbers in the range -32768 to + 32767. (0x8000 to 0x7FFF)
If you increment +32677, it wraps around to -32768.
This is an effect of the representation, called 2's complement

Need to use
unsigned int
if you want to count from 0 to 65,535.