zoeyeung 发表于 2012-4-13 11:05:13

有關Arduino自訂控制重覆的次數

這一個程式是可以不斷地重覆的,
但希望可以自訂控制重覆的次數,
請問各位大大有沒有方法?
謝謝了! (_ _)// these arrays store timer data
// on hour, on minute, off hour, off minute, day of week , on/off (0 off 1 on)
int t1224 = 0; // set later on in function mset1224()
int alarmon = 0; // 0 for off, 1 for on
int alarmh = 12; // alarm hour
int alarmm = 00; // alarm minute

int t1= {
0,0,0,0,0,0,0};
int d1= {
0,0,0,0,0,0,0};
int t2= {
0,0,0,0,0,0,0};
int d2= {
0,0,0,0,0,0,0};
int dd=1000; // used for delay in show timer data

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
#include <LiquidCrystal.h> // we need this library for the LCD commands
LiquidCrystal lcd(12,11,5,4,3,2);

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock

// Assumes you're passing in valid numbers

void setDateDs1307(byte second,      // 0-59
byte minute,      // 0-59
byte hour,          // 1-23
byte dayOfWeek,   // 1-7
byte dayOfMonth,    // 1-28/29/30/31
byte month,         // 1-12
byte year)          // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour));   
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.send(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
*second   = bcdToDec(Wire.receive() & 0x7f);
*minute   = bcdToDec(Wire.receive());
*hour       = bcdToDec(Wire.receive() & 0x3f);// Need to change this if 12 hour am/pm
*dayOfWeek= bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month      = bcdToDec(Wire.receive());
*year       = bcdToDec(Wire.receive());
}

int readdial(int rangemax, int dialpin)
// rangemax is the number of values in your range, e.g. if you want 0~9, set rangemax to be '10'
// dialpin is the analog pin number connected to the potentiometer to read
{
int kv=0;
int kr=0;
int kb=0;
float a=0;
float rd=0;
rd=1023/rangemax;
kb=analogRead(dialpin); // read potentiometer connected to analog pin 1
a=kb/rd;
kr=int(a);
if (kr>rangemax)
{
    kr=rangemax;
}
return kr;
}

void displaymenu()
// display the menu options, selectable by using the knob
{
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Turn knob slowly");
lcd.setCursor(0,1);
lcd.print("to select option");
while (digitalRead(8)==LOW)
{
    if      (readdial(6,0)==0) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Show timer data");
    }
      else if (readdial(6,0)==1) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Set timer 1");
    }
      else if (readdial(6,0)==2) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Set timer 2");
    }
    else if (readdial(6,0)==3) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Timers on/off");
    }
    else if (readdial(6,0)==4) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(" 12 or 24h time?");
    }
    else if (readdial(6,0)==5) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Set time and day");
    }
    else if (readdial(6,0)==6) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("exit menu");
    }
    delay(100); // stop screen flicker
}

switch(readdial(6,0))
{
case 0:
    displaytimerdata();// done, untested
    break;
case 1:
    msetalarm1();
    break;
case 2:
    msetalarm2();
    break;
case 3:
    timeronoff(); // done, untested
    break;
case 4:
    mset1224();
    break;
case 5:
    settimeday(); // done, untested
    break;
}
// if the knob is 5, that is for return to clock display. function will end and return to clock by default
}

void mset1224()
//allows user to select between displaying 12 and 24 hour time
{
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Turn knob slowly");
lcd.setCursor(0,1);
lcd.print("to select option");
while (digitalRead(8)==LOW)
{
    if      (readdial(2,0)==0) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("12-hour time");
    }
    else if (readdial(2,0)==1) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("24-hour time");
    }
    delay(100); // stop screen flicker
}
switch(readdial(2,0))
{
case 0:
    t1224=0;
    break;
case 1:
    t1224=1;
    break;
}
}

void displaytimerdata()
// scrolls through settings for the timers
{
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print(" Timer One: ");
if (t1==0)
{
    lcd.print("Off");
}
else if (t1==1)
{
    lcd.print("On");
}
lcd.setCursor(0,1);
lcd.print("On: ");
if (t1<10)
{
    lcd.print("0");
}
lcd.print(t1);
if (t1<10)
{
    lcd.print("0");
}
lcd.print(d1);
lcd.print("h");
delay(dd);
lcd.setCursor(0,1);
lcd.print("                ");
lcd.setCursor(0,1);
lcd.print("Off: ");
if (t1<10)
{
    lcd.print("0");
}
lcd.print(t1);
if (t1<10)
{
    lcd.print("0");
}
lcd.print(t1);
lcd.print("h");
delay(dd);
lcd.setCursor(0,1);
lcd.print("                ");
lcd.setCursor(0,1);
lcd.print("Freq: ");
switch(t1)
{
case 1:
    lcd.print("Sundays");
    break;
case 2:
    lcd.print("Mondays");
    break;
case 3:
    lcd.print("Tuesdays");
    break;
case 4:
    lcd.print("Wed'days");
    break;
case 5:
    lcd.print("Thursdays");
    break;
case 6:
    lcd.print("Fridays");
    break;
case 7:
    lcd.print("Saturdays");
    break;
case 8:
    lcd.print("Daily");
    break;
case 9:
    lcd.print("Weekends");
    break;
case 10:
    lcd.print("Weekdays");
    break;
}
delay(dd);
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print(" Timer Two: ");
if (t2==0)
{
    lcd.print("Off");
}
else if (t2==1)
{
    lcd.print("On");
}
lcd.setCursor(0,1);
lcd.print("On: ");
if (t2<10)
{
    lcd.print("0");
}
lcd.print(t2);
if (t2<10)
{
    lcd.print("0");
}
lcd.print(d2);
lcd.print("h");
delay(dd);
lcd.setCursor(0,1);
lcd.print("                ");
lcd.setCursor(0,1);
lcd.print("Off: ");
if (t2<10)
{
    lcd.print("0");
}
lcd.print(t2);
if (t2<10)
{
    lcd.print("0");
}
lcd.print(t2);
lcd.print("h");
delay(dd);
lcd.setCursor(0,1);
lcd.print("                ");
lcd.setCursor(0,1);
lcd.print("Freq: ");
switch(t2)
{
case 1:
    lcd.print("Sundays");
    break;
case 2:
    lcd.print("Mondays");
    break;
case 3:
    lcd.print("Tuesdays");
    break;
case 4:
    lcd.print("Wed'days");
    break;
case 5:
    lcd.print("Thursdays");
    break;
case 6:
    lcd.print("Fridays");
    break;
case 7:
    lcd.print("Saturdays");
    break;
case 8:
    lcd.print("Daily");
    break;
case 9:
    lcd.print("Weekends");
    break;
case 10:
    lcd.print("Weekdays");
    break;
}
delay(dd);

}

void timeronoff()
//allows user to turn timers on or off
{
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Turn knob slowly");
lcd.setCursor(0,1);
lcd.print("to select option");
while (digitalRead(8)==LOW)
{
    if      (readdial(2,0)==0) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(" Timer 1: Off    ");
    }
    else if (readdial(2,0)==1) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(" Timer 1: On   ");
    }
    delay(100); // stop screen flicker
}
switch(readdial(2,0))
{
case 0:
    t1=0;
    break;
case 1:
    t1=1;
    break;
}
delay(300); // for debounce
while (digitalRead(8)==LOW)
{
    if      (readdial(2,0)==0) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(" Timer 2: Off    ");
    }
    else if (readdial(2,0)==1) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(" Timer 2: On   ");
    }
    delay(100); // stop screen flicker
}
switch(readdial(2,0))
{
case 0:
    t2=0;
    break;
case 1:
    t2=1;
    break;
}

}

void settimeday()
// allows user to set current time and day of week, e.g. 1234h, Sunday
{
msethours();
delay(300); // for debounce
msetminutes();
delay(300); // for debounce
msetdayofweek();
delay(300); // for debounce
}

void msethours()
//allows user to select hour value
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Select hour:0~23");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(24,0));
    delay(100);
}
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
hour=readdial(24,0);
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
d1=hour;
d2=hour;
}

void msetminutes()
//allows user to select minute value
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set minute:0~59");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(60,0));
    delay(100);
}
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
minute=readdial(60,0);
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
d1=minute;
d2=minute;
}

void msetdayofweek()
//allows user to set day of week
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Set day of week");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(0,1);
    lcd.print("                ");   
    lcd.setCursor(0,1);
    switch(readdial(7,0))
    {
    case 1:
      lcd.print("Sunday");
      break;
    case 2:
      lcd.print("Monday");
      break;
    case 3:
      lcd.print("Tuesday");
      break;
    case 4:
      lcd.print("Wednesday");
      break;
    case 5:
      lcd.print("Thursday");
      break;
    case 6:
      lcd.print("Friday");
      break;
    case 7:
      lcd.print("Saturday");
      break;
    }
    delay(100);
}
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
dayOfWeek=readdial(7,0);
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

void msetalarm1()
//allows user to select timer one on/off times and calendar options
{
// set on hour
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("d1 eat every hour: 0~23");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(24,0));
    delay(100);
}
t1=readdial(24,0);
d1=d1+t1;
// set on minute
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("D1 eat every mins: 0~59");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(60,0));
    delay(100);
}
t1=readdial(60,0);
d1=d1+t1;
if
(d1>60)
    {
      d1=d1-60;
    d1=d1+1;
}
// set off hour
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("D1 Off hour:0~23");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(24,0));
    delay(100);
}
t1=readdial(24,0);
d1=d1+t1;
// set off minute
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("D1 Off min: 0~59");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(60,0));
    delay(100);
}
t1=readdial(60,0);
d1=d1+t1;
if
(d1>60)
    {
    d1=d1-60;
    d1=d1+1;
}
// set calendar options
// daily?
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Daily? ");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(1,1);
    lcd.print("          ");   
    lcd.setCursor(1,1);
    if (readdial(2,0)==0)
    {
      lcd.print("Yes");
    }
    else if (readdial(2,0)==1)
    {
      lcd.print("No");
    }
    delay(100);
}
if (readdial(2,0)==0)
{
    t1=8;
}
delay(300); // for debounce
// weekend only?
}

void msetalarm2()
//allows user to select timer two on/off times and calendar options
{
// set on hour
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("d2 eat every hour: 0~23");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(24,0));
    delay(100);
}
t2=readdial(24,0);
d2=d2+t2;
// set on minute
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("D2 eat every mins: 0~59");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(60,0));
    delay(100);
}
t2=readdial(60,0);
d2=d2+t2;
if
(d2>60)
    {
      d2=d2-60;
    d2=d2+1;
}
// set off hour
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("D2 Off hour:0~23");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(24,0));
    delay(100);
}
t2=readdial(24,0);
d2=d2+t2;
// set off minute
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("D2 Off min: 0~59");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(8,1);
    lcd.print("   ");   
    lcd.setCursor(8,1);
    lcd.print(readdial(60,0));
    delay(100);
}
t2=readdial(60,0);
d2=d2+t2;
if
(d2>60)
    {
    d2=d2-60;
    d2=d2+1;
}
// set calendar options
// daily?
delay(300); // for debounce
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Daily? ");
while (digitalRead(8)==LOW)
{
    lcd.setCursor(1,1);
    lcd.print("          ");   
    lcd.setCursor(1,1);
    if (readdial(2,0)==0)
    {
      lcd.print("Yes");
    }
    else if (readdial(2,0)==1)
    {
      lcd.print("No");
    }
    delay(100);
}
if (readdial(2,0)==0)
{
    t2=8;
}
delay(300); // for debounce
// weekend only?
}
void checktimer1()
// checks to see if need to turn timer 1 on or off
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
// if daily check on or off
if (t1==8)
{
    if (hour==d1 && minute==d1)
    {
      digitalWrite(9, HIGH);
      t1=1;
      d1=d1+t1;
      if
(d1>60)
    {
      d1=d1-60;
    d1=d1+1;
}
      Serial.println(d1, DEC);
      delay(1000);      
    }
    if (hour==d1 && minute==d1)
    {
      digitalWrite(9, LOW);
      t1=0;
      d1=d1+t1;
      Serial.println(d1, DEC);
      delay(1000);
    }
}
}

void checktimer2()
// checks to see if need to turn timer 2 on or off
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
// if daily check on or off
if (t2==8)
{
    if (hour==d2 && minute==d2)
{
      digitalWrite(10, HIGH);
      t2=1;
      d2=d2+t2;
      if
(d2>60)
    {
    d2=d2-60;
    d2=d2+1;
}
      Serial.println(d2, DEC);
      delay(1000);      
    }
    if (hour==d2 && minute==d2)
    {
      digitalWrite(10, LOW);
      t2=0;
      d2=d2+t2;
      Serial.println(d2, DEC);
      delay(1000);
    }
}
}

void setup()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
Serial.begin(9600);
second = 0;
minute = 27;
hour = 9;
dayOfWeek = 4;
dayOfMonth = 21;
month = 3;
year = 12;
//setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); // only do this once on first use
lcd.begin(16, 2); // tells Arduino the LCD dimensions
lcd.setCursor(0,0);
lcd.print("2timer"); // print text and move cursor to start of next line
lcd.setCursor(0,1);
lcd.print("*hi*");
delay(1500);
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print("alarm1"); // print text and move cursor to start of next line
lcd.setCursor(0,1);
lcd.print("alarm2");
delay(1500);
lcd.clear(); // clear LCD screen
pinMode(8, INPUT); // for button
pinMode(9, OUTPUT); // to circuit 1
pinMode(10, OUTPUT); // to circuit 2
}

void loop()
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
lcd.clear(); // clear LCD screen
lcd.setCursor(0,0);
lcd.print("");
if (t1224==0)
{
    if (hour==0)
    {
      lcd.print("12");
    }
    else
      if (hour>0 && hour<13)
      {
      lcd.print(hour, DEC);
      }
      else
      if (hour>=13)
      {
      lcd.print((hour-12), DEC);
      }
}
else if (t1224==1)
{
    lcd.print(hour, DEC);
}
lcd.print(":");
if (minute<10)
{
    lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
    lcd.print("0");
}
lcd.print(second, DEC);
if (t1224==1)
{
    lcd.print("h");
}
else if (t1224==0)
{
    if ((23-hour)>11)
    {
      lcd.print("am");
    }
    if ((23-hour)<12)
    {
      lcd.print("pm");
    }
}
lcd.print(" ");
switch(dayOfWeek){
case 1:
    lcd.print("Sun");
    break;
case 2:
    lcd.print("Mon");
    break;
case 3:
    lcd.print("Tue");
    break;
case 4:
    lcd.print("Wed");
    break;
case 5:
    lcd.print("Thu");
    break;
case 6:
    lcd.print("Fri");
    break;
case 7:
    lcd.print("Sat");
    break;
}
lcd.setCursor(0,1);
lcd.print("drug 1[");
if (t1==0)
{
    lcd.print("_] 2[");
}
else if (t1==1)
{
    lcd.print("!] 2[");
}


if (t2==0)
{
    lcd.print("_]");
}
else if (t2==1)
{
    lcd.print("!]");
}


if (digitalRead(8)==HIGH)
    // has the user pressed the button? If so, display the menu
{
    delay(200); // for debounce
    displaymenu();
}
   Serial.print(d1, DEC);
   Serial.print("-->");
   Serial.println(d1, DEC);
   Serial.println(":");
   Serial.println(d1, DEC);
   Serial.print(d2, DEC);
   Serial.print("-->");
   Serial.println(d2, DEC);
   Serial.println(":");
   Serial.println(d2, DEC);
// check for on/off
checktimer1();
checktimer2();
delay(200); // to stop screen flicker
}

zoeyeung 发表于 2012-4-23 21:14:01

沒有人知道嗎?@@

haojian 发表于 2012-4-27 09:16:20

不是很明白你的问题。应该再具体一些。
我想:1、定义一个全局变量,赋初值0。
      2、在LOOP()中检测这个变量是否到你想要的重复次数了,是则去完成你要做的事情。
      3、在某个函数(就是处理重复的那个函数)中让变量加一,需要具体读一下程序。

zhangdeyue1 发表于 2012-10-15 10:11:49

这个程序都写出了了自定控制次数搞不定?:L
页: [1]
查看完整版本: 有關Arduino自訂控制重覆的次數