时钟模块控制舵机2018-10-27

2019-03-13  本文已影响0人  愉快先生

这里面要添加个库,DS1302的库
库文件:https://github.com/msparks/arduino-ds1302
注意看注释部分,第一次设置初始时间需要,之后烧录要把那段代码注释掉。

// Example sketch for interfacing with the DS1302 timekeeping chip.
//
// Copyright (c) 2009, Matt Sparks
// All rights reserved.
//
// http://quadpoint.org/projects/arduino-ds1302
#include <stdio.h>
#include <DS1302.h>
#include<Servo.h>
Servo myservo;
int pos = 90;    // variable to store the servo position
const int a=60;

namespace {

// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip. See the DS1302
// datasheet:
//
//   http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
const int kCePin   = 5;  // Chip Enable,rst
const int kIoPin   = 6;  // Input/Output,dat
const int kSclkPin = 7;  // Serial Clock,clk

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  }
  return "(unknown day)";
}

void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();

  // Name the day of the week.
  const String day = dayAsString(t.day);

  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
}

}  // namespace

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.
  //从这里开始
//  rtc.writeProtect(false);
//  rtc.halt(false);
//
//  // Make a new time object to set the date and time.
//  // Sunday, September 22, 2013 at 01:38:50.
//  Time t(2018, 10, 28, 23, 26, 30, Time::kSunday);
//
//  // Set the time and date on the chip.
//  rtc.time(t);  
  //setup里面的函数,只需要在第一次设置初始时间时要,设好了,在把这段函数给去掉,在烧一次,就不会时间每次都重置了
}

// Loop and print the time every second.
void loop() {
  Time t = rtc.time();   
  printTime();
  delay(1000);
   
  if(t.hr==6&&t.min==30)   //t.hr和t.min中有一个小点是结构体,代表t结构里面的hr和min。。单上面必须时时有rtc.time这个函数,才时时有Time t这个结构体。
  {
    Serial.println("悄悄起床");
    OPEN();
    }
    else if(t.hr==6&&t.min==40)
    {
    Serial.println("悄悄起床");
    OPEN();
    
    }
    else
    {
     
    myservo.write(-30);
    
    
    
    }

  
}


void OPEN()
{ 
//       men.write(60);  //选60度作为中间值是因为,这个不能转负值,所以不选0度,,选60度,这样小于60为正方向,大于60为负方向。
//       delay(2000);
//       men.write(20);
//       delay(1000);
//       men.write(60);
//       delay(40000);
  for (pos = 0; pos <= 40; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(5);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 40; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  }  
  
  
上一篇下一篇

猜你喜欢

热点阅读