繁体   English   中英

停止伺服while循环

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM