/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
modified for the ESP32 on March 2017
by John Bennett
see http://www.arduino.cc/en/Tutorial/Sweep for a description of the original code
* Different servos require different pulse widths to vary servo angle, but the range is
* an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
* sweep 180 degrees, so the lowest number in the published range for a particular servo
* represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
* of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
* 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
* degrees.
*
* Circuit: (using an ESP32 Thing from Sparkfun)
* Servo motors have three wires: power, ground, and signal. The power wire is typically red,
* the ground wire is typically black or brown, and the signal wire is typically yellow,
* orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
* considerable power, we will connect servo power to the VBat pin of the ESP32 (located
* near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
*
* We could also connect servo power to a separate external
* power source (as long as we connect all of the grounds (ESP32, servo, and external power).
* In this example, we just connect ESP32 ground to servo ground. The servo signal pins
* connect to any available GPIO pins on the ESP32 (in this example, we use pin 18.
*
* In this example, we assume a Tower Pro MG995 large servo connected to an external power source.
* The published min and max for this servo is 1000 and 2000, respectively, so the defaults are fine.
* These values actually drive the servos a little past 0 and 180, so
* if you are particular, adjust the min and max values to match your needs.
*/
#include <ESP32Servo.h>
#include <ESP32Time.h>
#include <Stepper.h>
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
// ULN2003 Motor Driver Pins
#define IN1 16
#define IN2 4
#define IN3 0
#define IN4 2
// initialize the stepper library
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
ESP32Time rtc(0);
//Servo myservo; // create servo object to control a servo
// 16 servo objects can be created on the ESP32
//int pos = 0; // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
//int servoPin = 18;
int add_time_button=34;
int set_button =35;
int increase_button =32;
int decrease_button =33;
int current_time_table[10]; //{hour,minute,hour,minute,hour,minute,}
int num=sizeof(current_time_table)/sizeof(current_time_table[0]);
volatile int i=0;
void setup() {
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
//myservo.setPeriodHertz(50); // standard 50 hz servo
//myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 18 to the servo object
pinMode(add_time_button, INPUT_PULLUP);
pinMode(set_button, INPUT_PULLUP);
pinMode(increase_button, INPUT_PULLUP);
pinMode(decrease_button, INPUT_PULLUP);
rtc.setTime(30, 21, 20, 4, 12, 2022);
for(int j=0;j<num;j++){
current_time_table[j]=-1;
}
// set the speed at 5 rpm
myStepper.setSpeed(10);
// initialize the serial port
Serial.begin(115200);
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
}
void loop() {
int default_hour=0;
int default_minute=0;
int index_cpy=i;
int current_hour=rtc.getHour(true);
int current_minute=rtc.getMinute();
for(int j=0;j<num;j+=2)
{
if(current_hour==current_time_table[j]){
if(current_minute==current_time_table[j+1] && (rtc.getSecond()==0)){
Serial.println("clockwise");
myStepper.step(stepsPerRevolution/6);
delay(1000);
//
}
}
}
if(!(digitalRead(add_time_button))){
Serial.println("----------------adding a new time--------------- ");
delay(800);
if(index_cpy<num){
int x=1;
Serial.println("choose hour : ");
while(x) {
if(!(digitalRead(increase_button))){
if(default_hour==24)
{
default_hour=0;
}
else
{
default_hour++;
delay(200);
Serial.println(default_hour);
}
}
if(!(digitalRead(decrease_button))){
if(default_hour==0)
{
default_hour=24;
}
else
{
default_hour--;
delay(200);
Serial.println(default_hour);
}
}
if(!(digitalRead(set_button))){
current_time_table[i]=default_hour;
Serial.print("hour choosed is : ");
Serial.println(current_time_table[i]);
i+=2;
delay(150);
x=0;
}
}
x=1;
Serial.println("-----------------------");
Serial.println("choose minute : ");
while(x) {
if(!(digitalRead(increase_button))){
if(default_minute==60)
{
default_minute=0;
}
else
{
default_minute++;
delay(200);
Serial.println(default_minute);
}
}
if(!(digitalRead(decrease_button))){
if(default_minute==0)
{
default_minute=60;
}
else
{
default_minute--;
delay(200);
Serial.println(default_minute);
}
}
if(!(digitalRead(set_button))){
current_time_table[index_cpy+1]=default_minute;
Serial.print("minute choosed is : ");
Serial.println(current_time_table[index_cpy+1]);
delay(150);
x=0;
}
}
Serial.println ("------------------------------- ");
Serial.print ("time added is---->> ");
Serial.print(current_time_table[index_cpy]);
Serial.print (" : ");
Serial.println( current_time_table[index_cpy+1]);
Serial.println ("------------------------------- ");
}
else{
Serial.println("time table is full !!!!!!!!!! ");
}
}
}