Serial fehlerhafte übertragung

Hallo.

habe ein kleines problem

bei meinem projekt habe ich ein arduino uno und ein atmega328 auf einem steckbrett.
diese sollen über serial miteinander kommunizieren.

das habe ich wie folgt versucht zu realisiren.

Arduino empfängt

 if(Serial.available()){
    Serial.readBytes(inByte,1);
    if(inByte[0]== 0b10000000){Gruppe=7;}
    if(inByte[0]== 0b01000000){Gruppe=6;}
    if(inByte[0]== 0b00100000){Gruppe=5;}
    if(inByte[0]== 0b00010000){Gruppe=4;}

Arduino sendet

    if(Reset == HIGH && Lock == 0){
      Lock = 1; 
      outByte = 0b11110000;
      Serial.print(outByte);
     }

atmega328 empfängt

  if(Serial.available()){
    Serial.readBytes(inByte,1);
    LedRo=bitRead(inByte[0],7);
    LedGr=bitRead(inByte[0],6);
    LedBl=bitRead(inByte[0],5);
    LedGe=bitRead(inByte[0],4);
    Lock=0;
    pwmR=255, pwmG=255, pwmB=255;
  }

atmega328 sendet

if(Ge == HIGH && Lock == 0){
    LedGe = HIGH,
    LedGr = LOW,
    LedBl = LOW,
    LedRo = LOW,
    Lock = 1,
    Serial.print(0b00010000),
    pwmR=255,
    pwmG=60,
    pwmB=0;
  }

wenn ich die bytes per serial monitor sende läuft alles super und ich empfange auch die richtigen bytes

wenn ich die beiden allerdings unter sich arbeiten lasse und sende vom arduino

outByte = 0b11110000;
      Serial.print(outByte);

an den atmega328 dann habe ich das problem das z.b. bei

   LedRo=bitRead(inByte[0],7);
    LedGr=bitRead(inByte[0],6);
    LedBl=bitRead(inByte[0],5);
    LedGe=bitRead(inByte[0],4);

nur LedBl und LedGe auf 1 gesetzt werden und die anderen beiden bleiben 0.

auf die daten vom atmega regiert der arduino garnicht.

wie gesagt wenn ich die befehle per serial monitor seden und empfange geht alles.

beide laufen mit 9600 baud

Setze deinen Sketch in Code-Tags </>.
Dann ist dieser besser lesbar.

ok danke den hab ich total übersehen

Und gib uns die gesamten Sketche und nicht nur Auszüge.
Uwe

Arduino

//Pin Deklaration
const int I_Richtig=2;
const int I_Falsch=3;
const int I_Reset=4;
const int I_Hoch=5;
const int I_Runter=6;
const int I_Links=7;
const int I_Rechts=8;
const int I_Ok=9;

//Serielle Komunikation
byte inByte[1];
byte outByte = 0b11110000;

//Variablen
int Lock;
int Gruppe;
int Minuspunkte;

int Richtig;
int Falsch;
int Reset;
int Hoch;
int Runter;
int Links;
int Rechts;
int Ok;

void setup() {
  Serial.begin(9600);
  pinMode(I_Richtig, INPUT);
  pinMode(I_Falsch, INPUT);
  pinMode(I_Reset, INPUT);
  pinMode(I_Hoch, INPUT);
  pinMode(I_Runter, INPUT);
  pinMode(I_Links, INPUT);
  pinMode(I_Rechts, INPUT);
  pinMode(I_Ok, INPUT);
  Serial.println(0b10000000);
  Serial.println(0b01000000);
  Serial.println(0b00100000);
  Serial.println(0b00010000);
}

void loop() {
  Richtig = digitalRead(I_Richtig);
  Falsch = digitalRead(I_Falsch);
  Reset = digitalRead(I_Reset);
  Hoch = digitalRead(I_Hoch);
  Runter = digitalRead(I_Runter);
  Links = digitalRead(I_Links);
  Rechts = digitalRead(I_Rechts);
  Ok = digitalRead(I_Ok);
  
  if(Serial.available()){
    Serial.readBytes(inByte,1);
    if(inByte[0]== 0b10000000){Gruppe=7;}
    if(inByte[0]== 0b01000000){Gruppe=6;}
    if(inByte[0]== 0b00100000){Gruppe=5;}
    if(inByte[0]== 0b00010000){Gruppe=4;}
  }
  if(Richtig==LOW &&
     Falsch==LOW &&
     Reset==LOW &&
     Hoch==LOW &&
     Runter==LOW &&
     Links==LOW &&
     Rechts==LOW &&
     Ok==LOW){Lock = 0;}
     
     if(Reset == HIGH && Lock == 0){
      Lock = 1; 
      outByte = 0b11110000;
      Serial.print(outByte);
     }
     
  if(Gruppe == 7 && Richtig == HIGH && Lock == 0){
    Lock = 1;
    //Punkte
    outByte = 0b11110000;
    Serial.print(outByte);
    
  }
  if(Gruppe == 7 && Falsch == HIGH && Lock == 0){
    Lock = 1; 
    if(Minuspunkte == 1){/*Minuspunkte*/;}
    bitWrite(outByte,7,0);
    Serial.print(outByte);
  }

  if(Gruppe == 6 && Richtig == HIGH && Lock == 0){
    Lock = 1;
    //Punkte
    outByte = 0b11110000;
    Serial.print(outByte);
    
  }
  if(Gruppe == 6 && Falsch == HIGH && Lock == 0){
    Lock = 1; 
    if(Minuspunkte == 1){/*Minuspunkte*/;}
    bitWrite(outByte,6,0);
    Serial.print(outByte);
  }

  if(Gruppe == 5 && Richtig == HIGH && Lock == 0){
    Lock = 1;
    //Punkte
    outByte = 0b11110000;
    Serial.print(outByte);
    
  }
  if(Gruppe == 5 && Falsch == HIGH && Lock == 0){
    Lock = 1; 
    if(Minuspunkte == 1){/*Minuspunkte*/;}
    bitWrite(outByte,5,0);
    Serial.print(outByte);
  }

  if(Gruppe == 4 && Richtig == HIGH && Lock == 0){
    Lock = 1;
    //Punkte
    outByte = 0b11110000;
    Serial.print(outByte);
    
  }
  if(Gruppe == 4 && Falsch == HIGH && Lock == 0){
    Lock = 1; 
    if(Minuspunkte == 1){/*Minuspunkte*/;}
    bitWrite(outByte,4,0);
    Serial.print(outByte);
  }
}

atmega

//Pin Deklaration
const int O_Ro = 2;
const int O_Gr = 3;
const int O_Bl = 4;
const int O_Ge = 5;
const int I_Ro = A0;
const int I_Gr = A1;
const int I_Bl = A2;
const int I_Ge = A3;
const int FarbeR = 9;
const int FarbeG = 10;
const int FarbeB = 11;

//Variablen
int Lock;
int Ro;
int Gr;
int Bl;
int Ge;
int LedRo;
int LedGr;
int LedBl;
int LedGe;
int pwmR = 255;
int pwmG = 255;
int pwmB = 255;

//Serielle Komunikation
byte inByte[1];

void setup() {
  Serial.begin(9600);
  pinMode(O_Ro, OUTPUT);
  pinMode(O_Gr, OUTPUT);
  pinMode(O_Bl, OUTPUT);
  pinMode(O_Ge, OUTPUT);
  pinMode(I_Ro, INPUT);
  pinMode(I_Gr, INPUT);
  pinMode(I_Bl, INPUT);
  pinMode(I_Ge, INPUT);
  pinMode(FarbeR, OUTPUT);
  pinMode(FarbeG, OUTPUT);
  pinMode(FarbeB, OUTPUT);
}

void loop() {
  Ro = digitalRead(I_Ro);
  Gr = digitalRead(I_Gr);
  Bl = digitalRead(I_Bl);
  Ge = digitalRead(I_Ge);
  digitalWrite(O_Ro, LedRo);
  digitalWrite(O_Gr, LedGr);
  digitalWrite(O_Bl, LedBl);
  digitalWrite(O_Ge, LedGe);
  analogWrite(FarbeR, pwmR);
  analogWrite(FarbeG, pwmG);
  analogWrite(FarbeB, pwmB);
  if(Ro == HIGH && Lock == 0){
    LedRo = HIGH, LedGr = LOW, LedBl = LOW, LedGe = LOW, Lock = 1, Serial.print(0b10000000), pwmR=255, pwmG=0, pwmB=0;}
  if(Gr == HIGH && Lock == 0){
    LedGr = HIGH, LedRo = LOW, LedBl = LOW, LedGe = LOW, Lock = 1, Serial.print(0b01000000), pwmR=0, pwmG=255, pwmB=0;}
  if(Bl == HIGH && Lock == 0){
    LedBl = HIGH, LedGr = LOW, LedRo = LOW, LedGe = LOW, Lock = 1, Serial.print(0b00100000), pwmR=0, pwmG=0, pwmB=255;}
  if(Ge == HIGH && Lock == 0){
    LedGe = HIGH, LedGr = LOW, LedBl = LOW, LedRo = LOW, Lock = 1, Serial.print(0b00010000), pwmR=255, pwmG=60, pwmB=0;}
  if(Serial.available()){
    Serial.readBytes(inByte,1);
    LedRo=bitRead(inByte[0],7);
    LedGr=bitRead(inByte[0],6);
    LedBl=bitRead(inByte[0],5);
    LedGe=bitRead(inByte[0],4);
    Lock=0;
    pwmR=255, pwmG=255, pwmB=255;
  }
}

Wieso verwendest du ein Array wenn du nur ein Byte überträgst? Einfach Serial.read() machen um ein Byte einzulesen.

Für Binär-Daten nimmt man normal write() statt print().

Das hier ist übrigens nicht sauber:

 LedRo = HIGH, LedGr = LOW, LedBl = LOW, LedGe = LOW, Lock = 1, Serial.print(0b10000000), pwmR=255, pwmG=0, pwmB=0;}

Der Komma-Operator ist ganz gemein. Der führt den ersten Ausdruck aus und gibt den zweiten zurück. Die bekannteste Verwendung davon sind mehrere Anweisungen in einer for-Schleife. Aber ansonsten hat er wenig Nutzen und kann zu schlecht erkennbaren Fehlern führen.
Anweisungen werden normalerweise mit einem Strichpunkt abgeschlossen!

Sollte hier aber gehen, da die Anweisungen trotzdem ausgeführt werden.

wollte mit dem array noch platz für mehr offen lassen ist ja noch nicht fertig.

ich versuchs mal mit write und die die kommas weg.

danke schonmal

Noch ne Kleinigkeit:

  Serial.println(0b10000000);
  Serial.println(0b01000000);
  Serial.println(0b00100000);
  Serial.println(0b00010000);

in setup(). println() sendet ein CR/LF hintendran. Das ist glaube ich nicht was du möchtest

ja das war testweise für den serial monitor drinn das ich die möglichen daten zu beginn schonmal bekomme.

aber vielen dank der fehler ist behoben

:grin: :grin: :sunglasses:

byte inByte[1];

Würde das Array etwas größer machen.

Grüße Uwe