2 inputs controlling 2 outputs

Hi folks, Im hoping someone here can help me - Im a technology and engineering teacher and we are switching from PBasic STAMP to Arduino and trying to create a workbook for pupils to follow to help them learn how to use it. The only problem is that Im not that skilled at it yet. I've been following Jeremy Blum's on-line tutorials and have found them of great benefit.
In his second tutorial he goes over how to keep an output state on as well as debouncing a switch (code below)

/*
Arduino Tutorials
Episode 2
Switch3 Program (debounced)
Written by: Jeremy Blum
*/

int switchPin = 8;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ledOn = !ledOn;
  }
  lastButton = currentButton;
  
  digitalWrite(ledPin, ledOn);

}

my problem is that Im now trying to modify this to create work for my pupils. My idea is that they will design a queueing system for a theme park, in which an employee will push one button and it will show a red stop light, and when ready push another button that will switch this light off, and show green, later press the first button, green goes off and red goes on......you get the point!

Ive modified the inputs and outputs to set them up properly as shown

/*
Queue control System
*/

int greenswitchPin = 2;
int redswitchPin = 4;
int greenledPin = 8;
int redledPin = 13;
boolean lastButton = LOW;			
boolean currentButton = LOW;	
boolean ledOn = false;

void setup()
{
  pinMode(greenswitchPin, INPUT);
  pinMode(redswitchPin, INPUT);
  pinMode(greenledPin, OUTPUT);
  pinMode(redledPin, OUTPUT);
}

but REALLY confused where to go from here - do I just have to copy the next part of the sketch for red, and then for gree?

can anyone help me please while Ive still got some hair on my head! haha

thanks in advance!!!

But if we post a solution here, won't the students see it too?

no as it was only a recent decision to buy and use arduino - alot of our PBAsic Boards broke recently so my boss decided we will do this instead (hence me working through my summer holidays). The pupils dont know about our change in direction yet.

const byte greenSwitchPin = 2;
const byte redSwitchPin   = 4;
const byte greenLEDpin    = 8;
const byte redLEDpin      = 13;

boolean lastGreenState;			
boolean lastRedState;			
boolean greenLEDstate;  // true == ON, false == off.
boolean redLEDstate;

void setup()
{
  pinMode(greenSwitchPin, INPUT_PULLUP);
  lastGreenState = digitalRead (greenSwitchPin);
  pinMode(redSwitchPin, INPUT_PULLUP);
  lastRedState = digitalRead (redSwitchPin);

  pinMode(greenLEDpin, OUTPUT);
  pinMode(redLEDpin, OUTPUT);
  writeLED (greenLEDpin, greenLEDstate);
  writeLED (redLEDPin, redLEDstate);
}

void writeLED (const byte pin, boolean state)
{
#ifdef COMMON_ANODE
  digitWrite (pin, state ? LOW : HIGH);
#else  
  digitWrite (pin, state ? HIGH : LOW);
#endif
}

This is really basic stuff.

There are commands digitalRead and digitalWrite.

Read the green button pin. Write it's state to the green LED pin, and the inverted state to the red LED pin. (turning the green LED on and the red one off.) Do the same sort of thing for the red LED button.

BTW, Arduino inputs have an optional pullup resistor that lets you avoid the need for external inputs. Since it's a pull UP resistor, though, you wire the switch to ground the pin, and then it's LOW when not pressed and HIGH when pressed. You therefore have to invert your logic.

pmacbeath:
Hi folks, Im hoping someone here can help me - Im a technology and engineering teacher and we are switching from PBasic STAMP to Arduino and trying to create a workbook for pupils to follow to help them learn how to use it. The only problem is that Im not that skilled at it yet. I've been following Jeremy Blum's on-line tutorials and have found them of great benefit.
In his second tutorial he goes over how to keep an output state on as well as debouncing a switch (code below)

/*

Arduino Tutorials
Episode 2
Switch3 Program (debounced)
Written by: Jeremy Blum
*/

int switchPin = 8;
int ledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;

void setup()
{
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

void loop()
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ledOn = !ledOn;
  }
  lastButton = currentButton;
 
  digitalWrite(ledPin, ledOn);

}




my problem is that Im now trying to modify this to create work for my pupils. My idea is that they will design a queueing system for a theme park, in which an employee will push one button and it will show a red stop light, and when ready push another button that will switch this light off, and show green, later press the first button, green goes off and red goes on......you get the point!

Ive modified the inputs and outputs to set them up properly as shown


/*
Queue control System
*/

int greenswitchPin = 2;
int redswitchPin = 4;
int greenledPin = 8;
int redledPin = 13;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;

void setup()
{
  pinMode(greenswitchPin, INPUT);
  pinMode(redswitchPin, INPUT);
  pinMode(greenledPin, OUTPUT);
  pinMode(redledPin, OUTPUT);
}




but REALLY confused where to go from here - do I just have to copy the next part of the sketch for red, and then for gree?

can anyone help me please while Ive still got some hair on my head! haha

thanks in advance!!!

If you are skilled with the Basic Stamp. Then it's easy to port programs to Arduino. The code is 90% the same. Only the I/O is different.

steinie44:
If you are skilled with the Basic Stamp. Then it's easy to port programs to Arduino. The code is 90% the same. Only the I/O is different.

not really - you cant set up sub routines here as easily and you dont have to worry about things like switches bouncing or outputs only switching on once the button is pressed, then switching off when released. Ive got a program that works by transferring from Basic Stamp (as below)

/*
Thempark traffic lights
*/

int stopPin = 2;
int goPin = 4;
int redPin =9;
int greenPin = 13;


void setup() 
{ 
pinMode(redPin, OUTPUT); 
pinMode(greenPin, OUTPUT);   // initialize the LED pins as outputs
pinMode(stopPin, INPUT); 
pinMode(goPin, INPUT);       // initialize the pushbutton pins as inputs
}

void loop() 
{ 
  if (digitalRead(stopPin) == HIGH) 
  {digitalWrite(redPin, HIGH);}       //if the input is on put the light ON
else
if (digitalRead(goPin) == HIGH) 
  {digitalWrite(greenPin, HIGH);}     //if the input is on put the light ON
else
{ digitalWrite(redPin, LOW); 
  digitalWrite(greenPin, LOW); }     //both lights OFF

but my problem is adding the debounce to this and keeping the LEDs on once the switch is pressed. In Basic Stamp things stay on until you say switch it off and an input module never has any problems with switches bouncing.

By the way thanks everyone who has helped so far. And that you in particular to AWOL for giving me code, but to be honest with you I have no clue what alot of the stuff means (only been working with this for 3 weeks maybe?). I know I can look it up and will do later on the many websites Ive nbookmarked and books Ive bought, but Im also not really wanting to use them in a way as Im trying to relate it more to Jeremy Plums programme (they will be doign this first) so the pupils can see the progression - and then later add some of the newer code that you have entered.

From my dim recollection of any kind of BASIC, setting up subroutines was harder and much less flexible than in C.
Take this routine from the code you posted above

boolean debounce(boolean last)
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

(OK, we'll ignore that it uses the hated "delay()" for now)

One tiny change makes it more general-purpose

boolean debounce(byte pin, boolean last)
{
  boolean current = digitalRead(pin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(pin);
  }
  return current;
}

I'm tempted to rewrite the example, using a full-blown state machine, but I think for students, it is often useful to show them how to do something one way, let them play with it and discover for themselves the limitations, then show them the better way.
(I teach SCUBA diving, so I don't normally use this approach!)
Sometimes, for simple stuff (like a couple of switches and a couple of outputs), the Rolls Royce solution isn't necessary.

got it everyone - thanks for your help! My working code is below:

/*
Queue control System
*/
int greenSwitchPin = 2;
int redSwitchPin = 4;
int greenLEDPin = 8;
int redLEDPin = 13;
boolean greenlastButton = LOW;                 
boolean greencurrentButton = LOW;      
boolean greenLEDon = false;
boolean redlastButton = LOW;                   
boolean redcurrentButton = LOW;
boolean redLEDon = false;
void setup()
{
  pinMode(greenSwitchPin, INPUT);
  pinMode(redSwitchPin, INPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
}
boolean greendebounce(boolean last)            
{
        boolean current = digitalRead(greenSwitchPin); 
         if (last != current)                    
{
delay(5);              
current = digitalRead(greenSwitchPin);      
}
return current;        
}
boolean reddebounce(boolean last)              
{
        boolean current = digitalRead(redSwitchPin);   
         if (last != current)                    
{
delay(5);              
current = digitalRead(redSwitchPin);      
}
return current;        
}
void loop()
{
  greencurrentButton = greendebounce(greenlastButton); 
  redcurrentButton = reddebounce(redlastButton); 
if (greenlastButton == LOW && greencurrentButton == HIGH)       
 {
   
   greenLEDon = !greenLEDon;
   redLEDon = !greenLEDon; 
}
  greenlastButton = greencurrentButton;        
 digitalWrite(greenLEDPin, greenLEDon);
if (redlastButton == LOW && redcurrentButton == HIGH)      
 {
   
   redLEDon = !redLEDon;
   greenLEDon = !redLEDon;
}
  redlastButton = redcurrentButton;            
 digitalWrite(redLEDPin, redLEDon);
}

Just saw your newest post AWOL so will try that out in a minute. thanks :slight_smile:

not really - you cant set up sub routines here as easily and you dont have to worry about things like switches bouncing or outputs only switching on once the button is pressed, then switching off when released.

Sub routines are simple, just call it void name()
Switches bounce in any language, nature of the beast.
Outputs stay where you put them unless you change them.
In an example like yours, you don't have to debounce the buttons to turn on a LED.

AWOL I can see the change you made to my sketch - boolean debounce(byte pin, boolean last) - what does byte pin command do?

AWOL:

const byte greenSwitchPin = 2;

const byte redSwitchPin   = 4;
const byte greenLEDpin    = 8;
const byte redLEDpin      = 13;

boolean lastGreenState;
boolean lastRedState;
boolean greenLEDstate;  // true == ON, false == off.
boolean redLEDstate;

void setup()
{
  pinMode(greenSwitchPin, INPUT_PULLUP);
  lastGreenState = digitalRead (greenSwitchPin);
  pinMode(redSwitchPin, INPUT_PULLUP);
  lastRedState = digitalRead (redSwitchPin);

pinMode(greenLEDpin, OUTPUT);
  pinMode(redLEDpin, OUTPUT);
  writeLED (greenLEDpin, greenLEDstate);
  writeLED (redLEDPin, redLEDstate);
}

void writeLED (const byte pin, boolean state)
{
#ifdef COMMON_ANODE
  digitWrite (pin, state ? LOW : HIGH);
#else 
  digitWrite (pin, state ? HIGH : LOW);
#endif
}

thanks for doing this for me, but if im being honest I havent a clue what most of it means. is this the thing you were ferrefing to in the other thread? :~