Printing to serial is not as expected

I have the following code which should read the value from three different sensors and print in new line each sensor value

#define NUM_INPUTS 3

const byte SIG_pins[] = { A0, A1, A2 };
int heartbeatLED = 13;

const byte filter = 2;  //adjust as needed

const byte pressThreshold = 10;

int lastValue[NUM_INPUTS];
bool muxIsOn[NUM_INPUTS];


//timing variables
unsigned long previousMillis = 0;
unsigned long heartbeatMillis;

unsigned long interval = 20;




//********************************************^************************************************
void setup() {
  Serial.begin(115200);

  pinMode(heartbeatLED, OUTPUT);

  for (byte i = 0; i < NUM_INPUTS; i++) {
    pinMode(SIG_pins[i], INPUT);
  }
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    readValues();
    checkHeartbeatTIMER();
  }
}


//// functions ////



void readValues() {
  for (byte i = 0; i < NUM_INPUTS; i++) {
    //read the stabilized analog
   int SensorValue = 1023 - analogRead(SIG_pins[i]);

    //has analog value changed more than the filter amount ?
    if (abs(lastValue[i] - SensorValue) > filter) {
      if (lastValue[i] != SensorValue) {
        //update to the new value
        lastValue[i] = SensorValue;
        Serial.print("sensor");
        Serial.print(i + 1);
        Serial.print(" ");
        Serial.println(SensorValue);

        //      /* new feature removed for now
        if (SensorValue >= pressThreshold)  // should mux channel be on?
        {
          if (muxIsOn[i] == 0)  // was it off? tell us it went on
          {
            muxIsOn[i] = 1;  // remember it went on
            Serial.print("ch");
            Serial.print(i + 1);
            Serial.print("on ");  // and say so
            Serial.println(muxIsOn[i]);
          }
        } else  // mux channel should be off
        {
          if (muxIsOn[i] == 1)  // was it on? tell us it went off
          {
            muxIsOn[i] = 0;  // remember it is off
            Serial.print("ch");
            Serial.print(i + 1);
            Serial.print("on ");  // and say so
            Serial.println(muxIsOn[i]);
          }
        }
      }
    }
  }
}

void checkHeartbeatTIMER() {
  //*********************************                                heartbeat TIMER
  //is it time to toggle the heartbeatLED ?
  if (millis() - heartbeatMillis >= 500ul) {
    //restart this TIMER
    heartbeatMillis = millis();

    //toggle the heartbeatLED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

}  //END of   checkHeartbeatTIMER()

The printing I'm getting is something like this which is not what I expected:

How can I fix it? what I'm doing wrong?

Hello, do yourself a favour and please read How to get the best out of this forum and post accordingly

➜ can you describe your hardware and wiring / power ?

But I have posted the full code..

sorry - yes. that's a canned message which I posted too quickly.

I meant to ask about the hardware and wiring / power

also add a print in the setup, that will make it clear if the program is rebooting

Perhaps your PC is too busy to catch the entire Arduino output.
An interval of 20ms per line is too fast for a human eye. Try increasing it to 100 or 500 ms.

wiring as follow:

I change the reading interval to 100 and indeed the printing seems ok but is to slow.. I would like it to have faster response (it is for musical application, playing)

can you try with a different terminal application?

are you using IDE 2.x ?

ok, I'm not sure what it was but I reset my computer and it is working fine now with 20ms interval..

the joy of windows ? :wink:

1 Like

As I suspected, perhaps an update or virus check blocked the PC.

Just last week I had a similar problem with an unexpected update of my Linux, dead for about half an hour :frowning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.