3Pi Projects‎ > ‎

Project 4 - Push a Couple of Buttons

/* ############################################################################ */
/* ##                                                                        ## */
/* ##                    Project 4                                           ## */
/* ##                    Push a couple of buttons                            ## */
/* ##                    https://sites.google.com/site/3piprojects/          ## */
/* ##                                                                        ## */
/* ##                    Daniel J. Sullivan                                  ## */
/* ##                    July 22, 2011                                       ## */
/* ##                                                                        ## */
/* ############################################################################ */
   
/*
    1. Use the same initial code as in Project 3.
    2. Assign pinModes for the on board red and green LEDs.
    3. Use a while loop to wait for button A, B or C to be pushed.
    3. Use the main loop demonstrate a method to use multiple buttons to 
       control different actions:
           Push Button A: LEDs blink at a rate of 500 ms
           Push Button B: LEDs blink at a rate of 100 ms
           Push Button C: LEDs off.
           Push Button A and Button C: display battery voltage
*/

#include <OrangutanAnalog.h>
#include <OrangutanLCD.h> 

int redLED = 1;
int greenLED = 7;

int bat = OrangutanAnalog::readBatteryMillivolts();  

const int buttonA = 9;
const int buttonB = 12;
const int buttonC = 13;
  
void setup(){

  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, LOW);
  
  pinMode(buttonA, INPUT);            // set pins as inputs
  pinMode(buttonB, INPUT);
  pinMode(buttonC, INPUT);
  digitalWrite(buttonA, HIGH);        // enable pull-up resistors
  digitalWrite(buttonB, HIGH);
  digitalWrite(buttonC, HIGH);
  
  OrangutanLCD::clear();
  OrangutanLCD::print(bat);
  OrangutanLCD::print("mv");
  OrangutanLCD::gotoXY(0,1);
  OrangutanLCD::print("A B or C");  
  
  while (digitalRead(buttonA) == HIGH && digitalRead(buttonB) == HIGH && digitalRead(buttonC) == HIGH)
  {   
  }

  OrangutanLCD::clear();
  OrangutanLCD::print("Let's");
  OrangutanLCD::gotoXY(0,1);
  OrangutanLCD::print("Blink!");
  delay(1000);  
  
  while (digitalRead(buttonA) == HIGH && digitalRead(buttonB) == HIGH && digitalRead(buttonC) == HIGH)
  {   
  }
}

/*
   Main Loop of program is comprised of four if loops each that contain a while loop.
   1. When button A is pressed the "Blink Slow" message is displayed on LCD.
      The While loop is used to blink the LEDs until either button B or C are pressed.
   2. When button B is pressed the "Blink Fast" message is displayed on LCD.
      The While loop is used to blink the LEDs until either button A or C are pressed.
   3. When button C is pressed the "LEDs Off" message is displayed on LCD.
      The LEDs are turned off and a While loop waits until either button A or B are pressed.
   4. When buttons A and C are both held down, the battery voltage is checked and displayed on LCD.
      The empty while loop runs until either button A or B or C are pressed.
*/

void loop()
  
//************* Push Button A ******************  
  
  if (digitalRead(buttonA) == LOW)
  {
    OrangutanLCD::clear();
    OrangutanLCD::print("Blink");
    OrangutanLCD::gotoXY(0,1);
    OrangutanLCD::print("Slow"); 
    
    while(digitalRead(buttonB) == HIGH && digitalRead(buttonC) == HIGH)
    {
      digitalWrite(redLED, HIGH);
      digitalWrite(greenLED, LOW);
      delay(500);
      digitalWrite(redLED, LOW);
      digitalWrite(greenLED, HIGH);
      delay(500);
    }
  }
  
//************* Push Button B ******************
  
  if (digitalRead(buttonB) == LOW)
  {
    OrangutanLCD::clear();
    OrangutanLCD::print("Blink");
    OrangutanLCD::gotoXY(0,1);
    OrangutanLCD::print("Fast"); 
    
    while(digitalRead(buttonA) == HIGH && digitalRead(buttonC) == HIGH)
    {
      digitalWrite(redLED, HIGH);
      digitalWrite(greenLED, LOW);
      delay(100);
      digitalWrite(redLED, LOW);
      digitalWrite(greenLED, HIGH);
      delay(100);
    }
  }
  
//************* Push Button C ******************
  
   if (digitalRead(buttonC) == LOW)
  {
    OrangutanLCD::clear();
    OrangutanLCD::print("LEDs");
    OrangutanLCD::gotoXY(0,1);
    OrangutanLCD::print("Off"); 
   
    digitalWrite(redLED, LOW);
    digitalWrite(greenLED, LOW);
      
    while(digitalRead(buttonA) == HIGH && digitalRead(buttonB) == HIGH)
    {  
    }
  }
  
//************* Push both Button A and Button C ******************
  
   if (digitalRead(buttonA) == LOW && digitalRead(buttonC) == LOW)
  {
    digitalWrite(redLED, LOW);
    digitalWrite(greenLED, LOW);
    bat = OrangutanAnalog::readBatteryMillivolts();  
    OrangutanLCD::clear();
    OrangutanLCD::print(bat);
    OrangutanLCD::print("mv");
    OrangutanLCD::gotoXY(0,1);
    OrangutanLCD::print("Push B"); 
    
    while(digitalRead(buttonB) == HIGH)
    {
    }        // close while()
  }          // close if()
}            // close loop()


//**************************** End of Program ***************************


counter on tumblr
Č
ċ
Daniel Sullivan,
Jul 26, 2011, 8:59 AM
Comments