Advertisement
sunu

myArduinoUno328pAVR combined_template

Oct 9th, 2013
135
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Do not remove the include below
  2. // Untuk eclipse IDE
  3. #include "unoTemplate.h"
  4. //-----------------------------------------------------------------
  5. /*
  6. //The setup function is called once at startup of the sketch
  7. //Bare minimum Arduino sketch:
  8.  
  9. void setup()
  10. {
  11. // Add your initialization code here
  12. }
  13.  
  14. // The loop function is called in an endless loop
  15. void loop()
  16. {
  17. //Add your repeated code here
  18. }
  19. */
  20.  
  21. //-----------------------------------------------------------------
  22.  
  23. /*  //Untuk AVR
  24.     //avr-gcc -Wall  -g -c "%f"
  25.     //avr-gcc -Wall  -g -o "%e" "%f"
  26.  
  27.  
  28.     #define __AVR_ATmega328P__
  29.  
  30.     #ifndef F_CPU
  31.        #define F_CPU 16000000UL
  32.     #endif
  33.  
  34.     #include <avr/io.h>
  35.     #include <util/delay.h>
  36.  
  37.     int main(void)
  38.     {
  39.             return 0;
  40.     }
  41. */
  42. //-----------------------------------------------------------------
  43.  
  44.  
  45.  
  46.     //Sample using LiquidCrystal library
  47.     #include <LiquidCrystal.h>
  48.  
  49.     /*******************************************************
  50.  
  51.     This program will test the LCD panel and the buttons
  52.     Mark Bramwell, July 2010
  53.  
  54.     ********************************************************/
  55.     /*
  56.     Analog 0    Button (select, up, right, down and left)
  57.     Digital 4   DB4
  58.     Digital 5   DB5
  59.     Digital 6   DB6
  60.     Digital 7   DB7
  61.     Digital 8   RS (Data or Signal Display Selection)
  62.     Digital 9   Enable
  63.     Digital 10  Backlit Control
  64.     */
  65.     // select the pins used on the LCD panel
  66.     LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  67.  
  68.     // define some values used by the panel and buttons
  69.     int lcd_key     = 0;
  70.     int adc_key_in  = 0;
  71.     #define btnRIGHT  0
  72.     #define btnUP     1
  73.     #define btnDOWN   2
  74.     #define btnLEFT   3
  75.     #define btnSELECT 4
  76.     #define btnNONE   5
  77.  
  78.     int freeRam ();
  79.  
  80.     // read the buttons
  81.     int read_LCD_buttons()
  82.     {
  83.      adc_key_in = analogRead(0);      // read the value from the sensor
  84.      // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  85.      // we add approx 50 to those values and check to see if we are close
  86.      if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  87.      // For V1.1 us this threshold
  88.      /*
  89.      if (adc_key_in < 50)   return btnRIGHT;
  90.      if (adc_key_in < 250)  return btnUP;
  91.      if (adc_key_in < 450)  return btnDOWN;
  92.      if (adc_key_in < 650)  return btnLEFT;
  93.      if (adc_key_in < 850)  return btnSELECT;
  94.      */
  95.  
  96.      // For V1.0 comment the other threshold and use the one below:
  97.  
  98.      if (adc_key_in < 50)   return btnRIGHT;
  99.      if (adc_key_in < 195)  return btnUP;
  100.      if (adc_key_in < 380)  return btnDOWN;
  101.      if (adc_key_in < 555)  return btnLEFT;
  102.      if (adc_key_in < 790)  return btnSELECT;
  103.  
  104.  
  105.      return btnNONE;  // when all others fail, return this...
  106.     }
  107.  
  108.     void setup()
  109.     {
  110.      //lcd.begin(16, 2);              // start the library
  111.      //lcd.setCursor(0,0);
  112.  
  113.      Serial.begin(9600);
  114.      Serial.println("\n[memCheck]");
  115.      Serial.println(freeRam());
  116.  
  117.      //lcd.print("Push the buttons"); // print a simple message
  118.      delay(500);
  119.  
  120.     }
  121.  
  122.     void loop()
  123.     {
  124.  
  125.      //lcd.setCursor(9,1);            // move cursor to second line "1" and 9 spaces over
  126.      //lcd.print(millis()/1000);      // display seconds elapsed since power-up
  127.  
  128.  
  129.      //lcd.setCursor(0,1);            // move to the begining of the second line
  130.      //lcd_key = read_LCD_buttons();  // read the buttons
  131.  
  132.      switch (lcd_key)               // depending on which button was pushed, we perform an action
  133.      {
  134.        case btnRIGHT:
  135.          {
  136.          //lcd.print("RIGHT ");
  137.          break;
  138.          }
  139.        case btnLEFT:
  140.          {
  141.          //lcd.print("LEFT   ");
  142.          break;
  143.          }
  144.        case btnUP:
  145.          {
  146.          //lcd.print("UP    ");
  147.          break;
  148.          }
  149.        case btnDOWN:
  150.          {
  151.          //lcd.print("DOWN  ");
  152.          break;
  153.          }
  154.        case btnSELECT:
  155.          {
  156.          //lcd.print("SELECT");
  157.          break;
  158.          }
  159.          case btnNONE:
  160.          {
  161.          //lcd.print("NONE  ");
  162.          break;
  163.          }
  164.      }//endof switch
  165.  
  166.  
  167.  
  168.  
  169.     }//end of loop function
  170.  
  171.     // freeRam function credit:http://jeelabs.org/2011/05/22/atmega-memory-use/
  172.     int freeRam () {
  173.       extern int __heap_start, *__brkval;
  174.       int v;
  175.       return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  176.     }
Advertisement
RAW Paste Data Copied
Advertisement