3Pi Projects‎ > ‎

Project 5 - Push a couple of buttons using Libraries

/*  
   ############################################################################ 
   ##                                                                        ## 
   ##                    Project 5                                           ## 
   ##                    Push a couple of buttons using Library              ## 
   ##                    https://sites.google.com/site/3piprojects/          ## 
   ##                                                                        ## 
   ##                    Daniel J. Sullivan                                  ## 
   ##                    July 19, 2011                                       ## 
   ##                                                                        ## 
   ############################################################################ 
   
   
   This program has the same functionality as Project 4, but the coding is simplified 
   through the use of the Pushbuttons and LEDs libraries.
   The library command calls are simplified by assigning simpler names to the library calls.
   The use of the OrangutanBuzzer library is introduced.
*/

#include <OrangutanAnalog.h>
#include <OrangutanLCD.h>
#include <OrangutanPushbuttons.h>
#include <OrangutanLEDs.h>
#include <OrangutanBuzzer.h>

/*
  Declare simple names...
  So instead of having to write OrangutanLCD::clear(), one can use lcd.clear()
*/

OrangutanAnalog analog;
OrangutanLCD lcd;
OrangutanPushbuttons buttons;
OrangutanLEDs leds;
OrangutanBuzzer buzzer;

int bat = analog.readBatteryMillivolts();
int buttonA = 9;
int buttonB = 12;
int buttonC = 13;

unsigned char button;   // Define a button variable
  
void setup(){
  
// Battery voltage is written to the LCD.
// and...
// the buzzer plays a 6 kHz tone of 250 ms at a volume of 7 (volume is somewhat linear from 1 to 15).
  
  lcd.clear();
  lcd.print(bat);
  lcd.print("mv");
  lcd.gotoXY(0,1);
  lcd.print("A B or C"); 
  leds.red(LOW);
  leds.green(LOW);
  buzzer.playFrequency(6000, 250, 7);

// When any of the three buttons are pushed the message on the LCD is changed and the program proceeds 
// to the main program loop

  button = buttons.waitForPress(ANY_BUTTON);
  delay(250);
  lcd.clear();
  lcd.print("Let's");
  lcd.gotoXY(0,1);
  lcd.print("Blink!");
  buzzer.playFrequency(6000, 250, 7);
  button =buttons.waitForPress(ANY_BUTTON);
  
}

/*
   Main Loop of program has four possible events:
   1. When button A is pressed the "Blink Slow" message is displayed on LCD, and
      the LEDs blink at a 500 ms rate until either button B or C are pressed.
   2. When button B is pressed the "Blink Fast" message is displayed on LCD, and
      the LEDs blink at a 100 ms rate until either button A or C are pressed..
   3. When button C is pressed the "LEDs Off" message is displayed on LCD, and
      both LEDs turn off until buttons A or B are pressed.
   4. When buttons A and C are both held down, the battery voltage is checked and displayed on LCD
      until either button A or B are pressed.
*/

void loop()
  
//************* Push Button A ****************** 
  
  if (buttons.isPressed(buttonA))
  {
    buzzer.playFrequency(6000, 250, 7);
    lcd.clear();
    lcd.print("Blink");
    lcd.gotoXY(0,1);
    lcd.print("Slow"); 
    
    while(digitalRead(buttonB) == HIGH && digitalRead(buttonC) == HIGH)
    {
      leds.red(HIGH);
      leds.green(LOW);
      delay(500);
      leds.red(LOW);
      leds.green(HIGH);
      delay(500);
    }
  }
  
//************* Push Button B ****************** 
  
  if (buttons.isPressed(buttonB))
  {
    buzzer.playFrequency(6000, 250, 7);
    lcd.clear();
    lcd.print("Blink");
    lcd.gotoXY(0,1);
    lcd.print("Fast"); 
    
    while(digitalRead(buttonA) == HIGH && digitalRead(buttonC) == HIGH)
    {
      leds.red(HIGH);
      leds.green(LOW);
      delay(100);
      leds.red(LOW);
      leds.green(HIGH);
      delay(100);
    }
  }
  
//************* Push Button C ****************** 
  
  if (buttons.isPressed(buttonC))
  {
    buzzer.playFrequency(6000, 250, 7);
    lcd.clear();
    lcd.print("LEDs");
    lcd.gotoXY(0,1);
    lcd.print("Off"); 
    
    while(digitalRead(buttonA) == HIGH && digitalRead(buttonB) == HIGH)
    {
      leds.green(LOW);
      leds.red(LOW);
    }
  }
  
//************* Push Button A and C ****************** 
  
  if (digitalRead(buttonA) == LOW && digitalRead(buttonC) == LOW)
  {
    leds.green(LOW);
    leds.red(LOW);
    
    bat = analog.readBatteryMillivolts();  
    lcd.clear();
    lcd.print(bat);
    lcd.print("mv");
    lcd.gotoXY(0,1);
    lcd.print("Push B"); 
    
    while(digitalRead(buttonB) == HIGH)
    {
    }
  }
}
     
//**************************** End of Program ***************************
   
  

counter on tumblr
ċ
Daniel Sullivan,
Jul 26, 2011, 9:00 AM
Comments