What is the result of the array size calculation?
The probem is very easy to solve, in several different ways.
Some will require lots of changes, some very few.
Probably the simplest short term fix would be to change all the longs into unsigned ints.
PaulS:
Of course it is. Just get rid of all those arrays.
How would you implement it without using arrays?
How would you implement the serial communication part (with messenger library or something different?)
AWOL:
Probably the simplest short term fix would be to change all the longs into unsigned ints.
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.
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.
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..
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:
#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!");
}
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.
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.
ok, however finally it's nearly working perfect with messenger library and following sketch:
#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:
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?