Guest User

fix of http://pastebin.com/86JXkUBf

a guest
Aug 2nd, 2013
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // include the library code:
  2. #include <LiquidCrystal.h>
  3.   int byteincome = 0;
  4. // initialize the library with the numbers of the interface pins
  5. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  6. //Key message
  7. int adc_key_val[5] = {
  8.   30, 150, 360, 535, 760
  9. };
  10. String statuses[] = {
  11.   "Coding", "Doing Homework", "Please Leave", "Away", "Sleeping", "Calling Home", "Gaming", "GET OUT"
  12. };
  13. String top[] = {
  14.   "Status:", "Adam:", "Oliver:", "Adam & Oliver:", "Awesomeness", "OBEY:"
  15. };
  16. int NUM_KEYS = 5;
  17. int adc_key_in;
  18. int key = -1;
  19. int oldkey = -1;
  20. int mode = 0;
  21. int blp = 10;
  22. int i = 0;
  23. int j = 0;
  24.  
  25. void setup() {
  26.   // set up the LCD's number of columns and rows:
  27.   lcd.begin(16, 2);
  28.   lcd.setCursor(0, 1);
  29.   intro();
  30.   Serial.begin(9600);
  31.   pinMode(10, OUTPUT);
  32.   digitalWrite(blp, HIGH);
  33. }
  34.  
  35. void loop() {
  36.   // set the cursor to column 0, line 1
  37.   // (note: line 1 is the second row, since counting begins with 0):
  38.  
  39.   adc_key_in = analogRead(0); // read the value from the sensor
  40.   key = get_key(adc_key_in); // convert into key press
  41.   if (key != oldkey) // if keypress is detected
  42.   {
  43.     delay(50); // wait for debounce time
  44.     adc_key_in = analogRead(0); // read the value from the sensor
  45.     key = get_key(adc_key_in); // convert into key press
  46.     if (key != oldkey) {
  47.       oldkey = key;
  48.       if (key == 0) {
  49.         j++;
  50.         if (j > 5) {
  51.           j = 0;
  52.         }
  53.         lcd.clear();
  54.         lcd.setCursor(0, 0);
  55.         lcd.print(top[j]);
  56.         lcd.setCursor(0, 1);
  57.         lcd.print(statuses[i]);
  58.       }
  59.       if (key == 3) {
  60.         j--;
  61.         if (j < 0) {
  62.           j = 5;
  63.         }
  64.         lcd.clear();
  65.         lcd.setCursor(0, 0);
  66.         lcd.print(top[j]);
  67.         lcd.setCursor(0, 1);
  68.         lcd.print(statuses[i]);
  69.       }
  70.       if (key == 1) //down key
  71.       {
  72.         i--;
  73.         if (i == -1) {
  74.           i = 7;
  75.         }
  76.         lcd.clear();
  77.         lcd.setCursor(0, 0);
  78.         lcd.print(top[j]);
  79.         lcd.setCursor(0, 1);
  80.         lcd.print(statuses[i]);
  81.       }
  82.       if (key == 2) //up key
  83.       {
  84.         i++;
  85.         if (i > 7) {
  86.           i = 0;
  87.         }
  88.         lcd.clear();
  89.         lcd.setCursor(0, 0);
  90.         lcd.print(top[j]);
  91.         lcd.setCursor(0, 1);
  92.         lcd.print(statuses[i]);
  93.       } else if (key == 4) {
  94.         if (mode == 0) {
  95.           mode = 1;
  96.           lcd.noDisplay();
  97.           digitalWrite(10, LOW);
  98.         } else if (mode == 1) {
  99.           mode = 2;
  100.           lcd.display();
  101.           digitalWrite(10, LOW);
  102.         } else if (mode == 2) {
  103.           mode = 0;
  104.           lcd.display();
  105.           digitalWrite(10, HIGH);
  106.         }
  107.       }
  108.     }
  109.   }
  110. }
  111.  
  112. // Convert ADC value to key number
  113. int get_key(unsigned int input) {
  114.   int k;
  115.   for (k = 0; k < NUM_KEYS; k++) {
  116.     if (input < adc_key_val[k]) {
  117.       return k;
  118.     }
  119.   }
  120.   if (k >= NUM_KEYS)
  121.     k = -1; // No valid key pressed
  122.   return k;
  123.  
  124. }
  125. int intro() {
  126.   lcd.setCursor(0, 0);
  127.   lcd.print("Select: Mode");
  128.   lcd.setCursor(0, 1);
  129.   lcd.print("Left/Right: Top");
  130.   delay(1500);
  131.   lcd.clear();
  132.   lcd.setCursor(0, 0);
  133.   lcd.print("Left/Right: Top");
  134.   lcd.setCursor(0, 1);
  135.   lcd.print("Up/Down: Scroll");
  136.   delay(1500);
  137.   lcd.clear();
  138.   lcd.setCursor(0, 0);
  139.   lcd.print("Up/Down: Scroll");
  140.   lcd.setCursor(0, 1);
  141.   lcd.print("Status:");
  142.   delay(1500);
  143.   lcd.clear();
  144.   lcd.setCursor(0, 0);
  145.   lcd.print("Status:");
  146.   lcd.setCursor(0, 1);
  147.   lcd.print("Coding");
  148.   return 0;
  149. }
Add Comment
Please, Sign In to add comment