I’m building an interactive moodtoy for my gf. It’s my first more elaborate project and now I have totally hit the wall.
If I run all the loops separately they work fine but when I put them together all i get is a blinking rgbLED that don’t follow any of the set parameters for any of the loops.
Sorry for the long sketch but it is only when I run the full sketch that the problem(s?) appear.
Probably more than one error in my code but it’s my first more complex sketch so have patients plz
//LEDpins
const int redLED = 9;
const int greenLED = 10;
const int blueLED = 11;
//LED info
int redLevel = 0;
int greenLevel = 0;
int blueLevel = 0;
float counter = 0;
float pi = 3.14159;
//SENSORpins
const int HALL = 2;
const int BALL = 3;
const int LIGHT = 4;
//other ints
int randomValue = 0;
int brightness = 0;
int fadeAmount = 5;
void setup(){
//LEDpins
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
//Sensors
pinMode(HALL, INPUT);
pinMode(BALL, INPUT);
pinMode(LIGHT, INPUT);
}
//Start loop
void loop() {
while (true) {
MOOD();
}
}
//Set the mood to 1 out of 4
void MOOD() {
// chance % for the moods
int MOOD = random(100);
if (MOOD <=3) {
ANGRY();
}
if (MOOD > 3 && MOOD <= 10) {
CALM();
}
if (MOOD > 10 && MOOD <= 20) {
PLAY();
}
else
{
blendColors();
}
}
//Main mood, should run 3 times and then go back to MOOD() but havent found out how to make it work yet.
//I thought Arduino should get stuck here due to no way out of loop but it doesn't execute it as in the code (just fast, random blink without the color change effekt).
void blendColors() {
// code for blendColors here
counter = counter + 1;
redLevel = sin(counter/100)*1000;
greenLevel = sin(counter/100 + pi*2/3)*1000;
blueLevel = sin(counter/100 + pi*4/3)*1000;
redLevel = map(redLevel,-1000,1000,0,100);
greenLevel = map(greenLevel,-1000,1000,0,100);
blueLevel = map(blueLevel,-1000,1000,0,100);
analogWrite(redLED,redLevel);
analogWrite(greenLED,greenLevel);
analogWrite(blueLED,blueLevel);
delay(10);
}
// Angry MOOD, loop redLED fade util simultaneous input from two sensors = back to MOOD()
void ANGRY() {
// code for ANGRY here
// set the brightness of pin RED:
analogWrite(redLED, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
if (digitalRead (BALL) == HIGH && digitalRead (LIGHT) == HIGH)
{
MOOD();
}
}
// Same as ANGRY but with one sensor.
void CALM() {
// code for CALM here
analogWrite(greenLED, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
if (digitalRead (BALL) == HIGH)
{
MOOD();
}
}
//Same as green
void PLAY()
{
// code for PLAY here
analogWrite(blueLED, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255)
{
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
if (digitalRead (HALL) == HIGH)
{
MOOD();
}
}
I’ve spent 2 days on google trying to find a solution but still learning and it’s hard to find stuff you don’t know what it’s called (i.e. functions etc).
Hopefully I haven’t F-ed it from line one…