Counting and loop question

int led = 9;
int led2 = 10;
int count = 0;
int count2 = 0;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
 for(count = 0; count < 255; count++) { 
  analogWrite(led, count);
  delay(5);
 }
 
 for(count >= 255; count--;) { 
  analogWrite(led, count);
  delay(5);
 }
}

What is the best way to make count2 do the opposite of count?
When count goes from 0 to 255, i want count2 to go from 255 to 0.

Also, why do i need a semicolon on the count-- in last loop, but not on the count++ in the first loop?

you need the three operators in both, but think of doing it like this to make your life happier:

int led = 9;
int led2 = 10;
int count = 0;
int count2 = 0;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
 for(count = 0; count < 255; count++) 
 { 
  analogWrite(led, count);
  delay(5);
 }
 for(count = 0; count < 255; count++) 
 { 
  analogWrite(led, 255 - count);
  delay(5);
 }
}
int led = 9;
int led2 = 10;
int count = 0;
int count2 = 0;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  Serial.begin(9600);
}

void loop() {
 for(count = 0; count < 255; count++) { 
  analogWrite(led, count);
  count2 = ((~count) & 0xFF); //Inverts Count and filters out the last byte. Runs from 255 to 0.
  delay(5);
 }
 
 for(count = 255; count >= 1; count--;) { 
  analogWrite(led, count);
  count2 = ((~count) & 0xFF); //inverts count and filters out the last byte. Runs from 0 to 255.
  delay(5);
 }
}

Untested code! not in the possibility of testing it but 99% sure it'll work (Or give you ideas of how to do it!)

255 - count will give you the inverse of the value of count.

The code can be tidied up quite a lot.

const byte led1 = 9;
const byte led2 = 10;

void setup() 
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  Serial.begin(115200);
}

void loop() 
{
  for(int count = 0; count <= 255; count++) 
  { 
    lightLeds(count);  
  }

  for(int count = 255; count >= 0; count--) 
  { 
    lightLeds(count);
  }
}

void lightLeds(int count)
{
  analogWrite(led1, count);
  analogWrite(led2, 255 - count);
  delay(5);  
}

3 parameters in each for loop
Cretae a function for code used more than once
count variable declared locally to where it are needed in the for loops
const byte used for LED pin numbers
Each brace on its own line to aid readability

Eoiser:

 for(count >= 255; count--;) { 

analogWrite(led, count);
  delay(5);
}
}




Also, why do i need a semicolon on the count-- in last loop, but not on the count++ in the first loop?

You think you need a semicolon, but the root cause is that your for statement is incorrect. A for statement needs 3 parameters:

  • declaration and init
  • condition
  • update
    These 3 need to be separated by a semicolon (;). For example:
for(int i = 4; i < 8; i++)

Notice that the for loop is just a shorthand for a WHILE loop:

int i = 4;
while(i < 8)
{
   /* statements */
   i++;
}

You don't have to specify either of the three. For example you can leave out the initialiser. When you apply this to your for loop, it becomes clear where the error is. Because count is already declared, you should have written:

for(;count >= 0; count--)
{
   ...
}

See?

UKHeliBob:
255 - count will give you the inverse of the value of count.

The code can be tidied up quite a lot.

const byte led1 = 9;

const byte led2 = 10;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  for(int count = 0; count <= 255; count++)
  {
    lightLeds(count); 
  }

for(int count = 255; count >= 0; count--)
  {
    lightLeds(count);
  }
}

void lightLeds(int count)
{
  analogWrite(led1, count);
  analogWrite(led2, 255 - count);
  delay(5); 
}



3 parameters in each for loop
Cretae a function for code used more than once
count variable declared locally to where it are needed in the for loops
const byte used for LED pin numbers
Each brace on its own line to aid readability

What benefits do i gain from increasing the baud rate from 9600 to 115200?

in this sketch.... none

What benefits do i gain from increasing the baud rate from 9600 to 115200?

As BL says, none in this sketch but it is the rate at which I usually set it. The important thing is that the baud rate in the program matches that of the serial device being communicated with. As there is no serial communication in this program the Serial.begin() is not even needed but I tend to put it in because of the inevitable need to print for debugging. I did not include it in the code posted but I originally had

Serial.print(count);
Serial.print("\t");
Serial.println(255 - count);

in the lightLeds() function which allowed me to see the values without the need to wire up the LEDs.