Go Down

Topic: starting out with arduino (Read 454 times) previous topic - next topic

Shuttleu

Dec 26, 2012, 09:49 pm Last Edit: Dec 26, 2012, 09:51 pm by Shuttleu Reason: 1
Hello,

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
Code: [Select]
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

Arrch

#1
Dec 26, 2012, 09:53 pm Last Edit: Dec 26, 2012, 09:55 pm by Arrch Reason: 1
Code: [Select]

for (int i = 0; i == 3000; i++)


The English version of this line of code would be

Set i to zero. While i is 3000, increment i.

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.

Shuttleu


Code: [Select]

for (int i = 0; i == 3000; i++)


The English version of this line of code would be

Set i to zero. While i is 3000, increment i.

Doesn't sound right does it?


i see, i havent done programming for a very long time, my mistake

it should be <= not ==

thank you!

Pedro147

Hello Shuttleu,
   I am far from being proficient with coding  8) but just a few things I might suggest are -

   1. 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.
   2. 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.
http://www.pedroduino.com

WizenedEE



Code: [Select]

for (int i = 0; i == 3000; i++)


The English version of this line of code would be

Set i to zero. While i is 3000, increment i.

Doesn't sound right does it?


i see, i havent done programming for a very long time, my mistake

it should be <= not ==

thank you!


Note that that will run 3001 times

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy