Switching leds , & creating a timed switch ,

I have just bought an Arduio Leonardo because I was making a circuit that switches LEDs on and off and someone said "You could do that with an Arduino ", and so I am going to try. The first stumbling block has been finding sketches that resemble what I am looking for . These are two of the basic functions that need controlling.

  1. A set of four leds are controlled by four switches , each switch turns on it's own led and at the same time turns off any other of the leds that is on . Leds are only turned on by their own switch and only turned off by another switch. They are not sequential.

  2. In a second set of leds , an led is turned on by pressing and releasing a momentary switch , then turned off by pressing and releasing again - unless the switch is pressed and held for more than 1 second , in which case it switches off on release.

Any pointers towards relevant sketches would be helpful , just to have somewhere to start.
Thanks

There are sketch examples that come with the IDE.

Go through these sketches.

After you review the examples, your project should be easily accomplished.

When you have tried to write some code and if you run into problems, we can help.

Olino:

  1. A set of four leds are controlled by four switches , each switch turns on it's own led and at the same time turns off any other of the leds that is on . Leds are only turned on by their own switch and only turned off by another switch. They are not sequential.

Do a site search on 'radio button'.

larryd:
There are sketch examples that come with the IDE.

Go through these sketches.

+1

dougp:
Do a site search on 'radio button'.

Hopefully you would find my code:

// Radio Buttons!
const int led1Pin =  3;    // LED pin number
const int button1 =  2;
const int led2Pin =  5;
const int button2 =  4;
const int led3Pin =  6;
const int button3 =  7;
const int led4Pin =  9;
const int button4 =  8;
char bstate1 = 0;
char bstate2 = 0;
char bstate3 = 0;
char bstate4 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.
unsigned long bcount2 = 0;
unsigned long bcount3 = 0;
unsigned long bcount4 = 0;


// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
// Routines by Paul__B of Arduino Forum
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) {
    *marker += interval;    // move on ready for next interval
    return true;
  }
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
// Routines by Paul__B of Arduino Forum
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
    case 0: // Button up so far,
      if (button == HIGH) return false; // Nothing happening!
      else {
        *butnstate = 2;                 // record that is now pressed
        *marker = millis();             // note when was pressed
        return false;                   // and move on
      }

    case 1: // Button down so far,
      if (button == LOW) return false; // Nothing happening!
      else {
        *butnstate = 3;                 // record that is now released
        *marker = millis();             // note when was released
        return false;                   // and move on
      }

    case 2: // Button was up, now down.
      if (button == HIGH) {
        *butnstate = 0;                 // no, not debounced; revert the state
        return false;                   // False alarm!
      }
      else {
        if (millis() - *marker >= interval) {
          *butnstate = 1;               // jackpot!  update the state
          return true;                  // because we have the desired event!
        }
        else
          return false;                 // not done yet; just move on
      }

    case 3: // Button was down, now up.
      if (button == LOW) {
        *butnstate = 1;                 // no, not debounced; revert the state
        return false;                   // False alarm!
      }
      else {
        if (millis() - *marker >= interval) {
          *butnstate = 0;               // Debounced; update the state
          return false;                 // but it is not the event we want
        }
        else
          return false;                 // not done yet; just move on
      }
    default:                            // Error; recover anyway
      {
        *butnstate = 0;
        return false;                   // Definitely false!
      }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(button2, INPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(button3, INPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(button4, INPUT);
  digitalWrite (led1Pin, LOW);
  digitalWrite (led2Pin, LOW);
  digitalWrite (led3Pin, LOW);
  digitalWrite (led4Pin, LOW);
}

void loop() {
  // Select LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    digitalWrite (led1Pin, HIGH);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, LOW);
  }
  // Select LED if button debounced
  if (butndown(digitalRead(button2), &bcount2, &bstate2, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, HIGH);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, LOW);
  }
  // Select LED if button debounced
  if (butndown(digitalRead(button3), &bcount3, &bstate3, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, HIGH);
    digitalWrite (led4Pin, LOW);
  }
  // Select LED if button debounced
  if (butndown(digitalRead(button4), &bcount4, &bstate4, 10UL )) {
    digitalWrite (led1Pin, LOW);
    digitalWrite (led2Pin, LOW);
    digitalWrite (led3Pin, LOW);
    digitalWrite (led4Pin, HIGH);
  }
}

Looks complex but deadly effective!

Can't presently help with question 2, but my debounce routine would be the basis of it, detecting reliably when you press and release the button.

Have a look at some code I found from marcello.romani. He posted it on jan 1st, 2013.
The topic on subject is called : “button pressed more than 2 seconds do somethingelse”
Marcello’s code is so KISS (Keep It Simple Stupid) and efficient that it deserves a medal.
The goals is to handle the different signals that come from a button or switch action :
keyPress, keyRelease, longKeyPress etc… as you can find in the original sketch.
A good advice, start by soldering a 1 microF capacitor, parallel over each button.
Later when you are a bit used to coding, you can try to debounce buttons and switches with an algoritm.
Put all the variables needed for buttons and output (LED’s in your case) in arrays
e.g:
bool currKeyState[12] = {HIGH, HIGH, HIGH, HIGH};
bool prevKeyState[12] = {HIGH, HIGH, HIGH, HIGH};
Here it’s about 12 buttons, the first one is called button 0, because of the way arrays are pointed at.
Preset your buttons to HIGH if a pressed button sets the input to 0 (zero Volt); at release time, the inputs will have to be set to 5 Volt through a pullup resistor. But you can also work the other way around.

What the buttons will do precisely is your coding job.
Good luck.

Thanks to all for your replies , I figured out a way to do it not unlike Paul__B suggested. And the second set of Leds I got to work with "the switch on release after a second " using someone else's code ... I'm not sure quite why they work as they do but it's gradually becoming clearer.

The code I used is this:
Buttons 1-4 switch on leds 1-4 and then they are switched off by another of leds 1-4 being switched on.
Button 5 toggles leds 5 and 6 when pressed , and also switches when released if held for more than a second.

The keyboard code is part of the next stage and not relevant to this question.
Thanks again for your advice.

const int  buttonPin1 = 7;    // the pin that the pushbutton 1 is attached to
const int  buttonPin2 = 8;    // the pin that the pushbutton 2 is attached to
const int  buttonPin3 = 9;    // the pin that the pushbutton 3 is attached to
const int  buttonPin4 = 10;   // the pin that the pushbutton 4 is attached to
const int  buttonPin5 = 11;  // the pin that the pushbutton 5 is attached to
const int ledPin1 = 1;         // the pin that the LED1 is attached to
const int ledPin2 = 2;         // the pin that the LED2 is attached to
const int ledPin3 = 3;         // the pin that the LED3 is attached to
const int ledPin4 = 4;         // the pin that the LED4 is attached to
const int ledPin5 = 5;       // the pin that the LED5 is attached to
const int ledPin6 = 6;       // the pin that the LED6 is attached to
int lastPin1State,lastPin2State,lastPin3State,lastPin4State,lastPin5State;

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;
#include <Keyboard.h>
void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  pinMode(buttonPin5, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  startMillis = millis();  //initial start time
  Keyboard.begin();
}



void loop()
{
  // read the pushbutton input pin:
  int pin1State = digitalRead(buttonPin1);
  int pin2State = digitalRead(buttonPin2);
  int pin3State = digitalRead(buttonPin3);
  int pin4State = digitalRead(buttonPin4);
  int pin5State = digitalRead(buttonPin5);
  int pin6State = digitalRead(ledPin1);
  int pin7State = digitalRead(ledPin5);
  
{  // these four ifs turn on one of ledpin1 /2 /3 /4  and turns off whichever of the other 4 leds is on.

   if (pin1State == HIGH && lastPin1State == LOW )
  {
    digitalWrite(ledPin1,HIGH);
    digitalWrite(ledPin2,LOW);
    digitalWrite(ledPin3,LOW);
    digitalWrite(ledPin4,LOW);
  }
   if (pin2State == HIGH && lastPin2State == LOW )
  {
    digitalWrite(ledPin2,HIGH);
    digitalWrite(ledPin1,LOW);
    digitalWrite(ledPin3,LOW);
    digitalWrite(ledPin4,LOW);
  }

    if (pin3State == HIGH && lastPin3State == LOW )
  {
    digitalWrite(ledPin3,HIGH);
    digitalWrite(ledPin1,LOW);
    digitalWrite(ledPin2,LOW);
    digitalWrite(ledPin4,LOW);
  }

   
   if (pin4State == HIGH && lastPin4State == LOW )
  {
    digitalWrite(ledPin4,HIGH);
    digitalWrite(ledPin1,LOW);
    digitalWrite(ledPin2,LOW);
    digitalWrite(ledPin3,LOW);
  }
    
//the next step toggles leds 5 and 6 on button 5 press , and also on release if held for more than 1 second.
    
  if
      (pin6State == HIGH && (pin5State == HIGH && lastPin5State == LOW))    // this toggles ledPin5 and ledPin6 if ledPin1 is on.
  
   
   { startMillis = currentMillis;
    digitalWrite(ledPin6, !digitalRead(ledPin6));
   
     digitalWrite(ledPin5, !digitalRead(ledPin6));
   }
   
    
    
    {
      currentMillis = millis();
     if (currentMillis - startMillis >= period)
      if (pin6State == HIGH && (pin5State == LOW && lastPin5State == HIGH))  
     {                                                                      
       digitalWrite(ledPin6, !digitalRead(ledPin6));
   
     digitalWrite(ledPin5, !digitalRead(ledPin6));
     
    }
 
   lastPin5State = pin5State;
     
    
  delay(10);  //debounce 

  
}
   Keyboard.write('r');
}
}