Buffering blocks- how to resume?

Hello! I fixed the code with the "while (Serial.available() > 0)" and eliminating all the delays but still doesn't work. Prints continuosly one of the values even if the pins are free.

The electronic circuit is tested and working.

We are working on a "Makey Makey-like" arduino(Uno) with three touch controller (simple wires) trough digital pins.

here the code:

int inPin2 = 3 ;  
int inPin3 = 4 ; 
int inPin4 = 7 ; 

int digitalValue1;
int digitalValue2;
int digitalValue3;

void setup() 
{
  Serial.begin(9600);
   }


void loop() {
  
  while (Serial.available() > 0)
  
   int digitalValue1 = digitalRead(inPin2);
   
  if (digitalValue1 == HIGH){
    
    Serial.write("A");
      
  } 
 
  
   int digitalValue2 = digitalRead(inPin3);
   
    if (digitalValue2 == HIGH) {
    Serial.write ("B");
    
  } 
 
  
  int digitalValue3 = digitalRead(inPin4);
  
  
   if (digitalValue3 == HIGH) {
    Serial.write("C"); 
   
    
  } 

  }

What do you mean "pins are free"?

Better read this: Gammon Forum : Electronics : Microprocessors : Switches tutorial

add this to setup

pinMode (inPin2, INPUT_PULLUP);
pinMode (inPin3, INPUT_PULLUP);
pinMode (inPin4, INPUT_PULLUP);

and change your test to look for LOWs:

if (digitalValue1 == LOW){

Also, what is this doing for you?

while (Serial.available() > 0)

You never do

incomingByte = Serial.read();

if something does happen to be received.

[quote author=Nick Gammon link=topic=173056.msg1285202#msg1285202 date=1371705443]

What do you mean "pins are free"?

Better read this: Gammon Forum : Electronics : Microprocessors : Switches tutorial
[/quote]free= taking ALL the wires off from the arduino (pins, ground,voltage) the shield alone (only with the usb connected) still sends a same value

at first all it worked fine but later this problem presented twice time. It seems like a buffering issue.

here the circuit

JackMilan:

[quote author=Nick Gammon link=topic=173056.msg1285202#msg1285202 date=1371705443]

What do you mean "pins are free"?

Better read this: Gammon Forum : Electronics : Microprocessors : Switches tutorial

free= taking ALL the wires off from the arduino (pins, ground,voltage) the shield alone (only with the usb connected) still sends a same value
[/quote]

So in other words, the pin is floating with an undefined state.

Arrch:
So in other words, the pin is coating with an undefined state.

Exactly! and seems there is no way to get rid of this value coming from the buffer even when the rest of the sketch runs correctly and the arduino gets the right touch on/off values for the other pins

@ CrossRoads

I've done what you said but it still blocks with the PULL_UP mode while it seems for now :roll_eyes: to read again the sketch with the

while (Serial.available() > 0)
incomingByte =Serial.read();

put in the right place. Let's see if it will last!

JackMilan:

Arrch:
So in other words, the pin is coating with an undefined state.

Exactly! and seems there is no way to get rid of this value coming from the buffer even when the rest of the sketch runs correctly and the arduino gets the right touch on/off values for the other pins

If the pin is undefined the results will be undefined.

JackMilan:
I've done what you said but it still blocks with the PULL_UP mode ...

Better post your modified sketch.

Last Code:

int inPin2 = 3 ;  
int inPin3 = 4 ; 
int inPin4 = 7 ; 

int incomingByte = 0;
int digitalValue1;
int digitalValue2;
int digitalValue3;

void setup() 
{

  Serial.begin(9600);
   }


void loop() {
  
  while (Serial.available() > 0)
  incomingByte =Serial.read();
  
   int digitalValue1 = digitalRead(inPin2);
   
  if (digitalValue1 == HIGH){
    
    Serial.write("A");
      
  } 
 
  
   int digitalValue2 = digitalRead(inPin3);
   
    if (digitalValue2 == HIGH) {
    Serial.write ("B");
    
  } 
 
  
  int digitalValue3 = digitalRead(inPin4);
  
  
   if (digitalValue3 == HIGH) {
    Serial.write("C"); 
   
    
  } 

  }
  while (Serial.available() > 0)
  incomingByte =Serial.read();

If you're not doing anything with the data, why bother?

What's with the piss poor indenting? Use Tools + Auto Format BEFORE posting code.

What IS the problem?

JackMilan:
I've done what you said but it still blocks with the PULL_UP mode

The code you just posted does not enable the input pull-up resistors. Without any internal pullup and nothing connected externally the inputs will be floating and can return arbitrary values when you read them.

Probably I explaned in a wrong way :frowning:

I attach the wires to my arduino and load my sketch. Then i print only High Values (it's like getting an ON, if someone touchs). But insted of On i have "A" "B" "C".

I treat everything like three pushbuttons. I use the Pull down resistor mode here:

BUT ------> this works fine only for a while and then blocks, the light on TX became non stop RED and on my monitor i can see printing "B" without stopping even if nobody touches the wire. Even worse, if i get off the wires from my arduino (all) and reload the same sketch the problem remains. The "B" still prints.
Without any logic goes away after many tries to reload the sketch. That's why I think may be a buffering issue, need to clean the buffer every now and then. Hope that

while (Serial.available() > 0)
incomingByte =Serial.read();

is enough to do. :roll_eyes:

As others have said, it sounds like floating inputs. Experienced this with trimpots and switches that were not forming good connections to jumper wires.

   int digitalValue2 = digitalRead(inPin3);
   
    if (digitalValue2 == HIGH) {
    Serial.write ("B");
    
  }

Apart from any other problems this will very quickly fill up the output serial buffer and then the code will block while the buffer empties.

That's why I think may be a buffering issue, need to clean the buffer every now and then. Hope that

while (Serial.available() > 0)

incomingByte =Serial.read();




is enough to do.

Are you sending serial data to the Arduino? If so, why are you not doing anything with it?