Pages: 1 [2]   Go Down
Author Topic: Weird Messenger library  (Read 1648 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25794
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Job done, for now.
At least it is if FreeMemory() reports enough memory remains as you continue adding more code.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25794
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, I know, but at least something will have been learned.
More strings to your bow, and all that.
Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25794
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, go for the PROGMEM solution.
Or go for the factoring and look up table solution.
Sorry, there is no free lunch.
« Last Edit: November 06, 2011, 11:42:30 am by AWOL » Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok as the first solution brought me to the edge of and with arduino i was searching for another solution smiley-wink

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:
#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!");
}

Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any ideas?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
 
Logged

Pages: 1 [2]   Go Up
Jump to: