[英]Stopping a servo while loop with
我正在構建一個涉及伺服和 adifruit 電路游樂場快遞的項目。 我的目標是讓板子 go 通過一個光序列,讓伺服器轉動 15 度,再轉回 15 度,然后在 A 按鈕啟動時延遲 10 秒后重復。 這將 go 直到按下 B 按鈕,基本上打破了按下 A 按鈕進入的 while 循環。 我曾想過讓 while 循環檢查一個值是否在每次循環時仍然被認為是“真”,這讓我相信我應該嘗試一個“開關案例”的想法,但我不確定在開關中放什么()。 這是我所擁有的一些東西。 它只有 2 條 IF 語句,但 'buttonPressedA' if 語句無限運行,沒有中斷的可能性。 對此的幫助將不勝感激。
// 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
}
請注意,當您按下B時,微控制器正在執行其中一條延遲線,因此不檢查if(rightButtonPressedB)條件或 B 按鈕 state。
試試這個 function 而不是延遲(x):
void my_delay(int x):{
int i=0;
while(!rightButtonPressedB and i<x){
rightButtonPressedB = CircuitPlayground.rightButton();
delay(1);
i++;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.