Go Down

Topic: void loop() not looping? (Read 1 time) previous topic - next topic

stealthtransam

when i put an If statment inside of a loop() function it does not continue looping.  Once the if statment is true the board remains in that state and doesn't change anymore.  attached is my code.  Any suggustions or is this a bug in the board?


int ledPin = 13;                 // LED connected to digital pin 13
int buttonPin = 7;              // pushbutton connect to digital pin 7
int val = 0;                    // Set initial state to LED off

void setup()
{
 pinMode(ledPin, OUTPUT);      // sets the digital pin 13 as output
 pinMode(buttonPin, INPUT);    // sets the gitial pin 7 as an input
}

void loop()
{
 val = digitalRead(buttonPin); //read the input of pin 7
 if(val == 1)
 {
   digitalWrite(ledPin, val);   // sets the LED of pin 13
 }
}

mellis

You don't have any code that would turn off the LED.  You don't actually need an if-statement at all, just do:

Code: [Select]

void loop()
{
 val = digitalRead(buttonPin); //read the input of pin 7
 digitalWrite(ledPin, val);   // sets the LED of pin 13
}

stealthtransam

#2
Jul 15, 2006, 07:56 am Last Edit: Jul 15, 2006, 08:01 am by stealthtransam Reason: 1
acutually the code i posted is not the code i had in question.  I aplogise.  I'll put the right code up here with my question once i get get home and copy and paste it again.

Go Up