Hi, I just started programing for Arduino with "Getting Started Guide" and I programming by myself. I don't copy. I have problem. I wrote program and i used %. Program doesn't work well but it's good I thing. Its third program - " Example 03A: Turn on LED when the button is pressed and keep it on after it is released"
I know i can write program like man in the book but its good to know for future.
//Podmínka pro prepinac
if (val == 1) {
p = p + 1;
}
// Podminka pro svetlo
if ((p % 2) == 0) {
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
}
The loop() will happen tens of thousands of times a second in that code, so unless you can see a light blinking that quickly, it will look like the light is always on.
Just to prove a point, add the following line at the end of your loop:
delay(100);
Is it doing what you expect now?
Try reducing the delay time. Notice what is happening?
As a side note, don't compare the return value of digitalRead() to 1 or 0, compare it to HIGH or LOW. For all you know, one day Arduino might make digitalRead() return 2 and your code won't work anymore, whereas if you use HIGH, it probably would still.
Ok. I made probably some mistake in program - thats why It wont stay turned off so it's not because of "%". I was sure I did no mistake but I probably did. Sorry for spam with so silly problem. I'am happy that I can use in the future "%". Thanks
Here, you are comparing val to the result of reading the pin state, and discarding the result. You are not assigning the result of the read to the variable.
Here, you are comparing val to the result of reading the pin state, and discarding the result. You are not assigning the result of the read to the variable.
YES! You're right. I'am happy I programmed well. Stupid mistake. Thank you