Coding a switch.

Hi, I'm currently working on a project with an LDR and 3 LED's, so baisically the more light the more LED's will turn on, however I want to give the option to turn the LED's manually aswell, I have a small switch and I was wondering whether I could do the following sequence:

  • Press once, 1 LED
  • Press twice, 2 LED's
  • Press the third time, 3 LED's
  • And finally the fourth time the switch is being pressed all LED's will turn off.

Thank you!

Similar to this code, but instead of 0 to 255, you want 0 to 3.

const byte LED = 6;
const byte Button = 2;

void setup()
{
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT_PULLUP); // switch is normally HIGH and will go LOW when pressed.
}

void loop()
{
  static byte last = LOW;
  static short count = 0;
  byte ButtonState = digitalRead(Button);

  if (ButtonState != last) // if the button is not equal to its last state ie held.
  {
    if (ButtonState == LOW) // if button is pressed
    {
      Serial.println(count);
      analogWrite(LED, count += 5); increment count by a factor of 5
    }
    last = ButtonState;// update last button state.
  }

  if (count > 255) // if count goes over 255, reset it back to zero and set LED off
  {
    digitalWrite(LED, LOW);
    count = 0;
  }
}

Also is it a switch or a button?

HazardsMind:
Similar to this code, but instead of 0 to 255, you want 0 to 3.

const byte LED = 6;

const byte Button = 2;

void setup()
{
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT_PULLUP); // switch is normally HIGH and will go LOW when pressed.
}

void loop()
{
  static byte last = LOW;
  static short count = 0;
  byte ButtonState = digitalRead(Button);

if (ButtonState != last) // if the button is not equal to its last state ie held.
  {
    if (ButtonState == LOW) // if button is pressed
    {
      Serial.println(count);
      analogWrite(LED, count += 5); increment count by a factor of 5
    }
    last = ButtonState;// update last button state.
  }

if (count > 255) // if count goes over 255, reset it back to zero and set LED off
  {
    digitalWrite(LED, LOW);
    count = 0;
  }
}




Also is it a switch or a button?

Thanks, and it’s a button, my bad!

Here’s my coding:

//Amine Elazri
//19/01/2015
//LDR Circuit V001

int LDR_Pin = A0; //analog pin 0
int Led13 = 13;
int Led12 = 12;
int Led11= 11;

void setup() // One time run

{
Serial.begin(9600); //Data transmition between the board and the computer (9600bps)
pinMode (Led11, OUTPUT);
pinMode (Led12, OUTPUT);
pinMode (Led13, OUTPUT);
}

void loop() // will read multiple times “in a loop”

{
int LDRReading = analogRead(LDR_Pin);

Serial.println(LDRReading);

delay(250); // the interger is the amount of ms that the

if(LDRReading > 850)
{
digitalWrite; (Led11, LOW);
digitalWrite; (Led12, LOW);
digitalWrite; (Led13, LOW);
}

else if ((LDRReading >575) && (LDRReading <850))
{
digitalWrite; (Led11, HIGH);
digitalWrite; (Led12, LOW);
digitalWrite; (Led13, LOW);

}

else if ((LDRReading >350) && (LDRReading <575))
{
digitalWrite; (Led11, HIGH);
digitalWrite; (Led12, HIGH);
digitalWrite; (Led13, LOW);

}
else if ((LDRReading >250) && (LDRReading <350))

digitalWrite; (Led11, HIGH);
digitalWrite; (Led12, HIGH);
digitalWrite; (Led13, HIGH);
{

}
}

How would I incorporate it? Sorry I’ve just been learning this for a few weeks now!

if (count == 1) //turn on one LED

else if (count == 2) //turn on two LEDs

else if (count == 3) //turn on three LEDs

else // count = 0 or greater than 3 // all off

HazardsMind: if (count == 1) //turn on one LED

else if (count == 2) //turn on two LEDs

else if (count == 3) //turn on three LEDs

else // count = 0 or greater than 3 // all off

So will I need to put this in?

digitalWrite; (Led11, HIGH); digitalWrite; (Led12, LOW); digitalWrite; (Led13, LOW);

and so on?

Sorry im a newbie!

yes

HazardsMind:
yes

Thanks, can I put it anywhere in the sketch or does it have to be at the top?

in the loop