Go Down

Topic: Weird Messenger library (Read 1 time) previous topic - next topic

AWOL

One more time.
Change the 'long' array declarations to 'unsigned int'.
Change the sizeof calculation to use unsigned int instead of long (a macro would be useful here).
Job done, for now.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
Job done, for now.

At least it is if FreeMemory() reports enough memory remains as you continue adding more code.

AWOL

Yeah, I know, but at least something will have been learned.
More strings to your bow, and all that.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

nr1


One more time.
Change the 'long' array declarations to 'unsigned int'.
Change the sizeof calculation to use unsigned int instead of long (a macro would be useful here).
Job done, for now.


ok i did this now.
Now i had a bit more free space and could add 5 more IF's.
But now i'm again stuck only 79 free..

AWOL

#19
Nov 06, 2011, 05:40 pm Last Edit: Nov 06, 2011, 05:42 pm by AWOL Reason: 1
So, go for the PROGMEM solution.
Or go for the factoring and look up table solution.
Sorry, there is no free lunch.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

nr1

Ok as the first solution brought me to the edge of and with arduino i was searching for another solution ;)

Now i always have just have one array in memory, that gets filled with the pulse values by serial communication.
Via serial i can let the IR led blink when sending the "r" command.
Somehow this only works once, so each time i send the "r" command the arduino is stuck and doesn't react on serial communication anymore.
Can anyone see the problem?

Here it is:

Code: [Select]

#include <Messenger.h>

String deviceName = "LedRGBSteuerung";
char seperator = ',';
int irLedPin = 13;

int maxMsgLength = 100;
long msg[100];
int counter = 0;

Messenger message = Messenger(seperator);

void pulseIR(long microsecs)
{
  // we'll count down from the number of microseconds we are told to wait

  cli();  // this turns off any background interrupts

  while (microsecs > 0)
  {
    // 38 kHz is about 13 microseconds high and 13 microseconds low
   digitalWrite(irLedPin, HIGH);  // this takes about 3 microseconds to happen
   delayMicroseconds(10);         // hang out for 10 microseconds
   digitalWrite(irLedPin, LOW);   // this also takes about 3 microseconds
   delayMicroseconds(10);         // hang out for 10 microseconds

   // so 26 microseconds altogether
   microsecs -= 26;
  }
}

void blinkLed(long highTime, long lowTime)
{
  pulseIR(highTime);
  delayMicroseconds(lowTime);
}

void setup()
{
  // Initiate Serial Communication
  Serial.begin(115200);
 
  // Attach the callback function to the Messenger
  message.attach(messageReady);
 
  //reset array
  resetMsg();
 
  Serial.println(deviceName + " ready!!");
}

// Create the callback function
void messageReady()
{
      char firstChar = message.readChar();  // Gets the first word as a character 
     
      if (firstChar == 'c')
        resetMsg();
      else     
      if (firstChar == 'a')
      {                   
          long value;

          do
          {
            value = message.readLong();
           
            //if not -1 is returned save value in array
            //and increase counter
            if (value != -1)
            {
              msg[counter] = value;
              counter++;
            }
          }
          while (value != -1);
                 
          Serial.println("ADDED");
        }
        else
        if (firstChar == 'r')
        {                             
          //let the led blink
          for (int i=0; i<maxMsgLength;i=i+2)
            blinkLed(msg[i], msg[i+1]);
         
         
          Serial.println("BLINKED");
        }
        else
        if (firstChar == 'g')
        {
          //output of current array values
          Serial.print("On/Off: {");
          for (int i=0; i<maxMsgLength;i++)
            if (msg[i] != -1)
            {
              Serial.print(msg[i]);
              Serial.print(",");
            }
            else
              break;
           
          Serial.println("}");
        }
        else
        {
          Serial.println("UNKNOWN CMD");
          message.readInt(); 
        }
}

void loop()
{
  while ( Serial.available() )
    message.process(Serial.read () );
}

void resetMsg()
{
  counter = 0;
 
  for (int i=0; i<maxMsgLength;i++)
    msg[i] = -1;
   
  Serial.println("CLEAR DONE!");
}


WizenedEE

You turn off interrupts without turning them back on in pulseIR(). Serial communication depends on interrupts.

PaulS

Your code is very hard to read. When using else if, both words belong on the same line. For loops should always have { and }, even when not strictly needed. Random indenting and extraneous blank lines don't help.

When you send an r, I don't see that you reset the message instance, after processing the r.

I really can't see what value the Messenger library is adding to this sketch.

nr1


You turn off interrupts without turning them back on in pulseIR(). Serial communication depends on interrupts.


oh thx i see it now, this was the problem
thx a lot!!!

You turn off interrupts without turning them back on in pulseIR(). Serial communication depends on interrupts.


I really can't see what value the Messenger library is adding to this sketch.


How would you do it without using Messenger library for the serial communication?

PaulS

Quote
How would you do it without using Messenger library for the serial communication?

I'd collect the data in a character array, and parse it myself. You know what you are sending, how the pieces are delimited, and what order the pieces are in.
http://jhaskellsblog.blogspot.com/2011/05/serial-comm-fundamentals-on-arduino.html

nr1

ok, however finally it's nearly working perfect with messenger library and following sketch:

Code: [Select]

#include <Messenger.h>

String deviceName = "LedRGBSteuerung";
char seperator = ',';
int irLedPin = 13;

int maxMsgLength = 100;
long msg[100];
int counter = 0;

Messenger message = Messenger(seperator);

void pulseIR(long microsecs)
{
  // we'll count down from the number of microseconds we are told to wait

  cli();  // this turns off any background interrupts

  while (microsecs > 0)
  {
    // 38 kHz is about 13 microseconds high and 13 microseconds low
   digitalWrite(irLedPin, HIGH);  // this takes about 3 microseconds to happen
   delayMicroseconds(10);         // hang out for 10 microseconds
   digitalWrite(irLedPin, LOW);   // this also takes about 3 microseconds
   delayMicroseconds(10);         // hang out for 10 microseconds

   // so 26 microseconds altogether
   microsecs -= 26;
  }
 
  sei(); //turn on interrupts again
}

void blinkLed(long highTime, long lowTime)
{
  pulseIR(highTime);
  delayMicroseconds(lowTime);
}

void setup()
{
  // Initiate Serial Communication
  Serial.begin(115200);
 
  // Attach the callback function to the Messenger
  message.attach(messageReady);
 
  //array init
  resetMsg();
 
  Serial.println(deviceName + " ready!!");
}

// Create the callback function
void messageReady()
{   
      char firstChar = message.readChar();  // Gets the first word as a character 
     
      if (firstChar == 'c')
        resetMsg();
      else     
      if (firstChar == 'a')
      {                   
          long value;

          do
          {
            value = message.readLong();
           
            //if not -1 is returned save value in array
            //and increase counter
            if (value != -1)
            {
              msg[counter] = value;
              counter++;
            }
          }
          while (value != -1);
                 
          Serial.println("ADDED");
        }
        else
        if (firstChar == 'r')
        {                             
          //let the led blink       
          for (int i=0; i<maxMsgLength;i=i+2)
            if (msg[i+1]!=-1)
              blinkLed(msg[i], msg[i+1]);
             else
               break;
         
          Serial.println("BLINKED");
        }
        else
        if (firstChar == 'g')
        {
          //output of current array values
          Serial.print("On/Off: {");
          for (int i=0; i<maxMsgLength;i++)
          {
              Serial.print(msg[i]);
              Serial.print(",");
          }
           
          Serial.println("}");
        }
        else
        {
          Serial.println("UNKNOWN CMD");
          message.readInt(); 
        }
}

void loop()
{
  while ( Serial.available() )
    message.process(Serial.read () );
}

void resetMsg()
{
  counter = 0;
 
  for (int i=0; i<maxMsgLength;i++)
    msg[i] = -1;
   
  Serial.println("CLEAR DONE!");
}


There is only one problem:
If i send the messages too fast to Arduino, it get's stuck (IR Led is always on, Arduino doesn't react on serial commandos anymore).
How to solve this?

nr1


nr1


If i send the messages too fast to Arduino, it get's stuck (IR Led is always on, Arduino doesn't react on serial commandos anymore).
How to solve this?


I experimented some more time regarding this issue.
It seems that somehow a buffer (or even the RAM) is getting full when sending messages too fast to the Arduino. This causes the Arduino to be stuck completely (so i have to reset it / or replug the usb connector to get it working again).
How to solve this?
 

Go Up