I'm trying to combine a push button and the switch case to make some light project. How do I program a push button with a switch case?
How do I program a push button
I don't think you can program a push button. It would certainly be easier to program the Arduino.
with a switch case?
Wouldn't that depend, at least in part, on what you want to do? What are the cases of interest?
If you want to use the switch (){} function, check out the reference page for details: http://arduino.cc/en/Reference/SwitchCase. We need more information to be of more help.
Onions.
man i suck at asking question.
so here's the switch case method
switch (var) {
case label:
// statements
break;
case label:
// statements
break;
default:
// statements
}
I wanted to use the switch case to change lights mode. for example:
if I use a push button and push it once. it will blink the led for 100milli on/off. push it the second time will turn it on. push it third time will turn it off. i know i have to use a switch case but don't know exactly how to put a push button in it
i.e.
switch (pushbutton) {
case 1:
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
break;
case 2:
digitalWrite(13, HIGH);
break;
default:
digitalWrite(13, LOW);
}
better? let me know if I make it clearer. thanks..
Read up on the Bounce library, and implement it.
Pseudocode for what you're trying to do:
if button is pressed
increment state by 1 mod 3 ((++state)%3)
switch(state)
case 1: // etc.
Here is a sample program that was written by
Mark Bramwell. It is for the DFRobot LCD shield
that uses one of the analog inputs to generate
a signal that software decodes to determine which
of 5 buttons on the shield have been pressed. I
do not know Mark. The code works.
Perhaps if you study what Mark did you can
build from there.
RWW
//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
/*******************************************************
This program will test the LCD panel and the buttons
Mark Bramwell, July 2010
********************************************************/
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
int ledpin = 13;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup()
{
lcd.begin(16, 2); // start the lcd
lcd.setCursor(0,0);
lcd.print("Push the buttons"); // print a simple message
}
void loop()
{
lcd.setCursor(9,1); // move cursor to second line "1" and 9 spaces over
lcd.print(millis()/1000); // display seconds elapsed since power-up
//lcd.print (adc_key_in);
lcd.setCursor(0,1); // move to the begining of the second line
ReadCount = 0;
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.print(" RIGHT");
break;
}
case btnLEFT:
{
lcd.print(" LEFT");
break;
}
case btnUP:
{
lcd.print (" UP");
break;
}
case btnDOWN:
{
lcd.print(" DOWN");
break;
}
case btnSELECT:
{
lcd.print("SELECT");
break;
}
case btnNONE:
{
lcd.print(" NONE");
// lcd.print(DropCount);
break;
}
}
}