1

I've been working on a project where I want to transmit data from one arduino to another using an Xbee network and then parse it to update some PWM outputs accordingly (Building a system which controls some LED lamps).

The thing is I wanted to debug the receiver using a number of Serial.print() but it appears a lot of them are "ignored" by the Serial Monitor. For instance, if you take a look at the image below you will notice there is a "Zone:" that should be displayed somewhere on screen. It isn't. I also got a Serial.print("Coucou") in the loop() that is never displayed.

Has anyone ever encountered a bug of this kind ?

Strange Bug

As asked, the code that is producing the output

#include <EEPROM.h>
#include <Dispositif.h> // Custom class i wrote.

// how much serial data we expect before a newline
const uint8_t MAX_INPUT = 196;

//zone = 1, id = 1, idNode = 2
Dispositif dispositif;

void setup ()
{
  Serial.begin(9600);
  //delay(1000);
  Serial.println("Cool");
} // end of setup

// here to process incoming serial data after a terminator received
void process_data (const char * data, unsigned int input_pos)
{
  uint8_t value = 0;
  uint8_t i = 1;
  uint8_t currentZone;
  char c;

  if(data[0] != '<')
  {
    i = peekNextP(data);
  }
  while (data[i] != '>' && i < input_pos - 3)
  {
    // zone
    value = 0;
    i = peekNextDigit(data, i);
    do{
      c = data[i];
      i++;
      if(c >= '0' && c <= '9')        // is c a digit?
        value = value * 10 + c - '0';
    } while(c >= '0' && c <= '9');
    currentZone = value;
    Serial.print(currentZone);
    Serial.print(F("("));

    // 1st order
    value = 0;
    i = peekNextDigit(data, i);
    do{
      c = data[i];
      i++;
      if(c >= '0' && c <= '9')        // is c a digit?
        value = value * 10 + c - '0';
    } while(c >= '0' && c <= '9');
    Serial.print(value);
    Serial.print(F(","));

    if (currentZone == dispositif.zone) {
      dispositif.setBrightness(0, value);
    }
    // 2nd order
    value = 0;
    i = peekNextDigit(data, i);
    do{
      c = data[i];
      ++i;
      if(c >= '0' && c <= '9')        // is c a digit?
        value = value * 10 + c - '0';
    } while(c >= '0' && c <= '9');
    Serial.print(value);
    Serial.print(F(","));

    Serial.print(F("Zone:"));
    Serial.print(dispositif.zone);
    if (currentZone == dispositif.zone)
      dispositif.setBrightness(1, value);

    // 3nd order
    value = 0;
    i = peekNextDigit(data, i);
    do{
      c = data[i];
      ++i;
      if(c >= '0' && c <= '9')        // is c a digit?
        value = value * 10 + c - '0';
    } while(c >= '0' && c <= '9');
    Serial.print(value);
    Serial.println(F(")"));

    if (currentZone == dispositif.zone)
      dispositif.setBrightness(2, value);
  }

}  // end of process_data

uint8_t peekNextDigit(const char* data, uint8_t i)
{
  int c;
  while (1) {
    c = data[i];
    if (c >= '0' && c <= '9') return i;
    ++i;
  }
}

uint8_t peekNextP(const char* data)
{
  uint8_t c;
  uint8_t i = 0;
  while (1) {
    c = data[i];
    ++i;
    if (c == ')') return i;
  }
}

void processIncomingByte (const byte inByte)
{
  static char input_line [MAX_INPUT];
  static unsigned int input_pos = 0;

  switch (inByte)
  {
    case '\n':   // end of text
      input_line [input_pos] = 0;  // terminating null byte

      // terminator reached! process input_line here ...
      process_data (input_line, input_pos);

      // reset buffer for next time
      input_pos = 0;  
      break;

    case '\r':   // discard carriage return
      break;

    default:
      // keep adding if not full ... allow for terminating null byte
      if (input_pos < (MAX_INPUT - 1))
        input_line [input_pos++] = inByte;
      break;

  }  // end of switch

} // end of processIncomingByte  

void loop()
{

  Serial.println("Coucou");
  // if serial data available, process it
  if (Serial.available () > 0)
    processIncomingByte (Serial.read ());

}  // end of loop

The Serial output is :

<o>1(255,255,255)<o>
<o>1(0,0,0)<o>
<o>1(255,255,255)<o>

What I am aiming for :

Cool
Coucou
<o>1(255,255,Zone:1255)<o>
Coucou
<o>1(0,0,Zone:10)<o>

My library :

#include <Dispositif.h>
#include <EEPROM.h>

#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

#define pin1 5
#define pin2 6
#define pin3 9

Dispositif::Dispositif()
{
  pinMode(pin1, OUTPUT); // absolutely necessary because we use a custom PWM
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);

  setBrightness(0, 50);
  setBrightness(1, 50);
  setBrightness(2, 50);

  last_value[0] = 50;
  last_value[1] = 50;
  last_value[2] = 50;

  zone = EEPROM.read(0);
  id = EEPROM.read(1);
  idNode = EEPROM.read(2);
}

Dispositif::Dispositif(uint8_t zone, uint8_t id, uint8_t idNode)
{
  zone = zone;
  id = id;
  idNode = idNode;

  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);

  setBrightness(0, 50);
  setBrightness(1, 50);
  setBrightness(2, 50);

  last_value[0] = 50;
  last_value[1] = 50;
  last_value[2] = 50;

  EEPROM.write(0, zone);
  EEPROM.write(1, id);
  EEPROM.write(2, idNode);
}

void Dispositif::setBrightness(uint8_t number, uint8_t value)
{
    if (!Serial)
      Serial.begin(9600);
    unsigned long currentMillis = millis();
    unsigned long lastMillis = 0;

    while(value - last_value[number] > 25)
    {
      currentMillis = millis();
      if (currentMillis - lastMillis > 1000)
      {
        Serial.println("At least i'm here");
        Serial.println(last_value[number]);
        last_value[number] += 25;
        setPWM(number, last_value[number]);
        lastMillis = currentMillis;
      }
    }
    setPWM(number, value);
    last_value[number] = value;
}

void Dispositif::setPWM(uint8_t number, uint8_t val)
{
    if (1 == number) // pin 6
    {
      sbi(TCCR0A, COM0A1);
      OCR0A = val; // set pwm duty
    }
    else if (2 == number) // pin 9
    {
      sbi(TCCR1A, COM1A1);
      OCR1A = val; // set pwm duty
    }
    else // pin 5
    {
      sbi(TCCR0A, COM0B1);
      OCR0B = val; // set pwm duty
    }
}

void Dispositif::changeZone(uint8_t newZone)
{
  zone = newZone;
}

The code is not really pretty, because at one point I was using the SD library and had to reduce program space.

3
  • 2
    Cab you show us some more of your script? – Handoko Nov 18 '14 at 0:10
  • 1
    Instead of posting a picture of your IDE, can you please post the actual code and the actual serial output? Thanks! – Anonymous Penguin Nov 18 '14 at 1:20
  • I added all the involved code. I apologise for its messiness. – openTankist Nov 18 '14 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.