Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 2332|回復: 0
打印 上一主題 下一主題

求助 Arduino Mega 2560 LCD+直流馬達控制

[複製鏈接]
跳轉到指定樓層
1#
發表於 2014-8-7 17:52:00 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
各位大大好,我使用Arduino Mega 2560+LCD Keypad Shield+一顆直流馬達

我有一個程式是可以在LCD上設定時間以及鬧鐘,但我想把馬達運轉後停下來也加進去(鬧鐘響時),可是加進去後,馬達不是不動 不然就是一直轉停不下來,請問大大們 我該怎麼加比較好呢??

下面是設定鬧鐘的部分,因為整個程式太長我貼不上來......,我想在鬧鐘響後 --> 馬達運轉5秒 -->馬達停止
void setAlarm()
{
  int button = 0;
  // Pass maxCount to getTimerMinutes
  alarmHours = getTimerMinutes("Set Alarm Hour", alarmHours, 23);  
  // Validate alarm hours > 0 and < 24
  if (alarmHours >= 0 && alarmHours < 24)
  {
    // Pass maxCount to getTimerMinutes
    alarmMinutes = getTimerMinutes("Set Minutes", alarmMinutes, 59);
    // allow alarm minutes to be 0
    if (alarmMinutes < 60)
    {
      lcdClear();
      lcd.setCursor(0,1);
      //display alarm time
      lcd.print(alarmHours);      
      lcd.print(":");
      if (alarmMinutes < 10)
        lcd.print("0");
      lcd.print(alarmMinutes);
      if (button == btnRIGHT)
      {
        timedBeep(shortBeep,1);
        alarmSet = true;   
        lcd.setCursor(0,0);
        lcd.print("Alarm Set for");
        delay(1000);
        return;      
      }
      else
      {
        timerCancelled("Alarm");
        return;  
      }   
    }
    else
    {
      timerCancelled("Alarm");     
    }   
  }     
  else
  {
    timerCancelled("Alarm");      
  }
}

// setDateTime feature
void setDateTime()
{
  int button = 0;

  //get month
  int setMonth = getTimerMinutes("Set Month", lastMonth, 12);
  if (setMonth > 0 && setMonth < 13)
  {
    //get day
    // default day and hour settings on set date/time
    int setDay = getTimerMinutes("Set Day", lastDay, 31);
    if (setDay > 0 && setDay < 32)
    {
      //get year
      int setYear = getTimerMinutes("Set Year", lastYear, 2999);
      if (setYear > 2000 && setYear < 3000)
      {
        //get hour
        int thisHour = lastHour;
        // default day and hour settings on set date/time
        int setHour = getTimerMinutes("Set Hour", thisHour, 23);
        if (setHour >= 0 && setHour < 24)
        {
          //get minutes
          int setMinute = getTimerMinutes("Set Minute", lastMinute, 59);
          if (setMinute < 60)
          {
             // RTC.adjust(DateTime(setYear,setMonth,setDay,setHour,setMinute)); // for DS1307
                setTime(setHour, setMinute, 0, setDay, setMonth, setYear); // sec, min, hour, date, month, year

              lcd.setCursor(0,0);
              lcd.print("Saving...     ");
              delay(1000);
              return;      
            }
            else
            {
              timerCancelled("");
              return;  
            }  
          }
          else
          {
            timerCancelled("");     
          }   
        }     
        else
        {
          timerCancelled("");      
        }
      }
      else
      {
        timerCancelled("");     
      }   
    }     
    else
    {
      timerCancelled("");      
    }

}

// read the buttons from LCD keypad shield
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor
  // my buttons when read are centered at these valies: 0, 131, 307, 481, 722
  // we add approx 50 to those values and check to see if we are close
  // No button pressed should be 1023
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;  
  return btnNONE;  // when all others fail, return this...

}

void timedCountDown(int secondCount, char countLabel[])
{
  long seconds = 0;
  long minutes = 0;

  lcdClear();
  lcd.print(countLabel);
  for (int i = secondCount; i >= 0; i--)
  {
    seconds = i;
    minutes = i / 60;
    if (minutes > 0)
    {
      seconds = seconds - (minutes * 60);  
    }     

    if (minutes > 0)
    {
      lcd.setCursor(0,1);
      lcd.print(minutes);
      lcd.print(" min ");
    }
    else
    {
      lcd.setCursor(0,1);
    }
    if (seconds < 10) lcd.print("0");
    lcd.print(seconds);
    lcd.print(" sec remaining");
    if (seconds > 0) delay(1000);
    if (read_LCD_buttons() == btnSELECT) //cancel
    {
      timerCancelled("Timer");
      i = 0;
      return;
    }
  }
  lcd.setCursor(6,1);
  timedBeep(longBeep,3);
}

// Pass maxCount to getTimerMinutes
int getTimerMinutes(char timerText[], int startNum, int maxCount)
{
  int minutes = startNum;
  int button = 0;
  lcdClear();
  lcd.print(timerText);
  lcd.setCursor(0,1);
  lcd.print(minutes);   

  while (button != btnSELECT)
  {
    button = read_LCD_buttons();
    Serial.println(button);

    if (button == btnLEFT)
    {
      if ((minutes + 10) <= maxCount)
      {
        timedBeep(shortBeep,1);
        minutes = minutes + 10;
      }
      else
      {
        timedBeep(shortBeep,2);
      }
    }

    if (button == btnUP)
    {
      if (minutes < maxCount)
      {
        timedBeep(shortBeep,1);
        minutes++;
      }
      else
      {
        timedBeep(shortBeep,2);
        minutes = 0;
      }
    }
    if (button == btnDOWN)
    {
      if (minutes > 0)
      {
        timedBeep(shortBeep,1);
        minutes--;
      }
      else
      {
        timedBeep(shortBeep,2);
        minutes = maxCount;
      }   
    }
    if (button == btnRIGHT)
    {
      timedBeep(shortBeep,1);
      return minutes;
    }
    lcd.setCursor(0,1);
    lcd.print(minutes);
    lcd.print("   ");
  }
  return 0;
}

void timedBeep(int beepTime, int beepCount)
{
  for (int i = 0; i < beepCount; i ++)
  {
    digitalWrite(beeper, HIGH);
    delay(beepTime);
    digitalWrite(beeper, LOW);
    delay(beepTime);
  }
}

void lcdClear(){
  lcd.clear();
  lcd.begin(16,2);
  lcd.setCursor(0,0);
}

void timerCancelled(char message[])
{
  lcdClear();
  lcd.print(message);
  lcd.print(" Cancelled");
  timedBeep(shortBeep,3);   
}

void setOffAlarm()
{
  int button = 0;
  int i = 0;
  Serial.println(i);
  digitalWrite(backLight, HIGH); // turn backlight on
  while (button != btnSELECT)
  {
    button = read_LCD_buttons();
    lcdClear();
    i++;
    if (i > 50)
    {
      lcdClear();
      lcd.print("Alert Alert");
      lcd.setCursor(0,1);
      lcd.print("     Alert Alert");      
      i = 0;
      timedBeep(shortBeep,3);
    }

  }     
  timerCancelled("Alarm");
  alarmSet = false;  
}
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-9-30 18:36 , Processed in 0.163379 second(s), 7 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表