# Arduino Nano + RGB led + adxl335

#### mollekake

Jun 30, 2014
37
Hello!
So i've been fidling around with my arduino, a rgb led and an accelerometer.
I've tried out some tap detection, and got it to work, and decided i wanted to do something with it.
So i hooked up an RGB led, and am trying to make it change color for each tap.
The tap detection works, but the led colors are all wrong :S

Here is my code:
Code:
// these constants describe the pins. They won't change:
const int xpin = A2;                  // x-axis of the accelerometer
const int ypin = A3;                  // y-axis
const int zpin = A4;                  // z-axis (only on 3-axis models)

const int red = 9;
const int green = 10;
const int blue = 11;

//int sampleDelay = 50;   //number of milliseconds between readings

void setup()
{
// initialize the serial communications:
Serial.begin(9600);

//Make sure the analog-to-digital converter takes its reference voltage from
// the AREF pin
analogReference(EXTERNAL);

pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(zpin, INPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
}

int x = 0;
int y = 0;
int z = 0;
int prex = 0;
int prey = 0;
int prez = 0;

int redVal = 0;
int greenVal = 0;
int blueVal = 0;
boolean redb = false;
boolean greenb = false;
boolean blueb = false;

void loop()
{
prex = x;
prey = y;
prez = z;

Serial.print("\t");
//do this but haven't tested the importance
delay(1);
Serial.print("\t");
//do this but haven't tested the importance
delay(1);
Serial.print("\n");  // delay before next reading:
//delay(sampleDelay);
delay(1);

delay(1);
delay(1);

//Detect if the difference between last reading and current
//reading is too far apart, tap occured
//Tap detection is quite sensive, too sensitive?
if(
(x > (prex + 10) || x < (prex - 10)) ||
(y > (prey + 10) || y < (prey - 10)) ||
(z > (prez + 10) || z < (prez - 10))
){
Serial.print("TAP!!");
Serial.println();

//Switch color
if(redb){
redVal = 255;
greenVal = 0;
blueVal = 0;
redb = false;
greenb = true;
}else if(greenb){
redVal = 0;
greenVal = 255;
blueVal = 0;
greenb = false;
blueb = true;
}else if(blueb){
redVal = 0;
greenVal = 0;
blueVal = 255;
blueb = false;
redb = true;
}else{
redVal = 255;
greenVal = 0;
blueVal = 0;
greenb = true;
}
delay(1);
analogWrite(red, redVal);
analogWrite(green, greenVal);
analogWrite(blue, blueVal);
//END of color switch

delay(150);//Delay to prevent false tap detection
delay(1);
delay(1);
}
//End of tap detection

delay(10);
}

What happens is that when i write the program, the Led starts out lighting up BOTH green and blue. When i TAP it changes to RED. When i tap again, it is still RED. When i tap again, it goes to BOTH green and blue.
I can't see why :S
Halp?

#### mollekake

Jun 30, 2014
37
Figured it out after a lot of frustration. I did it the opposite of correct.
In order to light up blue, red and green must be at 255 and blue at 0. Green is red and blue 255, and green 0, and so on.

Replies
3
Views
943
Replies
8
Views
769
Replies
5
Views
3K
Replies
5
Views
1K
Replies
0
Views
2K