Crash when incrementing integer
hi all,
i have been working on below program time, , have based off topic found here:
http://arduino-cool.blogspot.com.au/2012/09/arduino-rgb-led-managed-by-remote.html
here code:
the issue having, , have scratched head on hours now, in varybrightness function, when incrementing brightness up, whole arduino locks up.
it works when it's going down. see no logical reason happening - have tried reversing reference/dereferencing methods no avail, thinking reference related.
i'm uploading arduino uno sdk 1.5.6-r2. i'm hoping can shed light on missing here causing fail!
thanks in advance
i have been working on below program time, , have based off topic found here:
http://arduino-cool.blogspot.com.au/2012/09/arduino-rgb-led-managed-by-remote.html
here code:
code: [select]
#include <wire.h>
#include <liquidcrystal_i2c.h>
#include <irremote.h>
// lcd driver
// a4 (sda), a5 (scl)
liquidcrystal_i2c lcd(0x27, 16, 2); // set lcd address 0x20(cooperate 3 short circuit caps) 16 chars , 2 line display
// motion sensor
// d3 (pwm)
#define motionpin 3
#define ledpin 2
// rgb driver
// d4, d9 (pwm), d10 (pwm), d11 (pwm)
#define irpin 4
#define bluepin 9
#define redpin 10
#define greenpin 11
irrecv irrecv(irpin); // initialise irrecv library
// digital button
// d13
#define buttonpin 13
//remote signals
#define on 0xff827d
#define off 0xff02fd
#define brightness_up 0xff3ac5
#define brightness_down 0xffba45
#define flash 0xffd02f
#define fade7 0xffe01f
#define fade3 0xff609f
#define jump7 0xffa05f
#define jump3 0xff20df
#define red 0xff1ae5
#define green 0xff9a65
#define blue 0xffa25d
#define white 0xff22dd
#define pink 0xff12ed
#define orange 0xffb04f
#define yellow_dark 0xffa857
#define yellow_medium 0xff9867
#define yellow_light 0xff8877
#define green_light 0xff30cf
#define green_blue1 0xff28d7
#define green_blue2 0xff18e7
#define green_blue3 0xff08f7
#define blue_red 0xff708f
#define purple_dark 0xff6897
#define purple_light 0xff58a7
#define increment 10
//maximum duty cycle used on each led color balancing.
//if "white" (r=255, g=255, b=255) doesn't white, reduce red, green, or blue max value.
const int max_red = 255;
const int max_green = 255;
const int max_blue = 255;
int motionstatus = 0; // stores whether motion detection on or off
decode_results results; // stores data received on ir
unsigned long rgb = 0;
int r, g, b;
void setup() {
// setup pins
pinmode(bluepin, output);
pinmode(redpin, output);
pinmode(greenpin, output);
pinmode(motionpin, input);
lcd.init(); // initialise lcd i2c connection
lcd.backlight();
irrecv.enableirin(); // start ir receiver
serial.begin(9600); // start serial
}
void loop() {
if (irrecv.decode(&results)) {
serial.println(results.value, hex); //print raw data
if ( results.value != 0xffffffff) {
switch (results.value) {
case brightness_up :
varybrightness (r, increment);
varybrightness (g, increment);
varybrightness (b, increment);
break;
case brightness_down :
varybrightness (r, -increment);
varybrightness (g, -increment);
varybrightness (b, -increment);
break;
case off :
r = g = b = 0;
break;
case on :
break;
// colours in same hex used in html, add 0x00
case red : rgb(0x00ff0000); break;
case green : rgb(0x0000ff00); break;
case blue : rgb(0x000000ff); break;
case white : rgb(0x00ffffff); break;
case orange : rgb(0x00ff7f00); break;
case yellow_dark : rgb(0x00ffaa00); break;
case yellow_medium : rgb(0x00ffd400); break;
case yellow_light : rgb(0x00ffff00); break;
case green_light : rgb(0x0000ffaa); break;
case green_blue1 : rgb(0x0000ffff); break;
case green_blue2 : rgb(0x0000aaff); break;
case green_blue3 : rgb(0x000055ff); break;
case blue_red : rgb(0x00000080); break;
case purple_dark : rgb(0x003f0080); break;
case purple_light : rgb(0x007a00bf); break;
case pink : rgb(0x00ff00ff); break;
}
serial.println(results.value, hex);
serial.println(r, dec);
serial.println(g, dec);
serial.println(b, dec);
setcolor (r, g, b);
}
irrecv.resume(); // receive next value
}
}
void varybrightness (int &color, int increment) {
// adds 10 rgb index unless it's > 255
serial.println("inc value:");
serial.println(increment);
if (increment > 0) {
if (color + increment <= 255) {
color += 10;
}
else {
color = 255;
}
}
// removes 10 rgb index unless it's < 0
else if (increment < 0) {
if (color + increment >= 0) {
color += increment;
}
else {
color = 0;
}
}
}
void rgb(unsigned long value) {
// convert hex rgb
r = value >> 16;
g = (value >> 8) & 0xff;
b = value & 0xff;
}
void setcolor(int red, int green, int blue)
{
//scale values map() r, g, , b brightnesses balanced.
analogwrite(redpin, map(red, 0, 255, 0, max_red));
analogwrite(greenpin, map(green, 0, 255, 0, max_green));
analogwrite(bluepin, map(blue, 0, 255, 0, max_blue));
lcd.home();
lcd.print(" ");
lcd.home();
lcd.print("r" + string(red) + " g" + string(green) + " b" + string(blue));
}
the issue having, , have scratched head on hours now, in varybrightness function, when incrementing brightness up, whole arduino locks up.
it works when it's going down. see no logical reason happening - have tried reversing reference/dereferencing methods no avail, thinking reference related.
i'm uploading arduino uno sdk 1.5.6-r2. i'm hoping can shed light on missing here causing fail!
thanks in advance
not problem but
if (color + increment <= 255) {
color += 10;
presumably should "color += increment".
how know fails inside function? not see "inc value:" being printed?
if start placing debug prints in function narrow problem down single line.
edit:
pretty sure this
should
you passing int receiving pointer int, modifying pointer in func , that's all, in fact can't see how works @ , how doesn't generate error.
hmmm, tried , got error.
______
rob
if (color + increment <= 255) {
color += 10;
presumably should "color += increment".
how know fails inside function? not see "inc value:" being printed?
if start placing debug prints in function narrow problem down single line.
edit:
pretty sure this
code: [select]
varybrightness (r, increment);
...
color += increment;
should
code: [select]
varybrightness (&r, increment);
...
*color += increment;
you passing int receiving pointer int, modifying pointer in func , that's all, in fact can't see how works @ , how doesn't generate error.
hmmm, tried , got error.
______
rob
Arduino Forum > Using Arduino > Programming Questions > Crash when incrementing integer
arduino
Comments
Post a Comment