Help! Hall Sensor and serialWrite (banging like a machine gun)

Hey all, I'm trying to use Hall sensors to send variables to Max7 to play samples.

I've got it working fine, my only problem is the string of incessant bangs from Arduino.
I'm not sure why, but the third sensor (hallState3) sends a consistent 3 when I have a
sensor on it; but hallState 1 and 2 both send constant 010101010101, or 020202020202.
I really need 1 and 2 to also be 11111111 etc. and 22222222 etc. But I can't get it to work.

I see that my code serialWrite(0); is at the bottom next to hallState3; but I can't seem to
get it to work for the others....

here's my code:

int hallPin = 12;
int hallPin2 = 2;
int hallPin3 = 7;
int ledPin = 13;

int hallState = 0;
int hallState2 = 0;
int hallState3 = 0;

void setup(){
pinMode(hallPin, INPUT);
pinMode(hallPin2, INPUT);
pinMode(hallPin3, INPUT);
Serial.begin(9600);
}

void loop(){
hallState = digitalRead(hallPin);
hallState2 = digitalRead(hallPin2);
hallState3 = digitalRead(hallPin3);

if (hallState == LOW){
digitalWrite (ledPin, HIGH);
Serial.write(1);
}
if (hallState2 == LOW){
digitalWrite (ledPin, HIGH);
Serial.write(2);
}
if (hallState3 == LOW){
digitalWrite (ledPin, HIGH);
Serial.write(3);
}
else {
digitalWrite (ledPin, LOW);
Serial.write(0);
}
delay(100);

}

Thanks for any help.

Not sure exactly what you mean but The last else relates only to the if test on hallState3 being HIGH ==> you don't have any code writing a 0 if the other two hallState are HIGH

Yea, I noticed that as well. But when I add the same Else statements to the
other ones, they all fire 10101010 etc insessantly.

That means your pin's value are changing... how is this wired?

I think the logic of your if statements is a bit muddled.

At the moment the else statement means you print 0 whenever hallState3 is High, every time through the loop. So when hallState1 is low and 3 is high, you see 1010101010

Here's my advice:

  1. work out when do you need the sketch to print 0
  2. Lose that else statement
  3. write an if statement that gives 0 when you need it.

Good luck! :slight_smile:

I suspect that you'll want to investigate the state change detection example. Sending data on EVERY pass through loop(), even though nothing has changed, doesn't make sense to me.