Robofun 機器人論壇

標題: swich case 問題 [打印本頁]

作者: j246810j    時間: 2015-10-6 17:50
標題: swich case 問題
本帖最後由 j246810j 於 2015-10-6 17:52 編輯

使用swich case 他會一直執行case3 無法跳出  我的目標是想要無窮迴圈 也就是1→2→3後再重來一遍 有高手可以幫我看看ㄇ



unsigned long previous_time;
int latchPin =3;
int clockPin =2;
int dataPin =4 ;
#define ULONG_MAX 0xffffffffL
#define latchPin2 A1
#define clockPin2 A0
#define dataPin2 A2

volatile unsigned long Count = 0;
volatile unsigned long Count2 = 0;
volatile unsigned long Count3 = 0;

unsigned long current_time = millis();
  unsigned long time_passed;
  int flag=1;
void setup() {
// 將 latchPin, clockPin, dataPin 設置為輸出
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  pinMode(latchPin2, OUTPUT);
  pinMode(clockPin2, OUTPUT);
  pinMode(dataPin2, OUTPUT);

previous_time = millis();

Serial.begin(9600);


}


void update2(unsigned long time_passed){
  static unsigned long time = 0;
  time += time_passed;
  if((time / 200) % 2 == 0){ // 第一組的LED明滅頻率為0.2秒
       digitalWrite(latchPin, LOW);
     shiftOut(dataPin, clockPin, MSBFIRST, 1);
        digitalWrite(latchPin,  HIGH);
  }
  else{
       digitalWrite(latchPin,  LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 0);
       digitalWrite(latchPin,  HIGH);
  }
}

void update3(unsigned long time_passed){
  static unsigned long time = 0;
  time += time_passed;
  if((time / 300) % 2 == 0){ // 第二組LED明滅頻率為0.3秒
     digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, 1);
      digitalWrite(latchPin2,  HIGH);
  }
  else{
      digitalWrite(latchPin2, LOW);
    shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
       digitalWrite(latchPin2,  HIGH);
  }
}

void update4(unsigned long time_passed){
  static unsigned long time = 0;
  time += time_passed;
  if((time / 200) % 2 == 0){ // 第一組的LED明滅頻率為0.2秒
       digitalWrite(latchPin, LOW);
     shiftOut(dataPin, clockPin, MSBFIRST, 128);
        digitalWrite(latchPin,  HIGH);
  }
  else{
       digitalWrite(latchPin,  LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 0);
       digitalWrite(latchPin,  HIGH);
  }
}

void update5(unsigned long time_passed){
  static unsigned long time = 0;
  time += time_passed;
  if((time / 300) % 2 == 0){ // 第二組LED明滅頻率為0.3秒
     digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST,128);
      digitalWrite(latchPin2,  HIGH);
  }
  else{
      digitalWrite(latchPin2, LOW);
    shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
       digitalWrite(latchPin2,  HIGH);
  }
}

void update6(unsigned long time_passed){
  static unsigned long time = 0;
  time += time_passed;
  if((time / 200) % 2 == 0){ // 第一組的LED明滅頻率為0.2秒
       digitalWrite(latchPin, LOW);
     shiftOut(dataPin, clockPin, MSBFIRST, 1);
        digitalWrite(latchPin,  HIGH);
  }
  else{
       digitalWrite(latchPin,  LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 0);
       digitalWrite(latchPin,  HIGH);
  }
}

void update7(unsigned long time_passed){
  static unsigned long time = 0;
  time += time_passed;
  if((time / 300) % 2 == 0){ // 第二組LED明滅頻率為0.3秒
     digitalWrite(latchPin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, 1);
      digitalWrite(latchPin2,  HIGH);
  }
  else{
      digitalWrite(latchPin2, LOW);
    shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
       digitalWrite(latchPin2,  HIGH);
  }
}

void Led1(){
  if(current_time > previous_time ){
    time_passed = current_time - previous_time;
  }
  else{
    time_passed = ULONG_MAX - previous_time + current_time;
  }
   if(Count >= 5000) {
       digitalWrite(latchPin2, LOW);
       shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
       digitalWrite(latchPin2,  HIGH);
        digitalWrite(latchPin, LOW);
       shiftOut(dataPin, clockPin, MSBFIRST, 0);
       digitalWrite(latchPin,  HIGH);
       flag=2;
    }
    else {   
       if(time_passed >= 100) {//每次在loop裡,檢查距離上次更新是否經過了100ms(0.1秒),若是,呼叫update2、update3
     ++Count;
      update2(time_passed);
      update3(time_passed);
             }
              previous_time = current_time;
    }
  }

void Led2(){
  if(current_time > previous_time ){
    time_passed = current_time - previous_time;
  }
  else{
    time_passed = ULONG_MAX - previous_time + current_time;
  }
  if(Count2 >= 5000) {
       digitalWrite(latchPin2, LOW);
       shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
       digitalWrite(latchPin2,  HIGH);
        digitalWrite(latchPin, LOW);
       shiftOut(dataPin, clockPin, MSBFIRST, 0);
       digitalWrite(latchPin,  HIGH);
           flag=3;

   }
    else {
       if(time_passed >= 100) {
      ++Count2;
      update4(time_passed);
      update5(time_passed);
               }
          previous_time = current_time;
    }

  }
void Led3(){
  if(current_time > previous_time ){
    time_passed = current_time - previous_time;
  }
  else{
    time_passed = ULONG_MAX - previous_time + current_time;
  }
   if(Count3 >= 5000) {
       digitalWrite(latchPin2, LOW);
       shiftOut(dataPin2, clockPin2, MSBFIRST, 0);
       digitalWrite(latchPin2,  HIGH);
        digitalWrite(latchPin, LOW);
       shiftOut(dataPin, clockPin, MSBFIRST, 0);
       digitalWrite(latchPin,  HIGH);  
      com=2;
    }
    else {
       if(time_passed >= 100) {
      ++Count3;
      update6(time_passed);
      update7(time_passed);
               }
      previous_time = current_time;
    }
  }

void loop() {

  switch(flag) {
case 1:
    Led1();
    break;
case 2:
    Led2();
    break;
case 3:
    Led3();
     break;     

}


}
作者: 超新手    時間: 2015-10-7 08:11
你的 current_time 只有開機給初始值一次
應該在 LOOP 中, 每次都要讀一次目前的時間才對吧?
void loop() {
current_time = millis();
switch(flag) {
case 1:
作者: j246810j    時間: 2015-10-8 15:41
感謝大大的幫忙 發覺只要把Count1 Count2 Count3做個歸零就會開始迴圈了




歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) Powered by Discuz! X3.2