mindthegap95:
I see how this combines both positive and negative values in one statement rather than multiple 'IF' statements, however, my only problem is when rolling right, I want LEDs to light up from left-to-right (hence I was using LED 5, 4, 3, 2 and 1) indicating the motion, and when rolling left I want the LEDs to light up from right-to-left (hence I was using LEDs 2, 3, 4, 5 and 6). Whereas this piece of code will light up from left to right in both cases.
No it won't.
I respectfully ask - Have you actually tried the updated code I posted?
Having it this way just makes things clearer as to how the LEDs are being used to explain the roll behaviour but without pulsing but increasing in an increasingly long bar graph as you called it.
So you do want the bar graph effect, or the single illuminate led effect?
And if I was to dip down below a certain range for example from 15º - 25º, to 5º - 15º it would turn off LED[4] as assigned in your example. And this is where my problem is, that's why I would write separate paragraphs for positive values and negative values because I could control particular LEDs.
It may be clearer, but it won't do what you want.
Because you never turn any leds OFF, except for at the end of your code block, where you turn ALL LEDs 1-6 OFF again if roll > -50. That's what's making the pulsing effect happen:Turning LEDs on you want ON, then after a short delay nullifying that by turning them ALL OFF again.
But if you insist on writing separate statements for each LED, then in order to get correct operation, you MUST:
a) Write ONE, and ONLY ONE, condition to turn ON each LED.
b) in the ELSE condition for that LED you must turn the LED OFF.
Unless you do this (one way or another) the code you write is going to have one or more of...
i) IF statements that conflict with each other
ii) LEDs which stay ON when they should not (because you never turn them off)
iii) Pulsing effect because you wrongly turn all/some leds OFF when you need them to stay on
As my last word on this subject I will present you four choice of code. Two for each of graph and single illuminated LED. One coded the compact way, the other entirely equivalent but coded using longhand "if" logic (up to you which you use, but I would encourage you to try both so you can see they work).
// SINGLE LED EFFECT - COMPACT
digitalWrite(ledPins[6], roll <= -45);
digitalWrite(ledPins[5], (roll >= 5 && roll < 15) || (roll <= -35 && roll > -45));
digitalWrite(ledPins[4], (roll >= 15 && roll < 25) || (roll <= -25 && roll > -35));
digitalWrite(ledPins[3], (roll >= 25 && roll < 35) || (roll <= -15 && roll > -25));
digitalWrite(ledPins[2], (roll >= 35 && roll < 45) || (roll <= -5 && roll > -15));
digitalWrite(ledPins[1], roll >= 45);
if (roll >= 45 || roll <= -45)
{
tone(13, 4000, 100);
}
// SINGLE LED EFFECT - IF STATEMENT LONGHAND
if (roll <= -45) {
digitalWrite(ledPins[6], HIGH);
} else {
digitalWrite(ledPins[6], LOW);
}
if ((roll >= 5 && roll < 15) || (roll <= -35 && roll > -45)) {
digitalWrite(ledPins[5], HIGH);
} else {
digitalWrite(ledPins[5], LOW);
}
if ((roll >= 15 && roll < 25) || (roll <= -25 && roll > -35)) {
digitalWrite(ledPins[4], HIGH);
} else {
digitalWrite(ledPins[4], LOW);
}
if ((roll >= 25 && roll < 35) || (roll <= -15 && roll > -25)) {
digitalWrite(ledPins[3], HIGH);
} else {
digitalWrite(ledPins[3], LOW);
}
if ((roll >= 35 && roll < 45) || (roll <= -5 && roll > -15)) {
digitalWrite(ledPins[2], HIGH);
} else {
digitalWrite(ledPins[2], LOW);
}
if (roll >= 45) {
digitalWrite(ledPins[1], HIGH);
} else {
digitalWrite(ledPins[1], LOW);
}
if (roll >= 45 || roll <= -45)
{
tone(13, 4000, 100);
}
// BAR GRAPH EFFECT - COMPACT
digitalWrite(ledPins[6], roll <= -45);
digitalWrite(ledPins[5], roll >= 5 || roll <= -35);
digitalWrite(ledPins[4], roll >= 15 || roll <= -25);
digitalWrite(ledPins[3], roll >= 25 || roll <= -15);
digitalWrite(ledPins[2], roll >= 35 || roll <= -5);
digitalWrite(ledPins[1], roll >= 45);
if (roll >= 45 || roll <= -45)
{
tone(13, 4000, 100);
}
// BAR GRAPH EFFECT - IF STATEMENT LONGHAND
if (roll <= -45) {
digitalWrite(ledPins[6], HIGH);
} else {
digitalWrite(ledPins[6], LOW);
}
if (roll >= 5 || roll <= -35) {
digitalWrite(ledPins[5], HIGH);
} else {
digitalWrite(ledPins[5], LOW);
}
if (roll >= 15 || roll <= -25) {
digitalWrite(ledPins[4], HIGH);
} else {
digitalWrite(ledPins[4], LOW);
}
if (roll >= 25 || roll <= -15) {
digitalWrite(ledPins[3], HIGH);
} else {
digitalWrite(ledPins[3], LOW);
}
if (roll >= 35 || roll <= -5) {
digitalWrite(ledPins[2], HIGH);
} else {
digitalWrite(ledPins[2], LOW);
}
if (roll >= 45) {
digitalWrite(ledPins[1], HIGH);
} else {
digitalWrite(ledPins[1], LOW);
}
if (roll >= 45 || roll <= -45)
{
tone(13, 4000, 100);
}