NaviX
NaviX

Reputation: 1

Arduino serial port printing all lines only when I put a Serial.println(" ") line in the loop

I have some time controlled segments inside my main loop that turning LEDs on/off and are storing values from a sensor into a linked list (Using this linked list library: https://github.com/ivanseidel/LinkedList). I have my baud rate set at 34800 because I want data to be transferred at a faster rate, and I have a a function that for loops the sensor data captured within the the timed loops and prints them.

The weird problem I'm facing is when I Serial.print() the data, it only works when I have a Serial.println()(just Serial.print() does not work) inside each of the if blocks. If I remove this Serial.println() the code does not work, and just gives out garbage in the Serial monitor.

What is causing this issue? How do I resolve it? Here is the code:

void setup( void )
{
  Serial.begin( 38400 );
  while(!Serial){
    }
  pinMode (ledPin670, OUTPUT);
  pinMode (ledPin850, OUTPUT);
  digitalWrite(ledPin670,HIGH);
  digitalWrite(ledPin850,HIGH);
  Start = millis();
  timer = micros();
}

void loop( void )
{
  if ( millis() - Start < 100 )
  {
    //Serial.read();
    Serial.println(" ");
    digitalWrite(ledPin670,LOW);
    analogRead( A0 );
    valList670.add(analogRead( A0 ));
    time670.add(micros() - timer);
    ++Count;
  }
  else if ((millis() - Start >= 100) && (millis() - Start < 110)){
    digitalWrite(ledPin670,HIGH);
    }
  else if ((millis() - Start >= 110) && (millis() - Start < 220))
  {
//    Serial.read();
    Serial.println(" ");
    digitalWrite(ledPin670,HIGH);
    digitalWrite(ledPin850,LOW);
    analogRead( A0 );
    valList950.add(analogRead( A0 ));
    time850.add(micros() - timer);
    ++Count2;
  }
  else if ((millis() - Start >= 220) && (millis() - Start < 230)){
    digitalWrite(ledPin850,HIGH);

  else
  {
    //Serial.println(millis() - Start);
    Serial.println("count:");
    Serial.println( Count );
    Serial.println( Count2 );
    Serial.println( Count3 );
    arrayLoop(valList670, time670,10);
    arrayLoop(valList850, time850,10);

    valList670.clear();
    valList850.clear();
    time850.clear();
    time670.clear();
    timer = micros();
    Count = 0;
    Count2 = 0;
    Start = millis();
}

void arrayLoop(LinkedList<int> &pinNum,LinkedList<unsigned long> &timer, int valDiff){
//  Serial.println(pinNum);
  int listSize = pinNum.size();
  for (int h = 0; h < listSize; h+=valDiff) {
      //Get value from list
      if (h <= listSize){
        int val = pinNum.get(h);
        unsigned long tim = timer.get(h);
        Serial.print(tim);
        Serial.print("\t");
        Serial.println(val);
      }
  }
}

Upvotes: 0

Views: 489

Answers (1)

Julie Jane Dumon
Julie Jane Dumon

Reputation: 3

Only call Serial.print() once.

If you define the circumstances under which you do, and do not, want to call Serial.print(), we can help you see that it happens that way.

Basically, though, you need to count how many times you have already printed. Only actually call print if the count is 0.

Upvotes: 0

Related Questions