Arduino NANO control RGB LED-Stripes with BMA020 sensor

Hello community,

I have a project within I want to control the colour of the LED-Stripe with the data from the BMA020 sensor. The 3d location of the BMA020 sensor is transferred in decimals. Then I want to connect the driving of the LED-Stipes with the data of the sensor. I just want to use the X axis data to change the colour of the stripe. So the analogWrite function, has the pin and a value between 0-255 as variables. There should be a complete chromatic circle when you turn around the BMA020 sensor. So the question is if this programm structure could work like this (does not work til now). There is a calculation of the r,g or b value in the if structure (could there be a mistake?). I want a chromatic circle like this: blue > violett > red > yellow > green > green-blue > blue. For example the blue color is: 0,0,255 (r,g,b).

Thanks a lot for your answers!

Lukas

#include <Wire.h>

//Definitionen Auslesung BMA020
#define AccelerometerAddress 0x38
#define Axis_X_Out1 0x02
#define Axis_X_Out2 0x03
#define Axis_Y_Out1 0x04
#define Axis_Y_Out2 0x05
#define Axis_Z_Out1 0x06
#define Axis_Z_Out2 0x07

//Definitionen Ansteuerung LED-Stripe
#define REDPIN 10
#define GREENPIN 9
#define BLUEPIN 11

#define FADESPEED 5

short shiftValueX = 0;
short shiftValueY = 0;
short shiftValueZ = 0;

boolean firstLoop = true;

void setup()
{
Wire.begin();
Serial.begin(9600); //normal (9600)

pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
}

void loop()
{
double r, g, b;

//ABFRAGE BMA020
if (true)
{
byte axisX1 = readAcceleration(Axis_X_Out1);
byte axisX2 = readAcceleration(Axis_X_Out2);

byte axisY1 = readAcceleration(Axis_Y_Out1);
byte axisY2 = readAcceleration(Axis_Y_Out2);

byte axisZ1 = readAcceleration(Axis_Z_Out1);
byte axisZ2 = readAcceleration(Axis_Z_Out2);

short resultX = (int)(axisX2 << 8 | axisX1);
short resultY = (int)(axisY2 << 8 | axisY1);
short resultZ = (int)(axisZ2 << 8 | axisZ1);

if (firstLoop) {
shiftValueX = resultX;
shiftValueY = resultY;
shiftValueZ = resultZ;
firstLoop = false;
}
else {
resultX = resultX - shiftValueX;
resultY = resultY - shiftValueY;
resultZ = resultZ - shiftValueZ;
}

Serial.print("X:");
Serial.print(resultX, DEC);
Serial.print(";Y:");
Serial.print(resultY, DEC);
Serial.print(";Z:");
Serial.print(resultZ, DEC);
Serial.println(";");

//LED-ANSTEUERUNG

if ((resultX > -15700) && (resultX < -10584))
{
g = 0;
b = 255;
r = resultX * 0,05 + 770;
analogWrite(REDPIN, r);
analogWrite(GREENPIN, g);
analogWrite(BLUEPIN, b);
delay(20);
}
else if((resultX > -10584) && (resultX < -5468))
{
r = 255;
g = 0;
b = resultX * -0,06 - 273;
analogWrite(REDPIN, r);
analogWrite(GREENPIN, g);
analogWrite(BLUEPIN, b);
delay(20);
}
else if((resultX > -5468) && (resultX < -352))
{
r = 255;
b = 0;
g = resultX * 0,05 + 273;
analogWrite(REDPIN, r);
analogWrite(GREENPIN, g);
analogWrite(BLUEPIN, b);
delay(20);
}

else if((resultX > -352) && (resultX < 4764)) {
g = 255;
b = 0;
r = -0,05 * resultX - 238;
analogWrite(REDPIN, r);
analogWrite(GREENPIN, g);
analogWrite(BLUEPIN, b);
delay(20);
}
else if((resultX > 4764) && (resultX < 9880)) {
r = 0;
g = -0,023 * resultX + 364, 5;
b = 0,027 * resultX - 129, 5;
analogWrite(REDPIN, r);
analogWrite(GREENPIN, g);
analogWrite(BLUEPIN, b);
delay(20);
}
else if((resultX > 9880) && (resultX < 15000)) {
r = 0;
g = -0,026 * resultX + 392, 5;
b = 0, 0226 * resultX - 84, 9;
analogWrite(REDPIN, r);
analogWrite(GREENPIN, g);
analogWrite(BLUEPIN, b);
delay(20);
}
}

}

byte readAcceleration(byte address)
{
byte val = 0x00;
Wire.beginTransmission(AccelerometerAddress);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(AccelerometerAddress, 1);
val = Wire.read();
return val;
}

if (true)
  {

Running an infinite loop inside an infinite loop() is stupid. Stop doing that.

What, exactly, does the code do?
How, exactly, does that differ from what you want?

There should be a complete chromatic circle when you turn around the BMA020 sensor.

Based on what data obtained from the sensor? How do you define "a complete chromatic circle"?

Thank you for the quick answer,

the code reads the data from the sensor with the readAccerleration function, and then the binary values are transferred in decimal. The location value is then stored in die variable resultX. Then we use the resultX value to calculate the colour. The idea is that when we turn the sensor, the colour changes of the led stripe. With chromatic circle I mean the colours: blue > violett > red > yellow > green > green-blue > blue. Blue is 0,0,255 (r,g,b), violett is 255,0,255 (r,g,b). So to get the colour from Blue to violett, I have to enlarge the r-value.

The problem I think, is that the calculation of the Values for driving the stripe is interruptet. The values are not calculated in a loop. So I just see red green and blue color on the stripe when i turn the sensor.

Could it be a wrong structure for calculating the values?

and then the binary values are transferred in decimal.

No idea what that means, or why you think that "binary values in decimal" are somehow useful.

b = resultX * -0,06 - 273;

What is that comma doing there? The C/C++ programming language does not recognize -0,06 as 6 hundredths. It invokes the comma operator which most assuredly is NOT what you want.

What I forgot to say is that the resultX values are from -15700 to 15000, so I made 6 areas with the if structures where I have the different colors. And in the if structure, there is the calculation to adjust the colour, when you go in the next area.

And in the if structure, there is the calculation to adjust the colour, when you go in the next area.

All the if blocks are full of code that is not doing what you seem to think it does. Print the results of the calculations for r, g, and b, to see for yourself.