Advertisement
Tarielect

analog_buttons2

Jul 17th, 2018
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.97 KB | None | 0 0
  1. //*************************************************************************
  2. //  Name     : analog_buttons2                                            *
  3. //  Author   : Tarilayefa Edward                                          *
  4. //  Notice   : Tari Electronics & Embedded Systems (TEES), 2018.          *
  5. //           : tarielectronics@yahoo.com                                  *
  6. //           : tarielect.edward@gmail.com                                 *
  7. //           : +23408184754883,+23408062251186, Nigeria.                  *
  8. //           : https//:web.facebook.com/groups/teestraining/              *
  9. //           : https//:web.facebook.com/groups/picarduino/                *
  10. //  Date     : 17/07/2018                                                 *
  11. //  Version  : 1.0                                                        *
  12. //  Notes    : A program to use 6 push buttons on one analog input pin.   *
  13. //           : Each analog push button toggles an arduino pin, displays   *
  14. //           : the analog value on serial monitor & lcd.                  *
  15. //  Reference: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx         *
  16. //  Compiler : avrgcc                                                     *
  17. //  IDE      : Arduino 1.8.5                                              *
  18. //  Target   : Atmega 328, Arduino Uno, Nano.                             *
  19. //*************************************************************************
  20. # include <LiquidCrystal.h>
  21. LiquidCrystal lcd(7,6,5,4,3,2); //(data,clk)
  22. //-----------------------------------------------------------------------
  23. int analogIn = A0;
  24. int t1 = 100; //initialize debounce value
  25. //-----------------------------------------------------------------------
  26. int led1 = 8; //led1 pin
  27. int led2 = 9;
  28. int led3 = 10;
  29. int led4 = 11;
  30. int led5 = 12;
  31. int led6 = 13;
  32. //-----------------------------------------------------------------------
  33. int pins[6] = {8,9,10,11,12,13}; //led pins
  34. //-----------------------------------------------------------------------
  35. int analogVal;  //adc variable
  36. //-----------------------------------------------------------------------
  37. //-----------------------------------------------------------------------
  38. void setup() //setup function
  39. {
  40.     for (int x = 0; x < 6; x++){
  41.         pinMode(pins[x], 1); //set pins to output mode
  42.     }
  43.     for (int y = 0; y <6; y++){
  44.         digitalWrite(pins[y], 0); //turn off pins
  45.     }
  46.     lcd.begin(16,2); //initialize a 16x2 char lcd
  47.     lcd.clear(); //clear lcd
  48.     delay(100); //wait for 100ms
  49.     lcd.print("Analog Buttons2 ");
  50.     Serial.begin(9600); //initialize serial communication
  51.     Serial.println("Analog Buttons2");
  52.     delay(2000);
  53. }
  54. //-----------------------------------------------------------------------
  55. //-----------------------------------------------------------------------
  56. void loop() //main loop function
  57. {
  58.     analogButtons();   
  59. }
  60. //-----------------------------------------------------------------------
  61. //-----------------------------------------------------------------------
  62. void analogButtons(){ //analog button function
  63.     //-----------------------------------------------------------------------
  64.     analogVal = analogRead(A0);//get the analog value
  65.     //-----------------------------------------------------------------------
  66.     lcd.setCursor(0,1);
  67.     lcd.print("A0=");
  68.     lcd.print(analogVal);
  69.     lcd.print("  ");
  70.     Serial.print("Analog value = ");
  71.     Serial.println(analogVal);
  72.     //------------------------------------------------------------------
  73.     //Ranges of analog value if button is pressed
  74.     if ((analogVal >= 0) && (analogVal < 200)){ //if btn1 pressed
  75.         delay(t1); //analog button debounce
  76.         if ((analogVal >= 0) && (analogVal < 20)){ //if btn1 still pressed
  77.             delay(t1); //analog button debounce
  78.             Serial.println("Analog btn1 pressed");
  79.             digitalWrite(led1, !digitalRead(led1)); //toggle pin
  80.             lcd.setCursor(8,1);
  81.             lcd.print("btn1");
  82.             lcd.print("     ");
  83.         }
  84.     }
  85.     //------------------------------------------------------------------
  86.     if ((analogVal >= 200) && (analogVal < 400)){ //if btn2 pressed
  87.         delay(t1);
  88.         if ((analogVal >= 200) && (analogVal < 400)){//if btn2 still pressed
  89.             delay(t1);
  90.             Serial.println("Analog btn2 pressed");
  91.             digitalWrite(led2, !digitalRead(led2));
  92.             lcd.setCursor(8,1);
  93.             lcd.print("btn2");
  94.             lcd.print("     ");
  95.         }
  96.     }
  97.     //-------------------------------------------------------------
  98.     if ((analogVal >= 400) && (analogVal < 460)){ //if btn3 pressed
  99.         delay(t1);
  100.         if ((analogVal >= 400) && (analogVal < 460)){//if btn3 still pressed
  101.             delay(t1);
  102.             Serial.println("Analog btn3 pressed");
  103.             digitalWrite(led3, !digitalRead(led3));
  104.             lcd.setCursor(8,1);
  105.             lcd.print("btn3");
  106.             lcd.print("     ");
  107.         }
  108.     }
  109.     //-------------------------------------------------------------
  110.     if ((analogVal >= 460) && (analogVal < 480)){ //if btn4 pressed
  111.         delay(t1);
  112.         if ((analogVal >= 460) && (analogVal < 480)){//if btn4 still pressed
  113.             delay(t1);
  114.             Serial.println("Analog btn4 pressed");
  115.             digitalWrite(led4, !digitalRead(led4));
  116.             lcd.setCursor(8,1);
  117.             lcd.print("btn4");
  118.             lcd.print("     ");
  119.         }
  120.     }
  121.     //-------------------------------------------------------------
  122.     if ((analogVal >= 480) && (analogVal < 500)){ //if btn5 pressed
  123.         delay(t1);
  124.         if ((analogVal >= 480) && (analogVal < 500)){//if btn5 still pressed
  125.             delay(t1);
  126.             Serial.println("Analog btn5 pressed");
  127.             digitalWrite(led5, !digitalRead(led5));
  128.             lcd.setCursor(8,1);
  129.             lcd.print("btn5");
  130.             lcd.print("     ");
  131.         }
  132.     }
  133.     //-------------------------------------------------------------
  134.     if ((analogVal >= 500) && (analogVal < 530)){ //if btn6 pressed
  135.         delay(t1);
  136.         if ((analogVal >= 500) && (analogVal < 530)){//if btn6 still pressed
  137.             delay(t1);
  138.             Serial.println("Analog btn6 pressed");
  139.             digitalWrite(led6, !digitalRead(led6));
  140.             lcd.setCursor(8,1);
  141.             lcd.print("btn6");
  142.             lcd.print("     ");
  143.         }
  144.     }
  145.     //-------------------------------------------------------------
  146.     //delay(200);
  147. }
  148. //-------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement