c++ - 停止伺服while循环
问题描述
我正在构建一个涉及伺服和 adifruit 电路游乐场快递的项目。我的目标是让电路板通过一个灯光序列,让伺服器转 15 度,转回 15 度,然后在 10 秒延迟后重复,同时由 A 按钮启动。这将一直持续到按下 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 按钮状态。
试试这个函数而不是延迟(x):
void my_delay(int x):{
int i=0;
while(!rightButtonPressedB and i<x){
rightButtonPressedB = CircuitPlayground.rightButton();
delay(1);
i++;
}
}
推荐阅读
- powerbi - 适用于 AD 来宾用户的 PowerBI
- uwp - 用于从 UWP 中的 html 字符串中提取子字符串的正则表达式?
- google-kubernetes-engine - configuration-snippet 注释不会将自定义标头添加到响应中
- c - 为什么当我尝试 system("cd PATH"); ,终端无法进入我的路径
- c++ - C++ 中 Boost 库中 cpp_dec_float 的最大值是多少?
- python - QProxyStyle 与 QStyleSheet
- c++ - gluLookAt() 在 OpenGL 中无效
- python - OpenVINO:如何使用推理引擎构建 OpenCV 以启用从模型优化器加载模型
- reactjs - 如何使用 React 创建可再分发的小部件
- python - 按熊猫系列中的第二列排序