//Sample using LiquidCrystal library #include #include Servo myservo; // create servo object to control a servo /******************************************************* Tento program otestuje Merkurino 01 s potenciometrem a modelářské servo Ladislav Vohralik 2014, MerkurRobot.cz ********************************************************/ // select the pins used on the LCD panel LiquidCrystal lcd(13, 12, 8, 7, 4, 2); // define some values used by the panel and buttons int lcd_key = 0; int adc_key_in = 0; int val; // proměnná pro hodnotu serva int pot; // proměnná pro hodnotu potenciometru int pot1; // proměnná pro hodnotu potenciometru int potpin = 2; // analog pin used to connect the potentiometer #define btnRIGHT 0 #define btnUP 1 #define btnDOWN 2 #define btnLEFT 3 #define btnENTER 4 #define btnESC 5 #define btnNONE 6 // read the buttons int read_LCD_buttons() { adc_key_in = analogRead(0); // přečti klávesnici na A0 //lcd.setCursor(12,1);lcd.print(adc_key_in); lcd.print(" "); // my buttons when read are centered at these valies: 0, 144, 329, 504, 741 // we add approx 50 to those values and check to see if we are close if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result // pro Merkurino 01 plati tyto hodnoty: if (adc_key_in < 40) return btnESC; if (adc_key_in < 110) return btnLEFT; if (adc_key_in < 300) return btnDOWN; if (adc_key_in < 450) return btnUP; if (adc_key_in < 700) return btnRIGHT; if (adc_key_in < 850) return btnENTER; return btnNONE; // when all others fail, return this... } void setup() { Serial.begin(9600); pinMode(A1, OUTPUT); myservo.attach(A1); // attaches the servo on pin 9 to the servo object lcd.begin(16, 2); // start the library lcd.setCursor(0,0); lcd.print("Stiskni tlacitko"); // print a simple message } void loop() //začátek nekonečné smyčky { lcd.setCursor(8,1); // move cursor to second line "1" and 9 spaces over lcd.print(millis()/1000); // display seconds elapsed since power-up lcd.setCursor(0,1); // move to the begining of the second line lcd_key = read_LCD_buttons(); // read the buttons val = adc_key_in; val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) switch (lcd_key) // depending on which button was pushed, we perform an action { case btnRIGHT: { lcd.print("vpravo "); myservo.write(val); // sets the servo position according to the scaled value break; } case btnLEFT: { lcd.print("vlevo "); myservo.write(val); // sets the servo position according to the scaled value break; } case btnUP: { lcd.print("nahoru "); myservo.write(val); // sets the servo position according to the scaled value break; } case btnDOWN: { lcd.print("dolu "); myservo.write(val); // sets the servo position according to the scaled value break; } case btnENTER: { lcd.print("potvrd "); myservo.write(val); // sets the servo position according to the scaled value break; } case btnESC: { lcd.print("zpet "); myservo.write(val); // sets the servo position according to the scaled value break; } case btnNONE: { lcd.print("nic "); pot = analogRead(potpin); // přečti potenciometr na A2 pot = map(pot, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) myservo.write(pot); // sets the servo position according to the scaled value if ( pot!= pot1 ){ // uložení stávající hodnoty potenciometru pot1 = pot; // aby se posílala pouze změna (pootočení) Serial.println(pot); } break; } } }