Arduino-通过按钮中断循环

时间:2018-12-19 01:25:56

标签: for-loop arduino

我到处都是,找不到问题的答案,所以我在这里是最后的选择,因为我正在拔头发。我对Arduino相当陌生,我自己已经做到了这一点,但是现在我真的可以提供一些帮助。

我正在尝试制作一个具有3个不同“程序”的闪烁电路,我有一个LCD和一个按钮。当我按下按钮时,它将在液晶显示屏上滚动3个不同的程序。这是,或工作正常。

我开始创建闪烁程序本身。我遵循了教程,并根据自己的喜好更改了一些参数。当前,它附加到p1(程序1)上,并在其自己的称为PowerSurge()的无效函数中。闪烁程序也可以正常运行,但是现在它阻止了我的按钮按下。我已经尝试了很多不同的方法,例如for循环中的if else语句,但是没有用。

看看代码:

 #include <avr/pgmspace.h> // required for storing flicker dimmer levels table in PROGMEM
#include <LiquidCrystal.h> // required for LCD display
#include <Bounce2.h> // required for debounce

#define ON HIGH // define flicker outport ON
#define OFF LOW // define flicker outport OFF

Bounce debouncer = Bounce();

const uint8_t OUTPORT1 = 13; // Pin out for Dimmer Gate
// Add more channel port defs here for multiple chanels.
const uint8_t STATPORT = 3;  // used for checking zero crossing on dimmer
const uint8_t REPEATS = 8; // used for dimmer

const uint8_t CHANNEL_1_TABLE[] PROGMEM =
  {
  15, 14, 13, 12, 11, 9 , 9 , 9 ,   11, 12, 11, 12, 13, 14, 15, 15,     // 1
  14, 10, 10, 11, 12, 10, 11, 12,   12, 13, 14, 7 , 8 , 9 , 10, 9 ,
  8 , 9 , 10, 5 , 6 , 7 , 9 , 9 ,   9 , 9 , 9 , 9 , 9 , 8 , 7 , 6 ,
  4 , 5 , 4 , 3 , 3 , 0 , 0 , 10,   0 , 1 , 2 , 2 , 3 , 2 , 3 , 4 ,

  13, 14, 15, 13, 15, 14, 14, 14,   13, 12, 13, 12, 11, 10, 9 , 0 ,     // 2
  1 , 2 , 3 , 3 , 4 , 5 , 10, 5 ,   5 , 6 , 7 , 8 , 9 , 10, 10, 9 ,
  9 , 10, 11, 12, 13, 14, 15, 15,   15, 15, 15, 5 , 6 , 5 , 15, 14,
  13, 12, 13, 12, 11, 10, 11, 5 ,   7 , 5 , 6 , 5 , 7 , 3 , 4 , 3 ,

  4 , 5 , 5 , 5 , 6 , 7 , 15, 13,   14, 14, 14, 15, 13, 14, 15, 12,     // 3
  13, 11, 11, 0 , 0 , 2 , 3 , 2 ,   9 , 10, 11, 12, 13, 13, 15, 15,
  15, 15, 15, 15, 15, 15, 13, 15,   13, 15, 2 , 3 , 2 , 4 , 2 , 13,
  12, 11, 10, 10, 9 , 8 , 10, 11,   12, 12, 12, 15, 13, 15, 15, 14,

  15, 14, 13, 9 , 7 , 7 , 7 , 5 ,   4 , 3 , 2 , 1 , 4 , 3 , 2 , 3 ,     // 4
  4 , 0 , 0 , 2 , 0 , 1 , 1 , 2 ,   3 , 4 , 5 , 6 , 7 , 7 , 7 , 3 ,
  7 , 9 , 8 , 8 , 15, 15, 15, 13,   14, 13, 2 , 0 , 0 , 0 , 1 , 2 ,
  3 , 2 , 3 , 2 , 5 , 4 , 7 , 8 ,   9 , 9 , 9 , 15, 14, 10, 15, 15,

   15, 15, 15, 0,15, 2,3, 14,  13, 10,13, 12,11,11,11, 12,     // 5
  13,14,13, 12,14, 7,14, 7,  14, 15, 14, 14, 14, 8, 7, 8,
   7, 2, 7, 4, 7, 5, 7, 7,   9, 8,11,12,13, 14,15, 15,
  13, 14,11, 7, 9, 5, 5, 7,   9, 10,11, 12,13, 5,15, 15,

  15, 9,15, 9,13, 9,13, 9,  8, 7,6, 7, 6, 5, 0, 5,     // 6
   4, 2, 3, 2, 8, 9, 8, 9,   10, 11,11, 12,13, 9,15, 9,
  15, 15,13, 14,15, 14, 10, 15,   13, 14, 14, 12, 15,14, 2, 5,
   4, 3, 2, 1,7, 6,7, 0,  5, 5,6, 7,8, 9,15, 9,

  13, 12,13, 9,15, 15,15, 15,   15, 15, 15, 15, 10,15, 14, 14,     // 7
   10, 14, 13, 12, 11, 10, 13, 14,  15, 2, 15, 14, 15, 14, 2, 3,
   2,0, 1, 2, 3, 4,5, 4, 3, 14,13, 2,15,13,10,2,
 3, 4,3, 0,0,0,3, 4,   4, 4, 4, 3, 4, 1, 5, 6,

   7, 6, 7, 6, 5, 4, 3, 2,   1, 2,1, 1,0, 1,7, 3,     // 8
  15, 13,3, 4,5, 5, 7, 6,   5, 4,3, 2,2, 2,5, 2,
  2, 4,3, 4,8, 9,10, 9,  11, 10,11, 7, 6, 5, 4, 5,
   4, 3, 2, 2, 0, 2, 7, 2,   1, 2,3,4,5,0,0, 0,

  0, 0,0, 1,2, 1, 1, 0,   3, 1, 1, 2,3, 2, 4, 2,     // 9
   3,4, 5, 0,11, 12,11, 12,  13, 14,14, 13,15, 15,15,15,
  10, 9,10, 9,9, 7,9, 11,   10, 9, 9, 8, 7, 6, 7,6,
   7, 0, 7, 4, 5, 6, 7, 8,   7, 5, 7,8, 7, 6, 7, 8,

   9, 9, 9, 11,10, 11,11, 0,  0, 0,13, 0,2, 3,4, 5,     // 10
  6, 2,7, 8,9, 10,15, 9,   9,10, 11, 12, 13, 14, 10, 15,
   14, 14,11, 2,11, 12,13,0,   1, 3,2, 3,4, 5,0, 2,
  3, 14,13,12, 9, 10,11, 12,   13, 5,14, 15,15, 15,15, 9,

  15, 9,9, 9,8, 9,10, 15,  14, 13,0, 0, 0, 4, 3, 2,     // 11
   1, 2, 3, 5, 3, 2, 7, 2,   2, 2,11, 7,2, 3,4, 0,
  5, 4,3, 3,3, 3, 9, 8,   9, 9, 9,9, 9, 13, 12,15,
   15,14, 15, 10,8, 7,8, 7,  9, 9,13, 14,15, 15,15,14,

  13, 15,13, 9,11, 14,11,12,   12, 12,12, 12, 12, 12, 12, 15,     // 12
  3, 4, 2, 5,13, 13, 12, 15,   8, 7, 7, 9, 9,9, 9, 6,
   9, 8, 9,7,7, 7,0, 7,  2, 5,4, 0,15, 9,15, 15,
  13,14,13, 13,11, 11,11, 7,   15, 10, 9, 8, 7,6, 7, 9,

   7, 6, 7, 8, 7, 12, 7, 7,   7, 5,5, 7,13, 14,15, 15,     // 13
  13, 15,15, 15, 11,12, 7,9,   9, 10,11, 11,9, 12,15,14,
  15, 15,15, 9,13,13,13, 9,  11, 7,11, 7, 9, 5, 9, 5,
   7, 6, 7, 5, 7, 2, 2, 2,   9, 5,5, 7,6, 9,15, 15,

  15, 14,13, 13,15, 13, 12, 11,   7,0, 0, 0, 0, 1, 2, 5,     // 14
   15, 2, 3, 2,0, 0,3, 4,  3, 0,3, 2,1, 9,15, 9,
  13, 13,13, 9,11, 7,11, 7,   9, 10, 9,10, 10, 11, 7,11,
   15, 15, 9,15,11, 7,11, 7,  13, 14,13, 9,15, 9,15, 9
  };

// Add more channel tables here, if desired. All must be the same length.

#define SIZE_TABLE ((uint16_t)(sizeof CHANNEL_1_TABLE / sizeof CHANNEL_1_TABLE[ 0 ] ))

volatile uint8_t isr_flag;
uint16_t table_index;
uint8_t half_cycle, brightness;
uint8_t table_1_value;
// Add more channel table value vars here.

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 6, d7 = 2; // LCD to pin number
const byte buttonPin = 7;            // pin button is connected
int inputState;         // current state of the input
int lastInputState=1;     // previous state of the input
int buttonPushCounter = 0;           // counter for the number of button presses
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void isr1()

  {
  isr_flag = 1U;
  }

void setup() {
  Serial.begin(9600); 
  lcd.begin(16, 2);     // set up the LCD's number of columns and rows:
  lcd.setCursor(0, 0);
  lcd.print("4WARD THEME WORX"); // load up screen text line 1
  lcd.setCursor(0, 1);
  lcd.print("LIGHT FLICKER V1"); // load up screen text line 2
  pinMode(buttonPin, INPUT);
  debouncer.attach(buttonPin);
  debouncer.interval(50);
  pinMode( STATPORT, INPUT ); // The input port detects power line zero-crossing for dimmer
  pinMode( OUTPORT1, OUTPUT );
  // Add more channel outputs here.

  digitalWrite( OUTPORT1, OFF );
  // Add more channel resets here.

  // set up zero crossing interrupt
  attachInterrupt( 1, isr1, RISING );
  interrupts();
}

// Wait for line zero crossing

void sync()

  {
  // wait until zero crossing detected (flag gets set)

  while (!isr_flag)
    ;

  // reset the flag
  isr_flag = 0U;

  // cancel all output drive
  digitalWrite( OUTPORT1, OFF );
  // Add more channel resets here.

  // wait for zero-cross end (pin to go low)
  while (digitalRead( STATPORT ))
    ;
  }

void loop()
{
  debounce();

  Program();
}

    // run selected program based on button count



//set up our programs
void debounce(){

   // do some swith debounce0
 debouncer.update();
// read the input pin:
  inputState = debouncer.read();
// compare the inputState to its previous state (lastInputState)

  if (inputState != lastInputState) //so it changed one way or the other
  {
    if (inputState == HIGH) //pressed
    {
      // if the current state is LOW then the input
      // went from not high to low:
      Serial.println("Pressed");

      buttonPushCounter++;  // add one to counter
      lcd.clear();  
      if (buttonPushCounter > 3) // if counter is over 3 reset the counter to 1 to show Program 1 and not load up text

      {
        buttonPushCounter = 1;
      }

    }
  } 

 lastInputState = inputState;

}


void Program(){
   if (buttonPushCounter == 1){    
      P1(); // Program 1
      }      
      else if (buttonPushCounter == 2){
      P2(); // Program 2
      }
      else if (buttonPushCounter == 3){
      P3();    // Program 3
      }
}
void P1(){ // program 1
          lcd.setCursor(0, 0);
          lcd.print("4WARD THEME WORX"); 
          lcd.setCursor(0, 1);
          lcd.print("P1 - POWER SURGE");

}

void P2(){ // program 2 
          lcd.setCursor(0, 0);
          lcd.print("4WARD THEME WORX"); 
          lcd.setCursor(0, 1);
          lcd.print("P2 - HAUNTED");
          Haunted();
}

void P3(){ // program 3  
          lcd.setCursor(0, 0);
          lcd.print("4WARD THEME WORX"); 
          lcd.setCursor(0, 1);
          lcd.print("P3 - GENERATOR");
          Generator();
}


void PowerSurge()  {


  // Table loop
  for (table_index = 0; table_index < SIZE_TABLE; ++table_index)
    {

    table_1_value = pgm_read_byte(CHANNEL_1_TABLE + table_index);
    // Add more channel table value loads here.
    // All use same table index.

    // Number of power line half-cycles loop
    for (half_cycle = 0; half_cycle < REPEATS; ++half_cycle)   // repeat 'N' half-cycles
      {

      // wait for line zero-cross
      sync();

      // Trigger loop
      for (brightness = 15; brightness > 0; --brightness )   // 15 = bright, 0 = off
        { 

        // Process channel trigger
        if (table_1_value == brightness)
          digitalWrite( OUTPORT1, ON );
        // Add more channel trigger processing here.

        delayMicroseconds( 500 );       // waste time (usec)


        }
      }  
    }


}



 void Haunted(){

 }

 void Generator(){

 }

我认为我遇到的问题是程序1在运行时会调用void函数PowerSurge,该函数包含一个for循环并进一步嵌套了for循环。它还包含一个Interupt,用于监听调光器模块的过零事件。我需要做的就是摆脱这个循环,进入下一个程序,但是对于我一生来说,我已经尝试了所有我能想到的东西,现在真的需要别人的帮助。

0 个答案:

没有答案