I got a arduino for christmas yesterday and have started using it, i feel i have a understanding of it but i have come across a problem i cant seem to understand
i will first post my sketch then explain my problem
const int button = 7;
const int led = 2;
int ledState = 1;
int ledSwitch = -1;
int buttonState = 0;
void setup(){
pinMode(button, INPUT);
pinMode(led, OUTPUT);
digitalWrite(led, ledState);
Serial.begin(9600);
}
void loop(){
buttonState = digitalRead(button);
Serial.println(buttonState);
if (buttonState == 1){
for (int i = 0; i == 3000; i++){
delay(10);
Serial.println('test');
if (digitalRead(button)==0) {
ledState += ledSwitch;
ledSwitch *= -1;
digitalWrite(led, ledState);
}
}
}
delay(100);
}
i believe the problem is somewhere in my sketch not my wiring as the button and the led works perfectly fine
right my problem is, when i press the button i can tell the button goes high, as the serial monitor is filled with 1's when i press the button. It should then go into the for loop, however it never does, i can tell because the serial monitor never says "test" which it should do at least once
Doesn't sound right does it? The for loop will continue as long as the second statement in the for loop is true, so after setting i to 0, checking if it's 3000 will fail, and the loop will immediately exit.
Hello Shuttleu,
I am far from being proficient with coding 8) but just a few things I might suggest are -
where you wish to print test in the serial monitor, test should be in double quotation marks i.e "test". I think that numbers
use the single quotation marks.
It is best to place a 1000ms delay after any println statement so you actually have time to read it before it scrolls off the
serial monitor.
Have fun with your Arduino.