Pages: [1]   Go Down
Author Topic: simple count up and down nested in if function  (Read 391 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello everyone,

i'm pretty new to arduino, and i've been working on a testing program which i just can't get to work. i've read a whole lotta references and forum posts but it seems i just cant get the code right. maybe anyone can help me get my thinking straight?

i connected a toggle switch to pin2, and i want to have the onboard led blink one way for 20 times then the other way for 20 times. if the switch is turned to off, i'll just have the led blink quickly.
its a test for a stepper motor driver that will have three different velocities (like the blinking), so instead of setting the coils, in this testing program i just set the led to blink.

here's the code:


 
int i;
 const int inPin = 2;
 const int ledPin = 13;
 int buttonState = 0;

void setup() {               
 
  pinMode(ledPin, OUTPUT);
  pinMode(inPin, INPUT); 
  Serial.begin(9600);

}

void loop() {
 
    buttonState = digitalRead(inPin); 
  Serial.println (buttonState);
 
 
 
  if (buttonState == HIGH)
   {
   
     
    for (int i = 0; i < 20; i++);
    {
    digitalWrite(13, HIGH);   
    delay(1000);             
    digitalWrite(13, LOW);   
    delay(1000); 
    Serial.println(i);
   
    }

   for (int i = 20; i>0; i--);
    {
   
    digitalWrite(13, HIGH);   
    delay(2000);             
    digitalWrite(13, LOW);   
    delay(200);
    Serial.println(i);
 
    }
   }
 

 else if (buttonState== LOW)
   {
     
      digitalWrite(13, HIGH);   
  delay(200);             
  digitalWrite(13, LOW);   
  delay(200);
 
  }
}


any information highly appreciated : ) i'm pretty sure its something very simple that just hadn't crossed my mind...
thank you.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26313
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How is the switch wired?

Quote
but it seems i just cant get the code right
You have, I assume, some idea in your mind of how the code should behave.
There is also some difference between this idea and how it actually does behave.
Do you want to share your observations?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This:
Code:
for (int i = 0; i < 20; i++);
isn't helping. You need to remove the semi-colon at the end. Same issue several times
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dear wildbill!
i already thought it had to be something so stupid that i just hadn't found in my my-brain-already-feels-like-mash-potatoes-today- haze... : )
of course it works perfectly now.

thank you very much!
Logged

Pages: [1]   Go Up
Jump to: