Ich möchte über ein paar buttons einen text in den Arduino eingeben.
Ich wollte jede stelle einzeln eingeben indem ich durch das Alphabet scrolle und dann mit einem Button bestätige.
Nachher soll die Eingabe auf einem LCD erscheinen und ich möchte eine CNC so steuern, dass sie den Text schreibt. Dabei dachte ich, dass ich jeden Buchstabe einzeln von der Maschine fräsen lasse.
// Konstanten geben die Pins der Buttons an
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int buttonPin3 = 4;
const int buttonPin4 = 5;
//const int ledPin = 13;
// Variables will change:
// Menü
int Buchstabe = 0; // Gibt an welcher Buchstabe im Menü ausgewählt wurde
bool Eingabe = true;
//int ledState = HIGH; // the current state of the output pin
int buttonState1; // the current reading from the input pin
int buttonState2; // the current reading from the input pin
int buttonState3; // the current reading from the input pin
int buttonState4; // the current reading from the input pin
int lastbuttonState1 = LOW; // the previous reading from the input pin
int lastbuttonState2 = LOW; // the previous reading from the input pin
int lastbuttonState3 = LOW; // the previous reading from the input pin
int lastbuttonState4 = LOW; // the previous reading from the input pin
// Hier nimmt man long da in Millisekunden gerechnet wird
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime1 = 0; // the last time the output pin was toggled
unsigned long lastDebounceTime2 = 0; // the last time the output pin was toggled
unsigned long lastDebounceTime3 = 0; // the last time the output pin was toggled
unsigned long lastDebounceTime4 = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
Serial.begin(9600); // Für die Kommunikation mit dem PC per USB
Serial.println("Starte...");
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(buttonPin4, INPUT_PULLUP);
//pinMode(ledPin, OUTPUT);
// set initial LED state
// digitalWrite(ledPin, ledState);
}
void loop() {
// read the state of the switch into a local variable:
//Serial.println("Geben Sie mit Hilfe der Buttons den zu Fräsenden Text ein!");
Texteingabe();
}
void Texteingabe ()
{
while(Eingabe == true)
{
int reading1 = digitalRead(buttonPin1);
int reading2 = digitalRead(buttonPin2);
int reading3 = digitalRead(buttonPin3);
int reading4 = digitalRead(buttonPin4);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading1 != lastbuttonState1) {
// reset the debouncing timer
lastDebounceTime1 = millis();
}
if (reading2 != lastbuttonState2) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}
if (reading3 != lastbuttonState3) {
// reset the debouncing timer
lastDebounceTime3 = millis();
}
if (reading4 != lastbuttonState4) {
// reset the debouncing timer
lastDebounceTime4 = millis();
}
// Button 1
if ((millis() - lastDebounceTime1) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// Button wurde gedrückt
if (reading1 != buttonState1) {
buttonState1 = reading1;
// only toggle the LED if the new button state is HIGH
if (buttonState1 == LOW) {
//ledState = !ledState;
Buchstabe ++;
if (Buchstabe >= 26)
{
Buchstabe = 26;
}
Ausgabe();
}
}
}
// Button 2
if ((millis() - lastDebounceTime2) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// Button wurde gedrückt
if (reading2 != buttonState2) {
buttonState2 = reading2;
// only toggle the LED if the new button state is HIGH
if (buttonState2 == LOW) {
//ledState = !ledState;
Buchstabe --;
if (Buchstabe <= 0)
{
Buchstabe = 0;
}
Ausgabe();
}
}
}
// set the LED:
//digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastbuttonState1:
lastbuttonState1 = reading1;
lastbuttonState2 = reading2;
lastbuttonState3 = reading3;
lastbuttonState4 = reading4;
}
}
void Ausgabe()
{
switch (Buchstabe)
{
case 1:
Serial.println("A");
break;
case 2:
Serial.println("B");
break;
case 3:
Serial.println("C");
break;
case 4:
Serial.println("D");
break;
case 5:
Serial.println("E");
break;
case 6:
Serial.println("F");
break;
case 7:
Serial.println("G");
break;
case 8:
Serial.println("H");
break;
case 9:
Serial.println("I");
break;
case 10:
Serial.println("J");
break;
case 11:
Serial.println("K");
break;
case 12:
Serial.println("L");
break;
case 13:
Serial.println("M");
break;
case 14:
Serial.println("N");
break;
case 15:
Serial.println("O");
break;
case 16:
Serial.println("P");
break;
case 17:
Serial.println("Q");
break;
case 18:
Serial.println("R");
break;
case 19:
Serial.println("S");
break;
case 20:
Serial.println("T");
break;
case 21:
Serial.println("U");
break;
case 22:
Serial.println("V");
break;
case 23:
Serial.println("W");
break;
case 24:
Serial.println("X");
break;
case 25:
Serial.println("Y");
break;
case 26:
Serial.println("Z");
break;
}
}
Wie mache ich das jetzt mit den nächsten Zeichen?
Ist es sinnvoll das ganze in ein Array zu schreiben?