x.xxExx
Kod: Markera allt
#include <LiquidCrystal.h>
LiquidCrystal lcd(40, 42, 28, 26, 24, 22);
unsigned long debounce_interval = 20;
unsigned long previousMillis = 0;
int up_state = HIGH;
int left_state = HIGH;
int down_state = HIGH;
int right_state = HIGH;
byte previous_up_state;
byte previous_down_state;
byte previous_right_state;
byte previous_left_state;
int last_right_state;
int last_left_state;
int last_up_state;
int last_down_state;
int up_down = 0;
int right_left = 0;
boolean done_right_left = HIGH;
boolean done_up_down = HIGH;
unsigned long debounce = 35;
unsigned long lastDebounceTime = 0;
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
byte up_reading = HIGH;
byte down_reading = HIGH;
byte right_reading = HIGH;
byte left_reading = HIGH;
String data;
char value[] = "6.48E-7";
const byte right = 49;
const byte left = 47;
const byte up = 53;
const byte down = 52;
unsigned long currentMillis = millis();
void setup() {
pinMode(up, INPUT);
pinMode(down, INPUT);
pinMode(left, INPUT);
pinMode(right, INPUT);
digitalWrite(up, HIGH);
digitalWrite(down, HIGH);
digitalWrite(left, HIGH);
digitalWrite(right, HIGH);
lcd.begin(20, 4);
lcd.clear();
}
void loop() {
check_buttons();
check_left_right();
}
void check_buttons() { //Kollar vad för knappar som tryckts ned
//och gör i botten det som skall i
//förhållande till knapp
byte up_reading = digitalRead(53);
byte down_reading = digitalRead(52);
byte right_reading = digitalRead(49);
byte left_reading = digitalRead(47);
if (up_reading != last_up_state) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounce) {
if (up_reading != up_state) {
up_state = up_reading;
if (up_state == LOW) {
up_down++;
if (up_down > 9)
up_down = 0;
}
}
}
previous_up_state = up_reading;
last_up_state = up_reading;
done_up_down = HIGH;
if (down_reading != last_down_state) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounce) {
if (down_reading != down_state) {
down_state = down_reading;
if (down_state == LOW) {
up_down--;
if (up_down < 0)
up_down = 9;
}
}
}
previous_down_state = down_reading;
last_down_state = down_reading;
done_up_down = HIGH;
if (right_reading != last_right_state) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounce) {
if (right_reading != right_state) {
right_state = right_reading;
if (right_state == LOW) {
right_left++;
if (right_left > 4)
right_left = 0;
}
}
}
previous_right_state = right_reading;
last_right_state = right_reading;
done_right_left = HIGH;
if (left_reading != last_left_state) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounce) {
if (left_reading != left_state) {
left_state = left_reading;
if (left_state == LOW) {
right_left--;
if (right_left < 0)
right_left = 4;
}
}
}
previous_left_state = left_reading;
last_left_state = left_reading;
done_right_left = HIGH;
}
//Allt ovan funkar som det skall, up_down och right_left
//räknas upp och ned precis som det skall enligt enkelt test
//Nedan så funkar right_left som det skall då cursorn
//skrivs ut på de olika ställena som förväntat men up_down
//skriver dock inte ut något alls vilket jag inte får ihop
void check_left_right() {
switch (right_left) {
case 0: {
if (done_right_left == HIGH) { //Så att det inte upprepas
lcd.setCursor(4, 1); //eller om knappen hålls intryckt
lcd.cursor();
done_right_left = LOW;
}
else if (done_up_down == HIGH) { //Exakt samma grund som ovan;
lcd.setCursor(4, 1); //trycks knappen ned skriv ut
lcd.print(up_down); //innehållet i up_down på displayen
done_up_down = LOW; //men nada händer
}
break;
}
case 1: {
if (done_right_left == HIGH) {
lcd.setCursor(6, 1);
lcd.cursor();
done_right_left = LOW;
}
else if (done_up_down == HIGH) {
lcd.setCursor(6, 1);
lcd.print(up_down);
done_up_down = LOW;
}
break;
}
case 2: {
if (done_right_left == HIGH) {
lcd.setCursor(7, 1);
lcd.cursor();
done_right_left = LOW;
}
else if (done_up_down == HIGH) {
lcd.setCursor(7, 1);
lcd.print(up_down);
done_up_down = LOW;
}
break;
}
case 3: {
if (done_right_left == HIGH) {
lcd.setCursor(9, 1);
lcd.cursor();
done_right_left = LOW;
}
else if (done_up_down == HIGH) {
lcd.setCursor(9, 1);
lcd.print(up_down);
done_up_down = LOW;
}
break;
}
case 4: {
if (done_right_left == HIGH) {
lcd.setCursor(10, 1);
lcd.cursor();
done_right_left = LOW;
}
else if (done_up_down == HIGH) {
lcd.setCursor(10, 1);
lcd.print(up_down);
done_up_down = LOW;
}
break;
}
}
}