Arduino spamming over serial [URGENT]

Hello guys.
I have this problem with arduino or code where arduno spamms something over serial even tho it should NOT. It is actually randomly spamming the things that it is supposed to send if the pins were powered. And man its fast. like 10 in a sec. Here is the code. Its simple 8 inputs and 8 outputs and it should send over serial which is connected. Or read over serial and turn some pins on. Yeah i know. A lot of code. But i need help urgently. I have tried it on Leonardo and Uno. Same on both.

int out1 = 4, out2 = 5, out3 = 6, out4 = 7, out5 = 8, out6 = 9, out7 = 10, out8 = 11;
int in1 = A0, in2 = A1, in3 = A2, in4 = A3, in5 = A4, in6 = A5, in7 = 2, in8 = 3;  
int val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0, val7 = 0, val8 = 0; 
int prev1 = 0, prev2 = 0, prev3 = 0, prev4 = 0, prev5 = 0, prev6 = 0, prev7 = 0, prev8 = 0;
int recv = 0;

void setup() {
  Serial.begin(9600);
  pinMode(out1, INPUT);
  pinMode(out2, INPUT);
  pinMode(out3, INPUT);
  pinMode(out4, INPUT);
  pinMode(out5, INPUT);
  pinMode(out6, INPUT);
  pinMode(out7, INPUT);
  pinMode(out8, INPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(in5, OUTPUT);
  pinMode(in6, OUTPUT);
  pinMode(in7, OUTPUT);
  pinMode(in8, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  if (Serial.available() > 0) {
    recv = Serial.read();
    if(recv == 97)
      digitalWrite(in1, HIGH);
    else if(recv == 98)
      digitalWrite(in2, HIGH);
    else if(recv == 99)
      digitalWrite(in3, HIGH);
    else if(recv == 100)
      digitalWrite(in4, HIGH);
    else if(recv == 101)
      digitalWrite(in5, HIGH);
    else if(recv == 102)
      digitalWrite(in6, HIGH);
    else if(recv == 103)
      digitalWrite(in7, HIGH);
    else if(recv == 104)
      digitalWrite(in8, HIGH);
    else if(recv == 107)
      digitalWrite(in1, LOW);
    else if(recv == 108)
      digitalWrite(in2, LOW);
    else if(recv == 109)
      digitalWrite(in3, LOW);
    else if(recv == 110)
      digitalWrite(in4, LOW);
    else if(recv == 111)
      digitalWrite(in5, LOW);
    else if(recv == 112)
      digitalWrite(in6, LOW);
    else if(recv == 113)
      digitalWrite(in7, LOW);
    else if(recv == 114)
      digitalWrite(in8, LOW);
  }
    
  val1 = digitalRead(out1); 
  if(val1 == HIGH && prev1 == 0) {
    Serial.println("out1");
    prev1 = 1;
  }
  else if(val1 == LOW && prev1 == 1) {
    Serial.println("out10");
    prev1 = 0;
  }
  
  val2 = digitalRead(out2); 
  if(val2 == HIGH && prev2 == 0) {
    Serial.println("out2");
    prev2 = 1;
  }
  else if(val2 == LOW && prev2 == 1) {
    Serial.println("out20");
    prev2 = 0;
  }
  
  val3 = digitalRead(out3); 
  if(val3 == HIGH && prev3 == 0) {
    Serial.println("out3");
    prev3 = 1;
  }
  else if(val3 == LOW && prev3 == 1) {
    Serial.println("out30");
    prev3 = 0;
  }
  
  val4 = digitalRead(out4); 
  if(val4 == HIGH && prev4 == 0) {
    Serial.println("out4");
    prev4 = 1;
  }
  else if(val4 == LOW && prev4 == 1) {
    Serial.println("out40");
    prev4 = 0;
  }
  
  val5 = digitalRead(out5); 
  if(val5 == HIGH && prev5 == 0) {
    Serial.println("out5");
    prev5 = 1;
  }
  else if(val5 == LOW && prev5 == 1) {
    Serial.println("out50");
    prev5 = 0;
  }
  
  val6 = digitalRead(out6); 
  if(val6 == HIGH && prev6 == 0) {
    Serial.println("out6");
    prev6 = 1;
  }
  else if(val6 == LOW && prev6 == 1) {
    Serial.println("out60");
    prev6 = 0;
  }
  
  val7 = digitalRead(out7); 
  if(val7 == HIGH && prev7 == 0) {
    Serial.println("out7");
    prev7 = 1;
  }
  else if(val7 == LOW && prev7 == 1) {
    Serial.println("out70");
    prev7 = 0;
  }
  
  val8 = digitalRead(out8); 
  if(val8 == HIGH && prev8 == 0) {
    Serial.println("out8");
    prev8 = 1;
  }
  else if(val8 == LOW && prev8 == 1) {
    Serial.println("out80");
    prev8 = 0;
  }
}

Sounds like your input pins are floating. Use mode INPUT_PULLUP to enable the internal pull-up resistor or provide an external pull-up or pull-down resistor.

Your code would be a lot shorter, a lot easier to read and a lot easier to maintain if you spent a little time learning about arrays.

pinMode(in1, OUTPUT);

Stuff like that puzzles me.

@JohnWasser
THANK YOU SO MUCH. I was in a really big hurry. This helped. Never knew that was availiable on arduino.

@AWOL
Yeah I could but this looks like a lot of code and its for a project which wll be presented to kids and peaople not familiar with coding and it will look more :smiley:

Yeah I could but this looks like a lot of code and its for a project which wll be presented to kids and peaople not familiar with coding and it will look more

Why not add a few multi line comments to make it look even more impressive ?

More seriously write a compact version and compare and contrast it with the verbose version.

and its for a project which wll be presented to kids and peaople not familiar with coding

In that case if(recv == 97)
will look more legible to them as if(recv == 'a')

Also, as a I pointed-out earlier, they might also be similarly confused by something called "in1" used in the same sense as something called "out1"

matijalazic:
Yeah I could but this looks like a lot of code and its for a project which wll be presented to kids and peaople not familiar with coding and it will look more

I don't think I have ever heard of a worse reason for poor code.

That audience is exactly the sort for which the best possible coding practices should be demonstrated.

Do you think successful authors of kids' books say to themselves - they are kids, any old crap will be fine.

ALSO - it is quite likely that some of the kids will know more about coding than you seem to.

...R

Your entire setup() block could and should be rewritten with only 3 lines if you were using arrays.

Another thing: since you are using the Serial Monitor, I'd refrain from using pins 0 and 1 (RX and TX, respectively), because they are used by the Serial class. using digitalWrite on PD1 might cause unwanted effects on what the Serial Monitor reads.