Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*************************************************************************
- // Name : analog_buttons2 *
- // Author : Tarilayefa Edward *
- // Notice : Tari Electronics & Embedded Systems (TEES), 2018. *
- // : tarielectronics@yahoo.com *
- // : tarielect.edward@gmail.com *
- // : +23408184754883,+23408062251186, Nigeria. *
- // : https//:web.facebook.com/groups/teestraining/ *
- // : https//:web.facebook.com/groups/picarduino/ *
- // Date : 17/07/2018 *
- // Version : 1.0 *
- // Notes : A program to use 6 push buttons on one analog input pin. *
- // : Each analog push button toggles an arduino pin, displays *
- // : the analog value on serial monitor & lcd. *
- // Reference: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx *
- // Compiler : avrgcc *
- // IDE : Arduino 1.8.5 *
- // Target : Atmega 328, Arduino Uno, Nano. *
- //*************************************************************************
- # include <LiquidCrystal.h>
- LiquidCrystal lcd(7,6,5,4,3,2); //(data,clk)
- //-----------------------------------------------------------------------
- int analogIn = A0;
- int t1 = 100; //initialize debounce value
- //-----------------------------------------------------------------------
- int led1 = 8; //led1 pin
- int led2 = 9;
- int led3 = 10;
- int led4 = 11;
- int led5 = 12;
- int led6 = 13;
- //-----------------------------------------------------------------------
- int pins[6] = {8,9,10,11,12,13}; //led pins
- //-----------------------------------------------------------------------
- int analogVal; //adc variable
- //-----------------------------------------------------------------------
- //-----------------------------------------------------------------------
- void setup() //setup function
- {
- for (int x = 0; x < 6; x++){
- pinMode(pins[x], 1); //set pins to output mode
- }
- for (int y = 0; y <6; y++){
- digitalWrite(pins[y], 0); //turn off pins
- }
- lcd.begin(16,2); //initialize a 16x2 char lcd
- lcd.clear(); //clear lcd
- delay(100); //wait for 100ms
- lcd.print("Analog Buttons2 ");
- Serial.begin(9600); //initialize serial communication
- Serial.println("Analog Buttons2");
- delay(2000);
- }
- //-----------------------------------------------------------------------
- //-----------------------------------------------------------------------
- void loop() //main loop function
- {
- analogButtons();
- }
- //-----------------------------------------------------------------------
- //-----------------------------------------------------------------------
- void analogButtons(){ //analog button function
- //-----------------------------------------------------------------------
- analogVal = analogRead(A0);//get the analog value
- //-----------------------------------------------------------------------
- lcd.setCursor(0,1);
- lcd.print("A0=");
- lcd.print(analogVal);
- lcd.print(" ");
- Serial.print("Analog value = ");
- Serial.println(analogVal);
- //------------------------------------------------------------------
- //Ranges of analog value if button is pressed
- if ((analogVal >= 0) && (analogVal < 200)){ //if btn1 pressed
- delay(t1); //analog button debounce
- if ((analogVal >= 0) && (analogVal < 20)){ //if btn1 still pressed
- delay(t1); //analog button debounce
- Serial.println("Analog btn1 pressed");
- digitalWrite(led1, !digitalRead(led1)); //toggle pin
- lcd.setCursor(8,1);
- lcd.print("btn1");
- lcd.print(" ");
- }
- }
- //------------------------------------------------------------------
- if ((analogVal >= 200) && (analogVal < 400)){ //if btn2 pressed
- delay(t1);
- if ((analogVal >= 200) && (analogVal < 400)){//if btn2 still pressed
- delay(t1);
- Serial.println("Analog btn2 pressed");
- digitalWrite(led2, !digitalRead(led2));
- lcd.setCursor(8,1);
- lcd.print("btn2");
- lcd.print(" ");
- }
- }
- //-------------------------------------------------------------
- if ((analogVal >= 400) && (analogVal < 460)){ //if btn3 pressed
- delay(t1);
- if ((analogVal >= 400) && (analogVal < 460)){//if btn3 still pressed
- delay(t1);
- Serial.println("Analog btn3 pressed");
- digitalWrite(led3, !digitalRead(led3));
- lcd.setCursor(8,1);
- lcd.print("btn3");
- lcd.print(" ");
- }
- }
- //-------------------------------------------------------------
- if ((analogVal >= 460) && (analogVal < 480)){ //if btn4 pressed
- delay(t1);
- if ((analogVal >= 460) && (analogVal < 480)){//if btn4 still pressed
- delay(t1);
- Serial.println("Analog btn4 pressed");
- digitalWrite(led4, !digitalRead(led4));
- lcd.setCursor(8,1);
- lcd.print("btn4");
- lcd.print(" ");
- }
- }
- //-------------------------------------------------------------
- if ((analogVal >= 480) && (analogVal < 500)){ //if btn5 pressed
- delay(t1);
- if ((analogVal >= 480) && (analogVal < 500)){//if btn5 still pressed
- delay(t1);
- Serial.println("Analog btn5 pressed");
- digitalWrite(led5, !digitalRead(led5));
- lcd.setCursor(8,1);
- lcd.print("btn5");
- lcd.print(" ");
- }
- }
- //-------------------------------------------------------------
- if ((analogVal >= 500) && (analogVal < 530)){ //if btn6 pressed
- delay(t1);
- if ((analogVal >= 500) && (analogVal < 530)){//if btn6 still pressed
- delay(t1);
- Serial.println("Analog btn6 pressed");
- digitalWrite(led6, !digitalRead(led6));
- lcd.setCursor(8,1);
- lcd.print("btn6");
- lcd.print(" ");
- }
- }
- //-------------------------------------------------------------
- //delay(200);
- }
- //-------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement