[SOLVED]Inconsistent Data Flow With Push Buttons

I have this circuit with 2 push buttons, an arduino uno and mega. I want the uno to read each state of the push buttons (1 for pressed, 0 for not pressed), and send that data to the mega via a send wire (pin 12 on each). The problem is that whenever i press one of the 2 push buttons, the state of the other one changes between 0 and 1 rapidly(image of COM6). Is there a way to fix this? Have i done something wrong?
Code and pictures below:

//Arduino Mega Code

int recievePin = 12;
int InitPin = 11;
int FirstPin = 5;
int LastPin = 6;
String n = "";

void setup()
{
  Serial.begin(9600);
  pinMode(recievePin, INPUT);
}

void loop()
{
  int s = digitalRead(InitPin);
  if(s == HIGH)
  {
    for(int i = FirstPin; i <= LastPin; i++)
    {
      int state;
     state = digitalRead(recievePin);
     if(state == HIGH)
     {
        n += "1";
      }
      else if(state == LOW)
      {
        n += "0";
     }
    }
     Serial.println(n);
     n = "";
  }
}
//Arduino Uno Code

int FirstPin = 5;
int LastPin = 6;
int sendPin = 12;
int InitPin = 11;
String n = "";

void setup()
{
  for(int i = FirstPin; i <= LastPin; i++)
  {
    pinMode(i , INPUT);
  }
  pinMode(sendPin, OUTPUT);
  pinMode(InitPin, OUTPUT);
  digitalWrite(InitPin, LOW);
  //Serial.begin(9600);
}

void loop()
{
  for(int i = FirstPin; i <= LastPin; i++)
  {
    int state;
    state = digitalRead(i);
    if(state == HIGH)
    {
      n += "1";
      digitalWrite(sendPin, HIGH);
    }
    else
    {
      n += "0";
      digitalWrite(sendPin, LOW);
    }
  }
  //Serial.println(n);
  n = "";
  int Characters = n.length();
  digitalWrite(InitPin, HIGH);
  for(int y; y <= Characters; y++)
  {
    char s = n.charAt(y);
    if(s == "1")
    {
      digitalWrite(sendPin, HIGH);
    }
    else if(s == "0")
    {
      digitalWrite(sendPin, LOW);
    }
  }
  digitalWrite(InitPin, LOW);
}

  for(int y; y <= Characters; y++)

what is the initial value of y?

why do you set the sendPin to value both because of the digitalRead() and the value of the String n? did you intend to set the String to the value of the read and later affect the sendPin based on the String value?

looks like your using the InitPin to synchronize the reads from the receive pin. how can you be sure you are reading the receive pin synchronous with the writes.

a common approach is to use the rising/falling edge of a clock pin to write/read a data pin. but then how do you determine the starting clock pulse. (period w/o any pulses)?

gcjr: ``` "looks like your using the InitPin to synchronize the reads from the receive pin.  how can you be sure you are reading the receive pin synchronous with the writes."

Interesting point. The InitPin system was made with the intention of synchronising the 2 systems. Looks like i will have to check that out.

Mikaelgameryolo: I have this circuit with 2 push buttons, an arduino uno and mega. I want the uno to read each state of the push buttons (1 for pressed, 0 for not pressed), and send that data to the mega via a send wire (pin 12 on each).

Is there some reason not to send the data using a simple Serial connection?

...R

Robin2: Is there some reason not to send the data using a simple Serial connection?

...R

Well, as far as i know, serial communication is slow. Plus, i am not sure how to transfer integers, and since this is a test for my project, i need only 2 ints to be transferred. In the final product, 26 zeros and ones must be transferred. I will try though.

Mikaelgameryolo: Well, as far as i know, serial communication is slow.

Not at 115,200 baud.

aarg: Not at 115,200 baud.

Good point. What about the 26 zeros and ones that need to be transferred? Can you recommend anything about that? Also, i am not planning on using the uno and mega, but some Attiny84s, so i am not sure how this can work.

Mikaelgameryolo: In the final product, 26 zeros and ones must be transferred.

Mikaelgameryolo: What about the 26 integers that need to be transferred?

Which is it?

dougp: Which is it?

Zeros and ones, my bad.

what is the requirement? 26 bits in what amount of time and how often?

gcjr: what is the requirement? 26 bits in what amount of time and how often?

Ok, so i need 26 bits every 30 milliseconds. So, an example of 5 bits would be: 01001

one character is transmitted in ~1 msec at 9600 bps. so a 20 byte message is probably more than big enough to send a 26 bit value, 8 digits, every 30 msec.

gcjr:
one character is transmitted in ~1 msec at 9600 bps. so a 20 byte message is probably more than big enough to send a 26 bit value, 8 digits, every 30 msec.

Alright, that solves the speed problem. And i looked it up, the Attiny84 can be used with serial. The thing is, i dont know how. Like, is it different from the uno? Are there any specific documents that can help me?

Mikaelgameryolo: And i looked it up, the Attiny84 can be used with serial.

When I made the suggestion in Reply #3 to use Serial it was on the basis that your Original Post said "an arduino uno and mega". Now you seem to have moved the goal posts

What is the distance between your Arduinos?

The Attiny84 does not have HardwareSerial but it does have SPI . This is a little more complex to use but it will perform a lot faster than SoftwareSerial. The Attiny84 datasheet has all the info and some sample code.

...R

Robin2: When I made the suggestion in Reply #3 to use Serial it was on the basis that your Original Post said "an arduino uno and mega". Now you seem to have moved the goal posts

What is the distance between your Arduinos?

The Attiny84 does not have HardwareSerial but it does have SPI . This is a little more complex to use but it will perform a lot faster than SoftwareSerial. The Attiny84 datasheet has all the info and some sample code.

...R

I have not heard of SPI before. I will try it out when i get home. Can you reference an article on it or something?

Mikaelgameryolo: Can you reference an article on it or something?

Always start with the Arduino reference documentation

...R

Thank you all for your help!