2016-08-04 75 views
0

我需要幫助爲線路跟蹤器循環一些語句,只要按下鍵盤上的'0'並且要繼續循環直到再次按下按鈕,然後再返回到運行主循環。任何幫助是最受歡迎的。在按下按鈕時運行一個循環

#include <LiquidCrystal.h> 
#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h> 
#include <Servo.h> 
#include <Keypad.h> 

//Pins of LCD  RS E D4 D5 D6 D7 
LiquidCrystal lcd(12, 11, 10, 9, 8, 7); // Pins of Arduino 
Servo servoMain; // Define our Servo 
int tempPin = A0; //LM35 Pin 
int ldrPin = A1; //LDR Pin 
int led = 6; //LED Pin 
int buzzer = 13; 
float temp; 
float tempC; 
int lightReading; 
int numTones = 2; 
int tones[] = {440, 349}; 
int Tones = 3; 
int ones2[] = {988, 740, 988}; 
int bulb = A2; 
int val; 
int motor1Pin1 = 26; // pin 2 on L293D IC 
int motor1Pin2 = 25; // pin 7 on L293D IC 
int enable1Pin = 27; // pin 1 on L293D IC 
int motor2Pin1 = 23; // pin 10 on L293D IC 
int motor2Pin2 = 22; // pin 15 on L293D IC 
int enable2Pin = 24; // pin 9 on L293D IC 
int state; 
int flag = 0;  //makes sure that the serial only prints once the state 
int stateStop = 0; 
int s1val; 
int s2val; 
int led1 = 43; 
int led2 = 42; 
const byte ROWS = 4; // Four rows 
const byte COLS = 3; // Three columns 
// Define the Keymap 
char keys[ROWS][COLS] = { 
    {'1', '2', '3'}, 
    {'4', '5', '6'}, 
    {'7', '8', '9'}, 
    {'*', '0', '#'} 
}; 
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins. 
byte rowPins[ROWS] = {48, 53, 52, 50}; 
// Connect keypad COL0, COL1 and COL2 to these Arduino pins. 
byte colPins[COLS] = {49, 47, 51 }; 

// Create the Keypad 
Keypad kpd = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

void setup() 
{ 
    lcd.begin(16, 2); 
    pinMode(led, OUTPUT); 
    pinMode(tempPin, INPUT); 
    pinMode(ldrPin, INPUT); 
    pinMode(buzzer, OUTPUT); 
    pinMode(bulb, OUTPUT); 
    pinMode(motor1Pin1, OUTPUT); 
    pinMode(motor1Pin2, OUTPUT); 
    pinMode(enable1Pin, OUTPUT); 
    pinMode(motor2Pin1, OUTPUT); 
    pinMode(motor2Pin2, OUTPUT); 
    pinMode(enable2Pin, OUTPUT); 
    digitalWrite(enable1Pin, HIGH); // set to high so that motor can run 
    digitalWrite(enable2Pin, HIGH); // set to high so that motor can run 
    servoMain.attach(3); // servo on pin 3 
    servoMain.write(72); 
    pinMode(led1, OUTPUT); 
    pinMode(led2, OUTPUT); 
    kpd.addEventListener(keypadEvent); 
    Serial.begin(9600); 

    setSyncProvider(RTC.get); // the function to get the time from the RTC 
    if (timeStatus() != timeSet) 
    Serial.println("Unable to sync with the RTC"); 
    else 
    Serial.println("RTC has set the system time"); 

    lcd.clear(); 
    beep(100); 
} 
void loop() 
{ 
    tempC = (5.0 * analogRead(tempPin) * 100.0)/1024; 
    temp = tempC; 
    lightReading = analogRead(ldrPin); 
    val = analogRead(3); 
    Serial.println(val); 
    digitalClockDisplay(); 
    s1val = analogRead(6); 
    s2val = analogRead(7); 
    char key = kpd.getKey(); 

    if (Serial.available() > 0) 
    { 
    state = Serial.read(); 
    flag = 0; 
    } 

    if (val >= 700)    // check if the input is HIGH 
    { 
    val = analogRead(3); 
    doSomething('f'); 
    delay(1000); 
    if (val >= 700) 
    { 
     doSomething('g'); 
    } 

    else 
    { 
     doSomething('m'); 
    } 
    } 

    else 
    { 
    doSomething('m'); 
    } 

    if (temp >= 35 && temp < 50) // "Hot Warning" temp 
    { 
    doSomething('a'); 
    } 

    else if (temp >= 34 && temp < 35) // "Warm Warning" temp 
    { 
    doSomething('b'); 
    } 

    else if (temp >= 50) // "Danger Warning" temp 
    { 
doSomething('c'); 
    } 

    else 
    { 
    doSomething('m'); 
    } 

    if (lightReading > 100 && lightReading <= 200) // Dim Warning 
    { 
    doSomething('d'); 
    } 

    else if (lightReading <= 100) // Dark Warning 
    { 
    doSomething('e'); 
    } 

    else 
    { 
    doSomething('m'); 
    } 

    if (state == '1') //Motor 
    { 
    doSomething('h'); 
    } 

    else if (state == '2')//Motor 
    { 
    doSomething('i'); 
    } 

    else if (state == '3')//Motor 
    { 
    doSomething('j'); 
    } 

    else if (state == '4') //Motor 
    { 
    doSomething('k'); 
    } 

    else if (state == '5')//Motor 
    { 
    doSomething('l'); 
    } 

    else 
    { 
    doSomething('m'); 
    } 

    if (key) // Check for a valid key. 
    { 
    switch (key) 
    { 
    case '1': 

    Serial.println(key); 
    servoMain.write(72); // Turn Servo to 0 
    break; 

    case '2': 

    Serial.println(key); 
    servoMain.write(0); // Turn Servo Left to 36 degrees 
    break; 

    case '3': 

    Serial.println(key); 
    servoMain.write(36); // Turn Servo Left to 72 degrees 
    break; 

    case '4': 

    Serial.println(key); 
    servoMain.write(108); // Turn Servo left to 108 degrees 
    break; 

    case '5': 

    Serial.println(key); 
    servoMain.write(144); // Turn Servo left to 144 degrees 
    break; 

    case '6': 

    Serial.println(key); 
    break; 

    case '7': 

    Serial.println(key); 
    break; 

    case '8': 

    Serial.println(key); 
    break; 

    case '9': 

    Serial.println(key); 
    break; 

    case '0': 

    Serial.println(key); 
    break; 

    case '*': 

    Serial.println(key); 
    break; 

    case '#': 

    Serial.println(key); 
    break; 
    } 
    } 

    delay(300); 
    lcd.clear(); 
    Serial.print("Light,"); Serial.print(lightReading); Serial.print(" "); Serial.print('\n'); 
} 

void keypadEvent(KeypadEvent key) { 
    switch (kpd.getState()) { 

    //U can use three modes: PRESSED, HOLD or RELEASED. 
    //U can also add more than one event with more than one key. 
    case PRESSED: 
     if (key == '0') { 
     if (s1val <= 0 && s2val >= 2) //turn left 
     { 
      digitalWrite(motor1Pin1, HIGH); 
      digitalWrite(motor1Pin2, LOW); 
      digitalWrite(motor2Pin1, LOW); 
      digitalWrite(motor2Pin2, LOW); 
      digitalWrite(led2, HIGH); 
      digitalWrite(led1, LOW); 
     } 

     else if (s1val >= 2 && s2val <= 0) // turn right 
     { 
      digitalWrite(motor1Pin1, LOW); 
      digitalWrite(motor1Pin2, LOW); 
      digitalWrite(motor2Pin1, LOW); 
      digitalWrite(motor2Pin2, HIGH); 
      digitalWrite(led1, HIGH); 
      digitalWrite(led2, LOW); 
     } 

     else if (s1val >= 2 && s2val >= 2) // go forward 
     { 
      digitalWrite(motor1Pin1, HIGH); 
      digitalWrite(motor1Pin2, LOW); 
      digitalWrite(motor2Pin1, LOW); 
      digitalWrite(motor2Pin2, HIGH); 
      digitalWrite(led2, HIGH); 
      digitalWrite(led1, HIGH); 
     } 

     else // stop 
     { 
      digitalWrite(motor1Pin1, LOW); 
      digitalWrite(motor1Pin2, LOW); 
      digitalWrite(motor2Pin1, LOW); 
      digitalWrite(motor2Pin2, LOW); 
      digitalWrite(led1, LOW); 
      digitalWrite(led2, LOW); 
     } 
     } 
    } 
} 

void doSomething(char condition) { 
    switch (condition) { 
    case 'a' : // if temp is higher than 35°C 
    lcd.setCursor(0, 0); 
    lcd.print(" Room Hot!! "); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(150); 
    digitalWrite(led, LOW); 
    delay(200); 
    break; 

case 'b' : // if temp is higher than 30°C 
    lcd.setCursor(0, 0); 
    lcd.print(" Room Warm! "); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(150); 
    digitalWrite(led, LOW); 
    delay(200); 
    break; 

case 'c' : // if temp is higher than 50°C 
    lcd.setCursor(0, 0); 
    lcd.print(" Warning  "); 
    lcd.setCursor(0, 1); 
    lcd.print(" Danger!! "); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep2(150); 
    digitalWrite(led, LOW); 
    delay(200); 
    break; 

case 'd' : // if room is dim 
    analogWrite(bulb, 150); 
    lcd.setCursor(0, 1); 
    lcd.print(" Room Dim!! "); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(150); 
    digitalWrite(led, LOW); 
    delay(200); 
    break; 

case 'e': // if room is dark 
    analogWrite(bulb, 255); 
    lcd.setCursor(0, 1); 
    lcd.print(" Room Dark "); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(150); 
    digitalWrite(led, LOW); 
    delay(200); 
    break; 

case 'f': // when motion detected by I.R Sensor 
    analogWrite(bulb, 0); 
    lcd.setCursor(0, 0); 
    lcd.print("Motion Detected"); 
    digitalWrite(motor1Pin1, LOW); 
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW); 
    digitalWrite(motor2Pin2, LOW); 
    delay(1000); 
    break; 

case 'g':     //motion detected turn on light for 20secs 
    analogWrite(bulb, 255); 
    delay(5000); 
    break; 

case 'h':      // forward 
    digitalWrite(motor1Pin1, HIGH); 
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW); 
    digitalWrite(motor2Pin2, HIGH); 
    break; 

case 'i':      //left 
    digitalWrite(motor1Pin1, HIGH); 
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW); 
    digitalWrite(motor2Pin2, LOW); 
    break; 

case 'j':      //stop 
    digitalWrite(motor1Pin1, LOW); 
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW); 
    digitalWrite(motor2Pin2, LOW); 
    break; 

case 'k':      //right 
    digitalWrite(motor1Pin1, LOW); 
    digitalWrite(motor1Pin2, LOW); 
    digitalWrite(motor2Pin1, LOW); 
    digitalWrite(motor2Pin2, HIGH); 
    break; 

case 'l':      //reverse 
    digitalWrite(motor1Pin1, LOW); 
    digitalWrite(motor1Pin2, HIGH); 
    digitalWrite(motor2Pin1, HIGH); 
    digitalWrite(motor2Pin2, LOW); 
    break; 

default: 
    lcd.setCursor(0, 0); 
    lcd.print("Time:"); 
    lcd.setCursor(6, 0);           // Set LCD cursor position (column, row) 
    lcd.print(hour()); 
    lcd.print(":"); 
    lcd.print (minute()); 
    lcd.print(" "); 
    lcd.print(second()); 
    lcd.print("  "); 
    lcd.setCursor(0, 1); 
    lcd.print("Date:"); 
    lcd.setCursor(6, 1);           // Set LCD cursor position (column,row) 
    lcd.print(day()); 
    lcd.print("."); 
    lcd.print(month()); 
    lcd.print("."); 
    lcd.print(year()); 
    analogWrite(bulb, 0); 
    break; 

    } 
} 

void beep(unsigned char delayms) { // buzzeer tones for room hot and warm 
    for (int x = 0; x < numTones; x++) 
    { 
    tone(buzzer, tones[x]); 
    delay(delayms); 
    } 
    noTone(buzzer); 
} 
void beep2(unsigned char delayms2) { // buzzer tone for room above 50°C 
    for (int y = 0; y < Tones; y++) 
    { 
    tone(buzzer, ones2[y]); 
    delay(delayms2); 
    } 
    noTone(buzzer); 
} 

void digitalClockDisplay() { 
    // digital clock display of the time 
    Serial.print(hour()); 
    printDigits(minute()); 
    printDigits(second()); 
    Serial.print(" "); 
    Serial.print(day()); 
    Serial.print(" "); 
    Serial.print(month()); 
    Serial.print(" "); 
    Serial.print(year()); 
    Serial.println(); 

    lcd.setCursor(0, 0); 
    lcd.print("Time:"); 
    lcd.setCursor(6, 0);  // Set LCD cursor position (column, row) 
    lcd.print(hour()); 
    lcd.print(":"); 
    lcd.print (minute()); 
    lcd.print(" "); 
    lcd.print(second()); 
    lcd.print(" "); 
    lcd.setCursor(0, 1); 
    lcd.print("Date:"); 
    lcd.setCursor(6, 1);  // Set LCD cursor position (column,row) 
    lcd.print(day()); 
    lcd.print("."); 
    lcd.print(month()); 
    lcd.print("."); 
    lcd.print(year()); 

    // Print text to LCD 
    // Delay to read text 
    // Clear the display 

} 

void printDigits(int digits) { 
    // utility function for digital clock display: prints preceding colon and leading 0 
    Serial.print(":"); 
    if (digits < 10) 
    Serial.print('0'); 
    Serial.print(digits); 
} 

回答

0

如果你想有一個按鈕被按下後,你的代碼繼續做需要的東西,一旦按下按鈕被設置並再次按下該按鈕復位「切換」變量。

在您的全局聲明,你可以把bool mustDoIt = false;並在main()

if (keyPressed) { 
    mustDoIt = !mustDoIt; 
} 

if (mustDoIt) { 
    ... 
} 

不少東西,我注意到:

  • 您有tempif ... else(其中的方式不是一個好名字,它可能是temperaturetemporary),您可以在其中調用另一個函數,在其中您可以進行相同的檢查。爲什麼不將溫度傳遞給函數並在函數中檢查它?
  • 你重複代碼幾次,

考慮編寫一個函數是:

喜歡的東西:

void displayMessage(char *first_row, char *second_row) { 
    lcd.setCursor(0, 0); 
    lcd.print(first_row); 
    lcd.setCursor(0, 1); 
    lcd.print(second_row); 
    digitalWrite(led, HIGH); 
    delay(200); 
    beep(150); 
    digitalWrite(led, LOW); 
    delay(200); 
} 
  • 你基本上只檢查tempstatelightReading,正如我所說,移動支票,你真正需要他們。將這三個變量傳遞給doSomething並在那裏進行檢查。

事情是這樣的:

void doSomething(float temp, int state, int lightReading) { 
    if (temp >= 35 && temp < 50) 
    { 
    displayMessage(" Room Hot!! ", ""); 
    } else if (temp >= 34 && temp < 35) 
    { 
    displayMessage(" Room Warm! ", ""); 
    } else if (temp >= 50) 
    { 
    displayMessage(" Warning  ", " Danger!! "); 
    } //...Add rest of the checks here 

    switch (state) { 
    case 1: //Motor 
     digitalWrite(motor1Pin1, HIGH); 
     digitalWrite(motor1Pin2, LOW); 
     digitalWrite(motor2Pin1, LOW); 
     digitalWrite(motor2Pin2, HIGH); 
    break; 
    case 2: 
     digitalWrite(motor1Pin1, HIGH); 
     digitalWrite(motor1Pin2, LOW); 
     digitalWrite(motor2Pin1, LOW); 
     digitalWrite(motor2Pin2, LOW); 
    break; 
    //...Add rest of the checks here 
    } 

而且同樣適用於lightReading

此外,digitalClockDisplay功能可以寫爲:

void digitalClockDisplay() { 
    char time_string[32]; 
    char date_string[32]; 

    memset(time_string, 0, 32); 
    sprintf(time_string, "Time: %02d:%02d:%02d  ", hour(), minute(), second()); 
    memset(date_string, 0, 32); 
    sprintf(date_string, "date: %02d.%02d.%02d  ", day(), month(), year()); 

    displayMessage(time_string, date_string); 

    Serial.println(time_string); 
} 

我現在不能測試的代碼,但我希望這給你的一些想法。

+0

好的。明天一旦我回到學校,我會嘗試編輯代碼。一旦完成,將會更新。非常感謝。 –