Nachdem mein Problem mit den return bei en Funktionen relativ leicht zu lösen war, dachte ich "Jetzt gehts vorwärts"....scheiße wars.
ich habe diesen Code.
#include <Adafruit_MCP23X17.h>
#include <SoftwareSerial.h>
#define encoderPinA A1 // CLK pin of Rotary Enocoder
#define encoderPinB A2 // DT pin of Rotary Enocoder
#define TX 3
#define RX 2
int encoderPinALast = LOW;
int encoderPinANow = LOW;
SoftwareSerial mySerial (RX,TX);
Adafruit_MCP23X17 mcp;
byte MCPADR=0x20;
byte PoM=0;
byte PoMt=0;
byte val=0;
bool Send=false;
bool ButtonCall=false;
byte IDB=0;
byte IDC=0;
byte Moist=0;
byte MoistMin=0;
byte MoistMax=0;
byte Hum=0;
byte HumMin=0;
byte HumMax=0;
void setup() {
pinMode (RX,INPUT);
pinMode (TX,OUTPUT);
pinMode (encoderPinA,INPUT_PULLUP);
pinMode (encoderPinB,INPUT_PULLUP);
mySerial.begin(9600);
Serial.begin (9600);
Serial.println("PROGRAMMER TEST");
if (!mcp.begin_I2C()) {
Serial.println("Error.");
while (1);{ Serial.print("Hänge Fest");
}
}
for(int i=0;i<16;i++){
Serial.println("Before mcp.pinmode");
mcp.pinMode(i, OUTPUT);
Serial.print("PinMode OUTPUT FOR PIN"); Serial.println(i);
}
}
void loop() {
switch(PoM){
case 0:{
if(PoMt<1){PoMt=9;}
if(PoMt>9){PoMt=1;}
PoMt=+encode(PoMt);
MenuLED(PoMt);
Serial.print("MENU== ");Serial.println(PoM);
Serial.print("MENUt= ");Serial.println(PoMt);
Serial.print("BytesReady: ");Serial.println(mySerial.available());
Serial.print("READ: ");Serial.println(mySerial.read());
Serial.print("ButtonState: ");Serial.println(ButtonDown());
if(ButtonDown()){
mySerial.write(PoM);
PoM=PoMt;
}//IF ButtonDown
}//case0
;break;
case 1:{
if(val<0){val=7;}
if(val>7){val=0;}
if(mySerial.available()) {
Serial.println("Have Some Data for IDB");
IDB=mySerial.read();
SevenSegment(IDB);
val=IDB;}
if(ButtonDown());{
}
val=+encode(val);
Serial.print("IDB ");Serial.println(val);
SevenSegment(val);
}//PoM==1
;break;
case 2:{
while(ButtonDown){}
if(mySerial.available()) {
Serial.println("Have Some Data for IDC");
IDC=mySerial.read();
Serial.println(IDC);
SevenSegment(IDC); }
if(ButtonDown());{
}
}//PoM==1
;break;
}//Switch
}//LOOP
int MenuLED(int i){
switch(i){
case 0:digitalWrite(9,LOW);digitalWrite(10,LOW);digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,LOW);break;
case 1:digitalWrite(9,HIGH);digitalWrite(10,LOW);digitalWrite(11,LOW);digitalWrite(12,HIGH);digitalWrite(13,LOW);break;
case 2:digitalWrite(9,HIGH);digitalWrite(10,LOW);digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,HIGH);break;
case 3:digitalWrite(9,LOW);digitalWrite(10,HIGH);digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,LOW);break;
case 4:digitalWrite(9,LOW);digitalWrite(10,HIGH);digitalWrite(11,LOW);digitalWrite(12,HIGH);digitalWrite(13,LOW);break;
case 5:digitalWrite(9,LOW);digitalWrite(10,HIGH);digitalWrite(11,LOW);digitalWrite(12,LOW);digitalWrite(13,HIGH);break;
case 6:digitalWrite(9,LOW);digitalWrite(10,LOW);digitalWrite(11,HIGH);digitalWrite(12,LOW);digitalWrite(13,LOW);break;
case 7:digitalWrite(9,LOW);digitalWrite(10,LOW);digitalWrite(11,HIGH);digitalWrite(12,HIGH);digitalWrite(13,LOW);break;
case 8:digitalWrite(9,LOW);digitalWrite(10,LOW);digitalWrite(11,HIGH);digitalWrite(12,LOW);digitalWrite(13,HIGH);break;
}
}//MENULED
int SevenSegment(int i){
int a;
int b;
if(i<10){a=0;b=i;}
else if(i<20){a=1;b=i-10;}
else if(i<30){a=2;b=i-20;}
else if(i<40){a=3;b=i-30;}
else if(i<50){a=4;b=i-40;}
else if(i<60){a=5;b=i-50;}
else if(i<70){a=6;b=i-60;}
else if(i<80){a=7;b=i-70;}
else if(i<90){a=8;b=i-80;}
else if(i<100){a=9;b=i-90;}
switch(b){
case 0:mcp.digitalWrite(1,0);mcp.digitalWrite(2,0);mcp.digitalWrite(3,0);mcp.digitalWrite(4,0);mcp.digitalWrite(5,0);mcp.digitalWrite(6,0);mcp.digitalWrite(7,1);break;
case 1:mcp.digitalWrite(1,1);mcp.digitalWrite(2,1);mcp.digitalWrite(3,1);mcp.digitalWrite(4,0);mcp.digitalWrite(5,0);mcp.digitalWrite(6,1);mcp.digitalWrite(7,1);break;
case 2:mcp.digitalWrite(1,1);mcp.digitalWrite(2,0);mcp.digitalWrite(3,0);mcp.digitalWrite(4,1);mcp.digitalWrite(5,0);mcp.digitalWrite(6,0);mcp.digitalWrite(7,0);break;
case 3:mcp.digitalWrite(1,1);mcp.digitalWrite(2,1);mcp.digitalWrite(3,0);mcp.digitalWrite(4,0);mcp.digitalWrite(5,0);mcp.digitalWrite(6,0);mcp.digitalWrite(7,0);break;
case 4:mcp.digitalWrite(1,0);mcp.digitalWrite(2,1);mcp.digitalWrite(3,1);mcp.digitalWrite(4,0);mcp.digitalWrite(5,0);mcp.digitalWrite(6,1);mcp.digitalWrite(7,0);break;
case 5:mcp.digitalWrite(1,0);mcp.digitalWrite(2,1);mcp.digitalWrite(3,0);mcp.digitalWrite(4,0);mcp.digitalWrite(5,1);mcp.digitalWrite(6,0);mcp.digitalWrite(7,0);break;
case 6:mcp.digitalWrite(1,1);mcp.digitalWrite(2,0);mcp.digitalWrite(3,0);mcp.digitalWrite(4,0);mcp.digitalWrite(5,0);mcp.digitalWrite(6,0);mcp.digitalWrite(7,0);break;
case 7:mcp.digitalWrite(1,1);mcp.digitalWrite(2,1);mcp.digitalWrite(3,1);mcp.digitalWrite(4,0);mcp.digitalWrite(5,0);mcp.digitalWrite(6,0);mcp.digitalWrite(7,1);break;
case 8:mcp.digitalWrite(1,0);mcp.digitalWrite(2,0);mcp.digitalWrite(3,0);mcp.digitalWrite(4,0);mcp.digitalWrite(5,0);mcp.digitalWrite(6,0);mcp.digitalWrite(7,0);break;
case 9:mcp.digitalWrite(1,0);mcp.digitalWrite(2,1);mcp.digitalWrite(3,0);mcp.digitalWrite(4,0);mcp.digitalWrite(5,0);mcp.digitalWrite(6,0);mcp.digitalWrite(7,0);break;
}
switch(a){
case 0:mcp.digitalWrite(8,0);mcp.digitalWrite(9,0);mcp.digitalWrite(10,0);mcp.digitalWrite(11,0);mcp.digitalWrite(12,0);mcp.digitalWrite(13,0);mcp.digitalWrite(14,1);break;
case 1:mcp.digitalWrite(8,1);mcp.digitalWrite(9,1);mcp.digitalWrite(10,1);mcp.digitalWrite(11,0);mcp.digitalWrite(12,0);mcp.digitalWrite(13,1);mcp.digitalWrite(14,1);break;
case 2:mcp.digitalWrite(8,1);mcp.digitalWrite(9,0);mcp.digitalWrite(10,0);mcp.digitalWrite(11,1);mcp.digitalWrite(12,0);mcp.digitalWrite(13,0);mcp.digitalWrite(14,0);break;
case 3:mcp.digitalWrite(8,1);mcp.digitalWrite(9,1);mcp.digitalWrite(10,0);mcp.digitalWrite(11,0);mcp.digitalWrite(12,0);mcp.digitalWrite(13,0);mcp.digitalWrite(14,0);break;
case 4:mcp.digitalWrite(8,0);mcp.digitalWrite(9,1);mcp.digitalWrite(10,1);mcp.digitalWrite(11,0);mcp.digitalWrite(12,0);mcp.digitalWrite(13,1);mcp.digitalWrite(14,0);break;
case 5:mcp.digitalWrite(8,0);mcp.digitalWrite(9,1);mcp.digitalWrite(10,0);mcp.digitalWrite(11,0);mcp.digitalWrite(12,1);mcp.digitalWrite(13,0);mcp.digitalWrite(14,0);break;
case 6:mcp.digitalWrite(8,1);mcp.digitalWrite(9,0);mcp.digitalWrite(10,0);mcp.digitalWrite(11,0);mcp.digitalWrite(12,0);mcp.digitalWrite(13,0);mcp.digitalWrite(14,0);break;
case 7:mcp.digitalWrite(8,1);mcp.digitalWrite(9,1);mcp.digitalWrite(10,1);mcp.digitalWrite(11,0);mcp.digitalWrite(12,0);mcp.digitalWrite(13,0);mcp.digitalWrite(14,1);break;
case 8:mcp.digitalWrite(8,0);mcp.digitalWrite(9,0);mcp.digitalWrite(10,0);mcp.digitalWrite(11,0);mcp.digitalWrite(12,0);mcp.digitalWrite(13,0);mcp.digitalWrite(14,0);break;
case 9:mcp.digitalWrite(8,0);mcp.digitalWrite(9,1);mcp.digitalWrite(10,0);mcp.digitalWrite(11,0);mcp.digitalWrite(12,0);mcp.digitalWrite(13,0);mcp.digitalWrite(14,0);break;
}
}//SevenSegment
bool ButtonDown(){
bool val=false;
if(digitalRead(A0)==1){
Serial.println("ButtonDown");
val=true;
while(digitalRead(A0)){}
}
return val;
}//VOID BUTTONDOWN
int encode(int encoderPos){
encoderPinANow = digitalRead(encoderPinA);
if ((encoderPinALast == HIGH) && (encoderPinANow == LOW)) {
if (digitalRead(encoderPinB) == HIGH) {
encoderPos++;
} else {
encoderPos--;
}
}
encoderPinALast = encoderPinANow;
return(encoderPos);
}//ENCODE
Dieser soll ein Menü steuern
im case 0 soll er sich einen Menüpunkt aussuchen (PoMt(PointOfMenuTemporary) und mit der Taste soll der Menüpunkt gesetzt werden (PoM(PointOfMenu)
das wäre dieser Part
case 0:{
if(PoMt<1){PoMt=9;}
if(PoMt>9){PoMt=1;}
PoMt=+encode(PoMt);
MenuLED(PoMt);
Serial.print("MENU== ");Serial.println(PoM);
Serial.print("MENUt= ");Serial.println(PoMt);
Serial.print("BytesReady: ");Serial.println(mySerial.available());
Serial.print("READ: ");Serial.println(mySerial.read());
Serial.print("ButtonState: ");Serial.println(ButtonDown());
if(ButtonDown()){
mySerial.write(PoM);
PoM=PoMt;
}//IF ButtonDown
}//case0
;break;
nun habe ich das Problem das aus irgend einem Grund der Switch nach dem drücken der Taste im Case0 verbleibt und mir folgendes ausgibt
20:59:49.631 -> ButtonState: ButtonDown
20:59:49.664 -> 1
20:59:49.664 -> MENU== 0
20:59:49.664 -> MENUt= 1
20:59:49.664 -> BytesReady: 0
20:59:49.696 -> READ: -1
und das obwohl da ja eig steht
if(ButtonDown()){
mySerial.write(PoM);
PoM=PoMt;
}//IF ButtonDown