I2C send | send not + Serial Monitor stops the code

Hi,

I have a big problem with my Robot project. I use 3 Mega 2560 and 2 Uno, which communicate via I2C.

My so called “S-Board” is the Master and gathers information about the used Sensors and also saves data on a SD-Cardshield with a 8GB micro SD. It sends commands to the M-Board (Motor Board //Uno), the C-Board (camera Board//Uno), the E-Board (Encoder Board//Mega//gathers steps via interupts), and the HG-Board (controls the motors to grab objects//Mega). It was a running System, which worked (more or less) very well. (only normal problems //Light and the Cam//grab when to far away, but nothing I can´t handle)

The problem now (after the use of the E-Board) is that I send a command to the E-Board, which tells them to start countnig (this works//I see it in the serial monitor on the E-Board), but then if the track ends, the E-Board doesn´t get the command to stop.

The codes are atteched.

Hope you guys have an idea on what´s happening here and let me know.

S_Board.ino (13.5 KB)

E_Board.ino (3.92 KB)

Also I have a problem that when the robot drives and I start the serial monitor to see, which commands were comming in, it stops driving and does not start again. I have to unplug the PC first and turn the power of the robot off, to let him drive again.

I didn´t find any information except of the fact that pin 0 and 1 has something to do with the Serial Port. But I don’t use them as you can see in the sketch below…

// Programm zum Ansteuern der Motoren

#ifndef TWI_FREQ
#define TWI_FREQ 400000L
#endif

#include <Wire.h>
byte info = 0;
const int step_rechts = 9;			//6
const int step_links = 6;			//3

const int en_rechts = 10;			//7
const int en_links = 7;				//4

const int dir_rechts = 8;			//5
const int dir_links = 5;			//2

// Achtung Groß und Kleinschreibung
int u = 2000;
int t = 800;
int v = 2000;
int w = 2000;
int r = 2000;
int o = 800;
int p = 1000;
int q = 1000;
int z = 900;

boolean zeitdifferenz_merken = false;

unsigned long zeitalt_links=0, zeitalt_rechts=0;
long zeit_links=0, zeit_rechts=0, diff_links=0, diff_rechts=0;

int Kp = 100;  //Verstärkungsfaktor des P-Reglers

void setup ()
{
  Wire.begin(4);
  Wire.onReceive(empfangen);
  pinMode (step_rechts,OUTPUT);
  pinMode (step_links,OUTPUT);
  pinMode (en_rechts,OUTPUT);
  pinMode (en_links,OUTPUT);
  pinMode (dir_rechts,OUTPUT);
  pinMode (dir_links,OUTPUT);
}

void empfangen(int Data)
{
  info = Wire.read();
}

void loop()
{
	Serial.println(info);
  if (info != 0)
  {
    digitalWrite (en_rechts,LOW);
    digitalWrite (en_links,LOW);
  }
  
  digitalWrite (step_links, LOW);
  digitalWrite (step_rechts, LOW);


/*------- 0 - Motoren Abschalten (Drehen der Welle möglich) ------------*/
  if (info == 0)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
    r = 2000;
    digitalWrite (en_rechts,HIGH);
    digitalWrite (en_links,HIGH);
    zeitdifferenz_merken = false;
  }

/*------- 1 - anhalten bei sofortigem Stop -----------------------------*/
  if (info == 1)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
    r = 2000;
    digitalWrite (step_rechts,LOW);
    digitalWrite (step_links,LOW);
    zeitdifferenz_merken = false;
  }

/*------- 2 - Geradeausfahrt -------------------------------------------*/
  if (info == 2)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
    digitalWrite (dir_rechts,LOW);
    digitalWrite (dir_links,LOW);
    
    schritte (o);
    zeitdifferenz_merken = false;
  }

/*------- 3 - Geradeausfahrt langsam -----------------------------------*/
  if (info == 3)
  {
    digitalWrite (dir_rechts,LOW);
    digitalWrite (dir_links,LOW);
    
    schritte (6000);
    zeitdifferenz_merken = false;
  }

/*------- 4 - Rechtsdrehung um 90°--------------------------------------*/
  if (info == 4)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
    digitalWrite (dir_rechts,HIGH);
    digitalWrite (dir_links,LOW);
    
    for (long i=0; i<=1850; i++) //2250
    {
      schritte (q);
    }
    
    info = 0;
    zeitdifferenz_merken = false;
  }

/*------- 5 - Linksdrehung um 90°---------------------------------------*/
  if (info == 5)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
    digitalWrite (dir_rechts,LOW);
    digitalWrite (dir_links,HIGH);
    
    for (long i=0; i<=1850; i++) //2250
    {
      schritte (p);
    }
    
    info = 0;
    zeitdifferenz_merken = false;
  }

/*------- 6 - nach Objekt ausrichten (10° drehen - links)---------------*/
  if (info == 6)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
    digitalWrite (dir_rechts,LOW);
    digitalWrite (dir_links,HIGH);
    
    for (long i=0; i<=222; i++) //250
    {
      schritte (p);
    }
    
    info = 0;
    zeitdifferenz_merken = false;
  }

/*------- 7 - nach Objekt ausrichten (10° drehen - rechts)---------------*/
  if (info == 7)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
    digitalWrite (dir_rechts,HIGH);
    digitalWrite (dir_links,LOW);
    
    for (long i=0; i<=222; i++) //250
    {
      schritte (p);
    }
    
    info = 0;
    zeitdifferenz_merken = false;
  }

/*------- 8 - Rückwärtsfahrt -------------------------------------------*/
  if (info == 8)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
    digitalWrite (dir_rechts,HIGH);
    digitalWrite (dir_links,HIGH);
    
    schritte (z);
    zeitdifferenz_merken = false;
  }

/*------- 9 - Kurvenfahrt (links)---------------------------------------*/
  if (info == 9)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
	digitalWrite (step_rechts,HIGH);
	digitalWrite (step_links,LOW);
  }
  
/*------- 10 - Kurvenfahrt (rechts)-------------------------------------*/
  if (info == 10)
  {
    u = 2000;
    t = 800;
    v = 2000;
    w = 2000;
	digitalWrite (step_links,HIGH);
	digitalWrite (step_rechts,LOW);
  }
  
}

void schritte (int a)
{
  digitalWrite (step_rechts,HIGH);
  digitalWrite (step_links,HIGH);
  delayMicroseconds (1);
  digitalWrite (step_rechts,LOW);
  digitalWrite (step_links,LOW);
  delayMicroseconds (a);
  if (info == 0) return;
  if (info == 1) return;
}