Well I want a variable to go like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and then reset to 1 and then goes from 1 to 20. HOW DO I DO IT??
Well I want a variable to go like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and then reset to 1 and then goes from 1 to 20. HOW DO I DO IT??
can you give me the code I don't really understand the for loop thingy @koudelad
Let me try to explain the example code from the link above. I am on my phone, so this may not work so well.
// Dim an LED using a PWM pin
int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10
void setup()
{ // no setup needed }
void loop()
{ for (int i=0; i <= 255; i++)
{ analogWrite(PWMpin, i);
delay(10); }
}
The line that has the “for” function declares variable “i” as an integer and sets the value to 0. The “for” loop will run as long as i is less than or equal to 255. It will send the value of i to the PWM pin to vary the brightness of the LED. the i++ means that the variable i gets incremented each loop.
instead of writing the value of i to a pin, you could print it instead.
Let me try to explain the example code from the link above. I am on my phone, so this may not work so well.
// Dim an LED using a PWM pin
int PWMpin = 10; // LED in series with 470 ohm resistor on pin 10
void setup()
{ // no setup needed }
void loop()
{ for (int i=0; i <= 255; i++)
{ analogWrite(PWMpin, i);
delay(10); }
}
The line that has the “for” function declares variable “i” as an integer and sets the value to 0. The “for” loop will run as long as i is less than or equal to 255. It will send the value of i to the PWM pin to vary the brightness of the LED. the i++ means that the variable i gets incremented each loop.
instead of writing the value of i to a pin, you could print it instead.
I'm sure there are many E14 members who could write the exact code you are after, but I suggest a better way to understand it would be to try out the code in your own Arduino IDE - e.g. the code that scmclain has kindly just explained. Then start to adjust that code and hopefully how it works will become clearer. If you get stuck then post the error messages and your existing code and we'll help you along .
If you understand what scmclain has explained then you may want to look up 'nested loops' to do your two separate counts (like this link at Tutorialspoint ).
Rod