I am building a project that involves a servo and an adifruit circuit playground express. My goal is to have the board go through a light sequence, have the servo turn 15 degrees, turn back 15 degrees and then repeat after a 10 second delay while being initiated by the A button. This will go until the B button is pressed essentially breaking the while loop that was entered with the press of the A button. I've thought of having the while loop check for if a value is still considered 'true' each time it loops which led me to believe I should try a 'switch-case' idea but I was not sure what to put in the switch(). Here is some of what I have. It's only 2 IF statements but the 'buttonPressedA' if statement runs infinitely without the possibility of breaking out of it. Help with this would be really appreciated.
// and here, we, go. (just like the joker says it)
#include <Adafruit_CircuitPlayground.h>
#include <Servo.h> // servo library
#define NEOPIX_PIN A2
#define NUM_PIXELS 5
Servo nohands; // creating an object so reference the servo library, no hands. get it. cuz you're not using hands... alright that's it.
int pos = 15; // varible to store the servo position. ALSO HAS THE START POSITION FOR THE SERVO
bool leftButtonPressedA;
bool rightButtonPressedB;
uint8_t pixeln = 0;
Adafruit_CPlay_NeoPixel strip = Adafruit_CPlay_NeoPixel(NUM_PIXELS, NEOPIX_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.println("Don't touch the wheel. I've got it from here!");
Serial.begin(9600);
CircuitPlayground.begin();
strip.begin();
nohands.attach(A7); //we are looking for pin A7
nohands.write(0); // setting the pos to 0
}
void loop() {
// put your main code here, to run repeatedly:
leftButtonPressedA = CircuitPlayground.leftButton();
rightButtonPressedB = CircuitPlayground.rightButton();
CircuitPlayground.clearPixels();
//*------------------------------------------------------------------------------------------------------
if (leftButtonPressedA) {
while(1) { // while loop to run infinite
delay(100); // will delay the timing of the loop by intervals of 10 seconds
Serial.println("GO");
strip.clear();
strip.show();
delay(1000); // will delay 1 second before initiallizing the light sequence
CircuitPlayground.setPixelColor(0,40,40,0);
delay(200); // will delay the progression by x ms (*10^-3sec) before moving onto the next one.
CircuitPlayground.setPixelColor(1,40,40,0);
delay(200);
CircuitPlayground.setPixelColor(2,40,40,0);
delay(200);
CircuitPlayground.setPixelColor(3,40,40,0);
delay(200);
CircuitPlayground.setPixelColor(4,40,40,0);
delay(100);
CircuitPlayground.setPixelColor(5,40,40,0); //lights
delay(100);
CircuitPlayground.setPixelColor(6,40,40,0);
delay(100);
CircuitPlayground.setPixelColor(7,40,40,0);
delay(100);
CircuitPlayground.setPixelColor(8,40,40,0);
delay(100);
CircuitPlayground.setPixelColor(9,40,40,0);
delay(100);
strip.show();
strip.clear();
strip.show();
CircuitPlayground.setPixelColor(0,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(1,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(2,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(3,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(4,0,0,0); //lights
delay(25);
CircuitPlayground.setPixelColor(5,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(6,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(7,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(8,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(9,0,0,0);
delay(25);
strip.show();
strip.clear();
strip.show();
CircuitPlayground.setPixelColor(0,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(1,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(2,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(3,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(4,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(5,0,200,0); //lights
delay(0.5);
CircuitPlayground.setPixelColor(6,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(7,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(8,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(9,0,200,0);
delay(0.5);
CircuitPlayground.playTone(500 + pixeln * 500, 100);
CircuitPlayground.playTone(750 + pixeln * 750, 100);
CircuitPlayground.playTone(1000 + pixeln * 200, 100);
/*delay(2000); // will shwo the lights all for a total of 5 seconds before it goes back to reloading */
strip.clear();
//*------------------------------------------------------------------------------------------------------
//beginning of servo for loop
for (pos = 0; pos <=15; pos += 3) {// set to 15 degrees
//pos+= is stepping in intervals of 1 degree
nohands.write(pos);
delay(150);
}
for (pos = 15; pos >= 0 ; pos -= 3){ // set to 15 degrees
nohands.write(pos); //adding while loop will have the entire think loop go for ever.
delay(150);
}
// end of GO
}
}
//*------------------------------------------------------------------------------------------------------
if (rightButtonPressedB) {
Serial.println("STOP");
strip.show();
delay(100); // will delay 1 second before initiallizing the light sequence
CircuitPlayground.setPixelColor(9,150,0,0);
delay(50); // will delay the progression by x ms (*10^-3sec) before moving onto the next one.
CircuitPlayground.setPixelColor(8,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(7,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(6,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(5,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(4,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(3,150,0,0); //lights
delay(50);
CircuitPlayground.setPixelColor(2,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(1,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(0,150,0,0);
delay(50);
strip.show();
strip.clear();
strip.show();
CircuitPlayground.setPixelColor(9,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(8,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(7,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(6,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(5,0,0,0); //lights
delay(25);
CircuitPlayground.setPixelColor(4,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(3,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(2,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(1,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(0,0,0,0);
delay(25);
strip.clear();
strip.show();
CircuitPlayground.setPixelColor(9,255,0,0);
delay(50); // will delay the progression by x ms (*10^-3sec) before moving onto the next one.
CircuitPlayground.setPixelColor(8,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(7,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(6,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(5,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(4,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(3,255,0,0); //lights
delay(50);
CircuitPlayground.setPixelColor(2,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(1,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(0,255,0,0);
delay(50);
CircuitPlayground.playTone(1000 + pixeln * 200, 100);
CircuitPlayground.playTone(750 + pixeln * 750, 100);
CircuitPlayground.playTone(500 + pixeln * 500, 100);
delay(2000); // will show the lights all for a total of 5 seconds before it goes back to reloading
//*------------------------------------------------------------------------------------------------------
} // end of STOP
} `// and here, we, go. (just like the joker says it)
#include <Adafruit_CircuitPlayground.h>
#include <Servo.h> // servo library
#define NEOPIX_PIN A2
#define NUM_PIXELS 5
Servo nohands; // creating an object so reference the servo library, no hands. get it. cuz you're not using hands... alright that's it.
int pos = 15; // varible to store the servo position. ALSO HAS THE START POSITION FOR THE SERVO
bool leftButtonPressedA;
bool rightButtonPressedB;
uint8_t pixeln = 0;
Adafruit_CPlay_NeoPixel strip = Adafruit_CPlay_NeoPixel(NUM_PIXELS, NEOPIX_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.println("Don't touch the wheel. I've got it from here!");
Serial.begin(9600);
CircuitPlayground.begin();
strip.begin();
nohands.attach(A7); //we are looking for pin A7
nohands.write(0); // setting the pos to 0
}
void loop() {
// put your main code here, to run repeatedly:
leftButtonPressedA = CircuitPlayground.leftButton();
rightButtonPressedB = CircuitPlayground.rightButton();
CircuitPlayground.clearPixels();
//*------------------------------------------------------------------------------------------------------
if (leftButtonPressedA) {
while(1) { // while loop to run infinite
delay(100); // will delay the timing of the loop by intervals of 10 seconds
Serial.println("GO");
strip.clear();
strip.show();
delay(1000); // will delay 1 second before initiallizing the light sequence
CircuitPlayground.setPixelColor(0,40,40,0);
delay(200); // will delay the progression by x ms (*10^-3sec) before moving onto the next one.
CircuitPlayground.setPixelColor(1,40,40,0);
delay(200);
CircuitPlayground.setPixelColor(2,40,40,0);
delay(200);
CircuitPlayground.setPixelColor(3,40,40,0);
delay(200);
CircuitPlayground.setPixelColor(4,40,40,0);
delay(100);
CircuitPlayground.setPixelColor(5,40,40,0); //lights
delay(100);
CircuitPlayground.setPixelColor(6,40,40,0);
delay(100);
CircuitPlayground.setPixelColor(7,40,40,0);
delay(100);
CircuitPlayground.setPixelColor(8,40,40,0);
delay(100);
CircuitPlayground.setPixelColor(9,40,40,0);
delay(100);
strip.show();
strip.clear();
strip.show();
CircuitPlayground.setPixelColor(0,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(1,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(2,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(3,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(4,0,0,0); //lights
delay(25);
CircuitPlayground.setPixelColor(5,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(6,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(7,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(8,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(9,0,0,0);
delay(25);
strip.show();
strip.clear();
strip.show();
CircuitPlayground.setPixelColor(0,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(1,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(2,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(3,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(4,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(5,0,200,0); //lights
delay(0.5);
CircuitPlayground.setPixelColor(6,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(7,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(8,0,200,0);
delay(0.5);
CircuitPlayground.setPixelColor(9,0,200,0);
delay(0.5);
CircuitPlayground.playTone(500 + pixeln * 500, 100);
CircuitPlayground.playTone(750 + pixeln * 750, 100);
CircuitPlayground.playTone(1000 + pixeln * 200, 100);
/*delay(2000); // will shwo the lights all for a total of 5 seconds before it goes back to reloading */
strip.clear();
//*------------------------------------------------------------------------------------------------------
//beginning of servo for loop
for (pos = 0; pos <=15; pos += 3) {// set to 15 degrees
//pos+= is stepping in intervals of 1 degree
nohands.write(pos);
delay(150);
}
for (pos = 15; pos >= 0 ; pos -= 3){ // set to 15 degrees
nohands.write(pos); //adding while loop will have the entire think loop go for ever.
delay(150);
}
// end of GO
}
}
//*------------------------------------------------------------------------------------------------------
if (rightButtonPressedB) {
Serial.println("STOP");
strip.show();
delay(100); // will delay 1 second before initiallizing the light sequence
CircuitPlayground.setPixelColor(9,150,0,0);
delay(50); // will delay the progression by x ms (*10^-3sec) before moving onto the next one.
CircuitPlayground.setPixelColor(8,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(7,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(6,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(5,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(4,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(3,150,0,0); //lights
delay(50);
CircuitPlayground.setPixelColor(2,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(1,150,0,0);
delay(50);
CircuitPlayground.setPixelColor(0,150,0,0);
delay(50);
strip.show();
strip.clear();
strip.show();
CircuitPlayground.setPixelColor(9,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(8,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(7,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(6,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(5,0,0,0); //lights
delay(25);
CircuitPlayground.setPixelColor(4,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(3,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(2,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(1,0,0,0);
delay(25);
CircuitPlayground.setPixelColor(0,0,0,0);
delay(25);
strip.clear();
strip.show();
CircuitPlayground.setPixelColor(9,255,0,0);
delay(50); // will delay the progression by x ms (*10^-3sec) before moving onto the next one.
CircuitPlayground.setPixelColor(8,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(7,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(6,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(5,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(4,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(3,255,0,0); //lights
delay(50);
CircuitPlayground.setPixelColor(2,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(1,255,0,0);
delay(50);
CircuitPlayground.setPixelColor(0,255,0,0);
delay(50);
CircuitPlayground.playTone(1000 + pixeln * 200, 100);
CircuitPlayground.playTone(750 + pixeln * 750, 100);
CircuitPlayground.playTone(500 + pixeln * 500, 100);
delay(2000); // will show the lights all for a total of 5 seconds before it goes back to reloading
//*------------------------------------------------------------------------------------------------------
} // end of STOP
}
attention to the fact that when you press B the microcontroller is executing one of the delay lines, therefore doesn't check the if(rightButtonPressedB) condition nor B button state.
Try this function instead of delay(x):
void my_delay(int x):{
int i=0;
while(!rightButtonPressedB and i<x){
rightButtonPressedB = CircuitPlayground.rightButton();
delay(1);
i++;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.