Go Down

Topic: Flash rate change not working! (Read 31 times) previous topic - next topic

Trying to change the rate at which an LED is flashing by using an if statment.  it is not working...   here is my code ... any help??  what did i do wrong??

/*
flashing one LED at two different rates...
Feed 5v to pin 4 ... the flash rate will slow down.
*/

const int slowdown = 4;
const int ledPin = 13;
int t1 = 35;
int t5 = 500;

void setup(){
  pinMode(slowdown, INPUT);
  pinMode(ledPin, OUTPUT);
 
  fastflash();
 
  slowflash();
}

void loop(){
  digitalRead(slowdown);
  if(slowdown == HIGH){
    slowflash();
  }
  else{
    fastflash();
  }
}

void fastflash(){
  digitalWrite(ledPin, HIGH);
  delay(t1);
  digitalWrite(ledPin, LOW);
  delay(t1);
}

void slowflash(){
  digitalWrite(ledPin, HIGH);
  delay(t5);
  digitalWrite(ledPin, LOW);
  delay(t5);
}
 
 

LarryD

Try changing

digitalRead(slowdown);
  if(slowdown == HIGH)

To

  if(digitalRead(slowdown) == HIGH)
The way you have it in your schematic isn't the same as how you have it wired up!

Robin2

#2
Oct 29, 2014, 10:17 am Last Edit: Oct 29, 2014, 10:18 am by Robin2
Code: [Select]
digitalRead(slowdown);
  if(slowdown == HIGH){



This code does NOT save the value from digitalRead(). slowdown is the pin number

You need to save the value to a variable - for example

Code: [Select]
byte buttonVal = digitalRead(slowdown);
  if(buttonVal == HIGH){


...R

Go Up