Problem : the code script is executed like just 8 times and stop !! help !!

hello guys,
i wrote a simple script to see what the problem might be
first i have a serial communication between 2 arduinos : 2 buttons are connected to the first and the second is controlling a 1 digit display, the display is showing numbers from 0 to 6 (0 is the default value)
so when i click the first button, numbers goes from 0 to 6 as long as i keep clicking (+1 every click)
and the when i click the second button is decrease back to 0
the problem is that when i go up and down 8 times, everything stops and i am not able to do it again, untill i re upload the scripts to both arduinos so what is the problem here ?

here is the script of the first arduino which has buttons connected :

//Arduino number 1

int downShift = 2; //Left paddle shift pin 2
int upShift = 3; //Right paddle shift pin 3
int burnout = 5; //Burnout Button pin 5
int neutral = 6; //Neutral Green Button pin 6
int reverse = 7; //Reverse Button pin 7
int limiter = 8; //Pit Limiter Button pin 8
int cam = 9; //Cam grey Button pin 9

//pin 4 is the white button

//downShift
boolean lastButton1 = LOW;
boolean currentButton1 = LOW;

//upShift
boolean lastButton2 = LOW;
boolean currentButton2 = LOW;

//burnout
boolean lastButton4 = LOW;
boolean currentButton4 = LOW;

//neutral
boolean lastButton5 = LOW;
boolean currentButton5 = LOW;

//reverse
boolean lastButton6 = LOW;
boolean currentButton6 = LOW;

//limiter
boolean lastButton7 = LOW;
boolean currentButton7 = LOW;

//cam
boolean lastButton8 = LOW;
boolean currentButton8 = LOW;

void setup(){
Serial.begin(9600);
for(int i = 2; i<9; i++){
pinMode(i, INPUT);
}

}

//debounce function
boolean debounce(boolean last, int button)
{
boolean current = digitalRead(button);
if(last != current)
{
delay(5);
current = digitalRead(button);
}
return current;
}

void loop(){
//for the downShift
currentButton1 = debounce(lastButton1, downShift);
if(lastButton1 == LOW && currentButton1 == HIGH)
{
Serial.write("-");
}
lastButton1 = currentButton1;

//for the upShift
currentButton2 = debounce(lastButton2, upShift);
if(lastButton2 == LOW && currentButton2 == HIGH)
{
Serial.write("+");
}
lastButton2 = currentButton2;

}

this is the second script for the second arduino which has the 7 segment display connected :

//For the 2 digits displays////////////////////////////////////////////////////////////////////
byte datapin = 2;
byte clockpin = 3;
byte latchpin = 4;

byte datapin2 = 7;
byte clockpin2 = 8;
byte latchpin2 = 9;

byte First = 5;
byte Second = 6;
byte Third = 10;
byte Fourth = 11;

int times;
byte segments[14] = {0b11111111,  //Blank - 0
                     0b00101011, //n - 1
                     0b10000110, //E - 2
                     0b00000111, //t - 3
                     0b10101111, //r - 4
                     0b11001111, //l - 5
                     0b10000011, //b - 6
                     0b11000000, //O - 7
                     0b11100011, //u - 8
                     0b11010101, //v - 9
                     0b11000111, //L - 10
                     0b11001111, //i - 11
                     0b11101010,  //M - 12
                     0b10001001
                     
                   };
                     

byte FirstDigit;
byte SecondDigit;
byte ThirdDigit;
byte FourthDigit;

byte FirstD;
byte SecondD;
byte ThirdD;
byte FourthD;

//these pins for the gear number display///////////////////////////////////////////////////////
byte latchPin0 = 15; //latch pin
byte clockPin0 = 16; //clock pin
byte dataPin0 = 14; //data pin

byte gearDisplay = 0;

int gearBox = 0;

byte segments1Digit[7] = {0b00000011, //0 - position 1
                          0b10011111, //1 - position 2
                          0b00100101, //2 - position 3
                          0b00001101, //3 - position 4
                          0b10011001, //4 - position 5
                          0b01001001, //5 - position 6
                          0b01000001, //6 - position 7
                         };
                   
boolean burnoutOn = false;
boolean neutralOn = false;
boolean reverseOn = false;
boolean limiterOn = false;
boolean camOn = false;

void setup()
{
  Serial.begin(9600);
  
  for(int x = 2; x < 20; x++){
    pinMode(x, OUTPUT);
  }
  
  digitalWrite(Second,HIGH);
  digitalWrite(First,HIGH);
  digitalWrite(datapin,LOW);
  digitalWrite(latchpin,LOW);
  digitalWrite(clockpin,LOW);
  
  digitalWrite(Fourth,HIGH);
  digitalWrite(Third,HIGH);
  digitalWrite(datapin2,LOW);
  digitalWrite(latchpin2,LOW);
  digitalWrite(clockpin2,LOW);
}

void loop()
{  
  //Puting Limits for the gear box up and down
  if(gearBox > 6){
      gearBox = 6;
    }else if(gearBox < 0){
      gearBox = 0;
    }  
  
  //Read from Serial port
  if(Serial.available() > 0){
    int value = Serial.read();
    if(value == '+'){
     
      gearBox++;
    
    }
    if(value == '-'){
      
      gearBox--;
    
    }
 
   
    
  }  
    
    
  gearDisplay = gearBox;
  updateShiftRegister();
  
  


}

void updateShiftRegister()
{
  
  shiftOut(dataPin0, clockPin0, LSBFIRST, segments1Digit[gearDisplay]);
  digitalWrite(latchPin0, LOW);  
  digitalWrite(latchPin0, HIGH);
  
}

You should not have to reload the script. What if you hit the reset buttons?
Which board is "stopping" (I don't expect both boards lock up at the same time).

Do you have any way to tell if a board is running, such as the onboard led blinking?

the board seems to be running properly i used the monitor to tell and everything is fine
i don't know i don't think the script is incorrect but i am confused about that it might be a hardware problem from the circuits !! it was working fine until an hour ago !! :S and sometimes it's not getting the message to change the number

You are using the serial monitor on one (or both) boards, through the USB?

What pins are you using serial communications between boards?

i used the monitor on the first and it gave me the result
then i used on second but nothing
and of course i disconnected both boards from each other
when i connect the boards it's threw 0 and 1 RX and TX

firashelou:
i used the monitor on the first and it gave me the result
then i used on second but nothing
and of course i disconnected both boards from each other
when i connect the boards it's threw 0 and 1 RX and TX

The USB serial connection to the host PC uses pins 0 and 1. If you're using the hardware Serial port, you should not connect pins 0 and 1 to anything.

However, with the software writing to and reading from the Serial port you do currently have a convenient way to test your sketches. If you run the sender on its own with no connections to the serial pins you should see + and - printed to the serial monitor each time you press the corresponding button. Similarly if you run the receiver on its own with nothing connected to the serial pins and type + and - to the serial monitor the receiving sketch should receive those characters.

Once you've completed that unit testing you will either need to leave both Arduinos disconnected from the USB ports, or use SoftwareSerial on two other pins for the connection between them.

PeterH:
The USB serial connection to the host PC uses pins 0 and 1. If you're using the hardware Serial port, you should not connect pins 0 and 1 to anything.

exactly that's what i've been doing i disconnect them from each other

PeterH:
However, with the software writing to and reading from the Serial port you do currently have a convenient way to test your sketches. If you run the sender on its own with no connections to the serial pins you should see + and - printed to the serial monitor each time you press the corresponding button. Similarly if you run the receiver on its own with nothing connected to the serial pins and type + and - to the serial monitor the receiving sketch should receive those characters.

Once you've completed that unit testing you will either need to leave both Arduinos disconnected from the USB ports, or use SoftwareSerial on two other pins for the connection between them.

what do you mean by SoftwareSerial ? why do i have to use it ? why not just keep the way i am doing ?

So, when you disconnect the two boards, and run your tests to the serial monitor, does both boards work good, or is the problem showing up? Which board is having the problem?

firashelou:
what do you mean by SoftwareSerial ? why do i have to use it ? why not just keep the way i am doing ?

SoftwareSerial is a library which enables you to use a serial connection on pins other than 0 and 1. You would need to do this if you want the two Arduinos to talk to each other over a serial connection while both also using the USB connection to the host computer.

yes both works good,
something happened : do you think the battery might have influence ? if the battery is going to empty state it might affect my circuit ? because what happened is when i changed the battery to better used one everything works fine

PeterH:

firashelou:
what do you mean by SoftwareSerial ? why do i have to use it ? why not just keep the way i am doing ?

SoftwareSerial is a library which enables you to use a serial connection on pins other than 0 and 1. You would need to do this if you want the two Arduinos to talk to each other over a serial connection while both also using the USB connection to the host computer.

ok so if i am not using the serial to the computer and i am just using the communication between the 2 arduinos, it is not necessary to do this step ?

Yes, if the battery was low/weak, it could act like that.

If you do not connect the usb serial monitor, and just communicate between the two boards, you should not need the softwareserial library.

ok thanks a lot i will check the battery thing and will give a feedback

You should be able to keep the PC connected to the Arduino that is transmitting codes while it's Tx pin is also connected to the Rx pin of the other Arduino. That way you can at least check that the output side is working.

If I was satisfied that the transmitting side is working I would try having the PC connected to the receiving Arduino while it is receiving. I'm not sure if it will work that way, but it might.

In a situation like this an FTDI cable is a great asset as you can use it to monitor the signals passing between the Arduinos.

...R

Robin2:
You should be able to keep the PC connected to the Arduino that is transmitting codes while it's Tx pin is also connected to the Rx pin of the other Arduino. That way you can at least check that the output side is working.

If I was satisfied that the transmitting side is working I would try having the PC connected to the receiving Arduino while it is receiving. I'm not sure if it will work that way, but it might.

In a situation like this an FTDI cable is a great asset as you can use it to monitor the signals passing between the Arduinos.

...R

but why use an FTDI while you have the USB cable ?!

firashelou:
but why use an FTDI while you have the USB cable ?!

I probably don't understand what's behind your question so if the following misses the point I apologize.

A USB cable is just a piece of wire with a connector at each end.

An FTDI cable has a microprocessor in it that converts the USB data into serial data with (inter alia) separate Tx and Rx lines that can read/send data directly from/to Arduino pins.

If you connect an Arduino to a PC with a regular USB cable the Arduino board performs the role of the FTDI cable.

Sometimes, however, it can be useful to have two lines of communication with the Arduino.

...R

Robin2:

firashelou:
but why use an FTDI while you have the USB cable ?!

I probably don't understand what's behind your question so if the following misses the point I apologize.

A USB cable is just a piece of wire with a connector at each end.

An FTDI cable has a microprocessor in it that converts the USB data into serial data with (inter alia) separate Tx and Rx lines that can read/send data directly from/to Arduino pins.

If you connect an Arduino to a PC with a regular USB cable the Arduino board performs the role of the FTDI cable.

Sometimes, however, it can be useful to have two lines of communication with the Arduino.

...R

ok so the FTDI will take the role of the SoftwareSerial ?

firashelou:
ok so the FTDI will take the role of the SoftwareSerial ?

No. The FTDI cable allows you to connect a PC's USB port to whatever pins on an Arduino are used for SoftwareSerial Tx and Rx. All the FTDI cable does is to translate the serial data into stuff that the USB system understands and vice versa.

...R

firashelou:
ok so the FTDI will take the role of the SoftwareSerial ?

The FTDI will act as a bridge between an async TTL serial interface on one side (like a SoftwareSerial interface provides) and a USB interface that you can plug into your PC.

excuse me guys but i really didn't get the difference :S
when we buy an arduino a USB cable comes with it, so this is a normal USB that allows me to connect the arduino to PC
so why would i need the FTDI ? i mean what advantage i will gain from this ?

@robin2 you mean i connect the FTDI to PC then the other side i plugged it into a TX or RX pin 0 or 1 or else from the arduino ? so i won't use the USB post of arduino ?