Hallo zusammen,
Nachdem ich ja schon stundenlang Bücher über C programmieren und Arduino Bücher gelesen habe, will ich jetzt mein erstes Praxisbezogenes Projekt erstellen, um gelesenes auch zu verwenden.
Das Projekt soll:
Per Matrixtastatur einen Sollwert an den Frequenzumrichter weitergeben (0-10V).
Am Display die Benutzereingabe, Sollwert, Istwert anzeigen.
Die Hardware funtioniert soweit.
Was mein grosses Problem ist, ich würde gerne zwecks eigenem Verständnis selber Funktionen erstellen.
Der erste Sketch funktioniert so wie ich will.
Das geht es um den abschnitt im Sketch "Wert der Matrixtastatur einlesen".
Im zweiten Sketch versuche ich es in eine Funktion zu verpacken was mir leider nicht gelingt.
Meiner Meinung nach:
- Ich habe keine Parameter zu übergeben
- Ich habe den Rückgabewert valueInt
Erster Sketch:
#include <LCD5110_Graph.h>
#include <Keypad.h>
#include <string.h>
#include <TimerOne.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, A4}; //connect to the column pinouts of the keypad
Keypad keyp = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
LCD5110 LCD(13,9,10,11,12);
extern unsigned char SmallFont[];
extern unsigned char TinyFont[];
int ZaehlerTaste;
char WertTaste[4];
static int WertRPM=0;
unsigned int counter=0;
unsigned int rotation=0;
void docount() // counts from the speed sensor
{
counter++; // increase +1 the counter value
}
void timerIsr()
{
Timer1.detachInterrupt(); //stop the timer
Serial.print("Motor Speed: ");
int rotation = (counter / 2); // divide by number of holes in Disc
Serial.print(rotation,DEC);
LCD.printNumI(rotation,RIGHT,16);
Serial.println(" Rotation per seconds");
counter=0; // reset counter to zero
Timer1.attachInterrupt( timerIsr ); //enable the timer
}
void setup(){
pinMode(A0,INPUT);
Serial.begin(9600);
LCD.InitLCD();
LCD.setFont(SmallFont);
Timer1.initialize(1000000); // set timer for 1sec
attachInterrupt(0, docount, RISING); // increase counter when speed sensor pin goes High
Timer1.attachInterrupt( timerIsr ); // enable the timer
}
void loop(){
LCD.print("Eingabe: ",LEFT,0);
LCD.print("SOLLWERT: ",LEFT,8);
LCD.print("ISTWERT: " ,LEFT,16);
LCD.update();
//Wert der Matrixtastatur einlesen
char keypressed= keyp.getKey();
if(keypressed != NO_KEY)
{
WertTaste[ZaehlerTaste]=keypressed;
ZaehlerTaste++;
int Wertint=atoi(WertTaste);
LCD.printNumI(Wertint,RIGHT,0); //Eingabewert an Display
LCD.update();
switch (keypressed)
{
case '*': //Löscht die Eingabe
Wertint=0;
memset(WertTaste, '\0', sizeof(WertTaste));
ZaehlerTaste=0;
LCD.clrScr();
LCD.printNumI(WertRPM,RIGHT,8);
LCD.update();
break;
case '#': //Übernimmt die Eingabe
LCD.clrScr();
WertRPM=Wertint;
LCD.printNumI(WertRPM,RIGHT,8);
Wertint=0;
memset(WertTaste, '\0', sizeof(WertTaste));
ZaehlerTaste=0;
LCD.update();
//Serial.print(WertRPM);
break;
}
}
}
Und hier im zweiten Sketch versuch ich die Eingabe der Tastatur in eine Funktion zu verpacken (keyInput)
Leider mach ich da was falsch, entweder bei der ersten Taste die ich drücke kommt irgendein Wert, oder das Display blinkt nur, und etliche seltsame Dinge. Ich hab schon die wildesten Konstellationen versucht,
komm aber nicht drauf was ich falsch mache.
#include <LCD5110_Graph.h>
#include <Keypad.h>
#include <string.h>
#include <TimerOne.h>
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, A4}; //connect to the column pinouts of the keypad
Keypad keyp = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
LCD5110 LCD(13,9,10,11,12);
extern unsigned char SmallFont[];
extern unsigned char TinyFont[];
int ZaehlerTaste =0;
int Wertint =0;
int valueInt =0;
char WertTaste[4];
static int WertRPM =0;
unsigned int counter =0;
unsigned int rotation =0;
//Funktionen
void docount() // counts from the speed sensor
{
counter++; // increase +1 the counter value
}
void timerIsr() //Drehzahlmesser
{
Timer1.detachInterrupt(); //stop the timer
Serial.print("Motor Speed: ");
int rotation = (counter / 2); // divide by number of holes in Disc
Serial.print(rotation,DEC);
LCD.printNumI(rotation,RIGHT,16);
Serial.println(" Rotation per seconds");
counter=0; // reset counter to zero
Timer1.attachInterrupt( timerIsr ); //enable the timer
}
int keyInput () // Funktion sollte den Wert der Matrixtastatur als INT übergeben
{
char keypressed=keyp.getKey();
WertTaste[ZaehlerTaste]=keypressed;
ZaehlerTaste++;
valueInt=atoi(WertTaste);
return valueInt;
}
void setup(){
pinMode(A0,INPUT);
Serial.begin(9600);
LCD.InitLCD();
LCD.setFont(SmallFont);
Timer1.initialize(1000000); // set timer for 1sec
attachInterrupt(0, docount, RISING); // increase counter when speed sensor pin goes High
Timer1.attachInterrupt( timerIsr ); // enable the timer
}
void loop(){
LCD.print("Eingabe: ",LEFT,0);
LCD.print("SOLLWERT: ",LEFT,8);
LCD.print("ISTWERT: " ,LEFT,16);
LCD.update();
char keypressed= keyp.getKey();
if(keypressed != NO_KEY)
{
keyInput(); //Aufruf der Funktion
Wertint=valueInt;
LCD.printNumI(Wertint,RIGHT,0); //Ausgabe am Display
LCD.update();
switch (keypressed)
{
case '*': //Löscht die Eingabe
Wertint=0;
memset(WertTaste, '\0', sizeof(WertTaste));
ZaehlerTaste=0;
LCD.clrScr();
LCD.printNumI(WertRPM,RIGHT,8);
LCD.update();
break;
case '#': //Übernimmt die Eingabe
LCD.clrScr();
WertRPM=Wertint;
LCD.printNumI(WertRPM,RIGHT,8);
Wertint=0;
memset(WertTaste, '\0', sizeof(WertTaste));
ZaehlerTaste=0;
LCD.update();
//Serial.print(WertRPM);
break;
}
}
}
Kann mir da wer helfen bitte, ist sicher eine Kleinigkeit für einen erfahrenen Arduinonutzer.
Gruss
Chubacca