Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 26th, 2012  |  syntax: C  |  size: 7.38 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <LiquidCrystal.h>
  2. #include <avr/pgmspace.h>
  3. #include <SD.h>
  4.  
  5. #define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
  6. #define LCD_BACKLIGHT_PIN         3  // D3 controls LCD backlight
  7. // ADC readings expected for the 5 buttons on the ADC input
  8. #define RIGHT_10BIT_ADC           0  // right
  9. #define UP_10BIT_ADC            145  // up
  10. #define DOWN_10BIT_ADC          329  // down
  11. #define LEFT_10BIT_ADC          505  // left
  12. #define SELECT_10BIT_ADC        741  // right
  13. #define BUTTONHYSTERESIS         10  // hysteresis for valid button sensing window
  14. //return values for ReadButtons()
  15. #define BUTTON_NONE               0  //
  16. #define BUTTON_RIGHT              1  //
  17. #define BUTTON_UP                 2  //
  18. #define BUTTON_DOWN               3  //
  19. #define BUTTON_LEFT               4  //
  20. #define BUTTON_SELECT             5  //
  21.  
  22. const int chipSelect = 2;
  23. int linecount = 0;
  24. byte buttonJustPressed  = false;         //this will be true after a ReadButtons() call if triggered
  25. byte buttonJustReleased = false;         //this will be true after a ReadButtons() call if triggered
  26. byte buttonWas          = BUTTON_NONE;   //used by ReadButtons() for detection of button events
  27.  
  28. LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
  29.  
  30. int count = 99;
  31. int randNumber = random(count);
  32. char buffer[30];
  33. unsigned int yorn = 0; // 0 no 1 yes
  34. int user = 0; //0 select 1 nic 2 laura
  35. int nic[99]; // 0 blank 1 yes 2 no
  36. int laura[99]; //0 blank 1 yes 2 no
  37. File dataFile = SD.open("names.txt");
  38. File indexFile = SD.open("index.txt");
  39.  
  40. void setup() {
  41.   if (!SD.begin(chipSelect)) {
  42.     Serial.println("Card failed, or not present");
  43.     // don't do anything more:
  44.     return;
  45.   }
  46.   Serial.println("card initialized.");
  47.   File dataFile = SD.open("names.txt");
  48.   File indexFile = SD.open("index.txt");
  49.   if (dataFile) {
  50.     while (dataFile.available()) {
  51.       char tmp = dataFile.read();
  52.       if (tmp == '\n') {
  53.         if (indexFile) {
  54.           linecount++;
  55.           indexFile.write(linecount);
  56.           indexFile.print(",");
  57.           indexFile.write(dataFile.position());
  58.         }
  59.         else
  60.         {
  61.           Serial.println("Could not open index.txt");
  62.         }
  63.       }
  64.     }
  65.     dataFile.close();
  66.     indexFile.close();
  67.   }  
  68.   // if the file isn't open, pop up an error:
  69.   else {
  70.     Serial.println("error opening datalog.txt");
  71.   }
  72.  
  73.   randomSeed(analogRead(3));
  74.   randNumber = random(count);
  75.  
  76.   pinMode( BUTTON_ADC_PIN, INPUT );         //ensure A0 is an input
  77.   digitalWrite( BUTTON_ADC_PIN, LOW );      //ensure pullup is off on A0
  78.   //lcd backlight control
  79.   digitalWrite( LCD_BACKLIGHT_PIN, HIGH );  //backlight control pin D3 is high (on)
  80.   pinMode( LCD_BACKLIGHT_PIN, OUTPUT );     //D3 is an output
  81.   lcd.begin(16, 2);
  82.   lcd.print(user);
  83.   Serial.begin(9600);
  84. }
  85.  
  86. void loop() {
  87.   if (!dataFile) File dataFile = SD.open("names.txt");
  88.   int i = 0;
  89.   randomSeed(analogRead(3));
  90.   int randNumber = random(linecount);
  91.   if (dataFile) {
  92.     while (dataFile.available()) {
  93.       char tmp = dataFile.read();
  94.       if (i == randNumber)
  95.       {
  96.         Serial.write(tmp);
  97.       }
  98.       if (tmp == '\n') i++;
  99.     }
  100.     //dataFile.close();
  101.   }
  102.   else {
  103.     Serial.println("WUT!");
  104.   }
  105.  
  106.   byte button;
  107.   button = ReadButtons();
  108.  
  109.   if( buttonJustPressed || buttonJustReleased )
  110.   {
  111.     //lcd.setCursor( 4, 1 );
  112.     //lcd.print( "            " );
  113.   }
  114.  
  115.   switch( button )
  116.   {
  117.   case BUTTON_NONE:
  118.     {
  119.       int length = strlen(buffer);
  120.       lcd.setCursor( 8-(length/2), 1 );
  121.       //lcd.print("    ");
  122.       lcd.print( buffer );
  123.       lcd.setCursor(0,0);
  124.       if (user == 0) lcd.print("Select User  ");
  125.       else if (user == 1) {
  126.         lcd.print("Nic Says:    ");
  127.         if (nic[randNumber] == 1) lcd.print("YES");
  128.         else if (nic[randNumber] == 2) lcd.print(" NO");
  129.       }
  130.       else if (user == 2) {
  131.         lcd.print("Laura Says:  ");
  132.         if (laura[randNumber] == 1) lcd.print("YES");
  133.         else if (laura[randNumber] == 2) lcd.print(" NO");
  134.       }
  135.       break;
  136.     }
  137.   case BUTTON_RIGHT:
  138.     {
  139.       lcd.setCursor( 13, 0 );
  140.       if (user == 1) nic[randNumber] = 1;
  141.       else if (user == 2) laura[randNumber] = 1;
  142.       break;
  143.     }
  144.   case BUTTON_UP:
  145.     {
  146.       user = 1;
  147.       lcd.setCursor(0,0);
  148.       lcd.print("                ");
  149.       break;
  150.     }
  151.   case BUTTON_DOWN:
  152.     {
  153.       user = 2;
  154.       lcd.setCursor(0,0);
  155.       lcd.print("                ");
  156.       break;
  157.     }
  158.   case BUTTON_LEFT:
  159.     {
  160.       lcd.setCursor( 13, 0 );
  161.       if (user == 1) nic[randNumber] = 2;
  162.       else if (user == 2) laura[randNumber] = 2;
  163.       break;
  164.     }
  165.   case BUTTON_SELECT:
  166.     {
  167.       randomSeed(analogRead(3));
  168.       randNumber = random(count);
  169.       strcpy_P(buffer, (char*)pgm_read_word(&(string_table[randNumber])));
  170.       lcd.setCursor( 0, 1 );
  171.       lcd.print( "    NEW NAME    " );
  172.       lcd.setCursor( 13, 0 );
  173.       lcd.print("   ");
  174.       delay(500);  
  175.       lcd.setCursor( 0, 1 );
  176.       lcd.print( "                " );
  177.       break;
  178.     }
  179.   default:
  180.     {
  181.       break;
  182.     }
  183.   }
  184.  
  185.  
  186.   //clear the buttonJustPressed or buttonJustReleased flags, they've already done their job now.
  187.   if( buttonJustPressed )
  188.     buttonJustPressed = false;
  189.   if( buttonJustReleased )
  190.     buttonJustReleased = false;
  191. }
  192. /*--------------------------------------------------------------------------------------
  193.  ReadButtons()
  194.  Detect the button pressed and return the value
  195.  Uses global values buttonWas, buttonJustPressed, buttonJustReleased.
  196.  --------------------------------------------------------------------------------------*/
  197. byte ReadButtons()
  198. {
  199.   unsigned int buttonVoltage;
  200.   byte button = BUTTON_NONE;   // return no button pressed if the below checks don't write to btn
  201.  
  202.   //read the button ADC pin voltage
  203.   buttonVoltage = analogRead( BUTTON_ADC_PIN );
  204.   //sense if the voltage falls within valid voltage windows
  205.   if( buttonVoltage < ( RIGHT_10BIT_ADC + BUTTONHYSTERESIS ) )
  206.   {
  207.     button = BUTTON_RIGHT;
  208.   }
  209.   else if(   buttonVoltage >= ( UP_10BIT_ADC - BUTTONHYSTERESIS )
  210.     && buttonVoltage <= ( UP_10BIT_ADC + BUTTONHYSTERESIS ) )
  211.   {
  212.     button = BUTTON_UP;
  213.   }
  214.   else if(   buttonVoltage >= ( DOWN_10BIT_ADC - BUTTONHYSTERESIS )
  215.     && buttonVoltage <= ( DOWN_10BIT_ADC + BUTTONHYSTERESIS ) )
  216.   {
  217.     button = BUTTON_DOWN;
  218.   }
  219.   else if(   buttonVoltage >= ( LEFT_10BIT_ADC - BUTTONHYSTERESIS )
  220.     && buttonVoltage <= ( LEFT_10BIT_ADC + BUTTONHYSTERESIS ) )
  221.   {
  222.     button = BUTTON_LEFT;
  223.   }
  224.   else if(   buttonVoltage >= ( SELECT_10BIT_ADC - BUTTONHYSTERESIS )
  225.     && buttonVoltage <= ( SELECT_10BIT_ADC + BUTTONHYSTERESIS ) )
  226.   {
  227.     button = BUTTON_SELECT;
  228.   }
  229.   //handle button flags for just pressed and just released events
  230.   if( ( buttonWas == BUTTON_NONE ) && ( button != BUTTON_NONE ) )
  231.   {
  232.     //the button was just pressed, set buttonJustPressed, this can optionally be used to trigger a once-off action for a button press eve
  233.     //it's the duty of the receiver to clear these flags if it wants to detect a new button change event
  234.     buttonJustPressed  = true;
  235.     buttonJustReleased = false;
  236.   }
  237.   if( ( buttonWas != BUTTON_NONE ) && ( button == BUTTON_NONE ) )
  238.   {
  239.     buttonJustPressed  = false;
  240.     buttonJustReleased = true;
  241.   }
  242.  
  243.   //save the latest button value, for change event detection next time round
  244.   buttonWas = button;
  245.  
  246.   return( button );
  247. }