138Views11Replies

Author Options:

servo programming on arduino? Answered

hi,
i need a help on my arduino project.
i need to make daily program my servo motor on arduino. for example;
23:00 at night ,it should turn right 90'; 
8:00 at morning, it should turn left(back) 90'

also;
when i need; i should turn this servo, left-right manually via internet.
how can i make this, which component i should use, which commands i should use? 
any tutorial, or samples like this?

i found some examples on instructables like using servo via internet, Control a Servo over the Internet etc. but i dont know how make this two option together.

ps my arduino level: intermediate 

tI appreciate any help that you can provide :)

Discussions

0
None
herneise

3 years ago

// this is the first part that i am working on, the programming part.

//after that i am planning to start on internet part

//i use as a base tutarial : https://www.instructables.com/id/Arduino-Timer-With...

//waiting for your comments

//thanks

#include <LiquidCrystal.h> //Libraries

#include <RTClib.h>

#include <Wire.h>

// servo things:

#include <Servo.h>

Servo s1; //servo 1

#define SERV1 13 //servo 1 on digital port 13

int pos1 = 170; //servo 1 current position

int vel = 10; //servos' speed (100 -> 90º in 1 s)(1 -> 90º in 9 s)

int pos1min = 10; //servo 1 minimum position

int pos1max = 170; //servo 1 maximum position

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //Arduino pins to lcd

//LCD 1602

//RS to 2pin, EN to 3pin, D4 to 4pin, D5 to 5pin, D6 to 6pin, D7 to 7pin

//Contrast= 10K potentiometer: 1 to Vcc, 2(center)to VO, 3 to GND

//Backlight= K to GND(with 1K resistor), A to Vcc

//Vss to GND , Vdd to +5v

RTC_DS1307 RTC; // define the Real Time Clock object

//RTC 1307

//SDA to analog 4, SCL to analog 5

//You need 4 buttons like this,to enter the set points, with 10K resistor to ground (for each one)

// To arduino pins(8, 9, 10, 11)

// |

// |

// switch |

// _______ | 10K

// +5V 0-----0 0----0------/\/\/\/\-------0 to GND

const int DOWN_BUTTONon =9; //Buttons Pins

const int UP_BUTTONon =8;

const int DOWN_BUTTONoff =11;

const int UP_BUTTONoff =10;

//-----Variables for debouncing------//

boolean lastDownONButton = LOW;

boolean currentDownONButton = LOW;

boolean lastUpONButton = LOW;

boolean currentUpONButton = LOW;

boolean lastDownOFFButton = LOW;

boolean currentDownOFFButton = LOW;

boolean lastUpOFFButton = LOW;

boolean currentUpOFFButton = LOW;

int set_on = 12; //The default "ON" desired time

int set_off= 12; //The default "OFF" desired time

void setup(){

//attach and set servo angles

s1.attach(SERV1);

s1.write(pos1min);

pinMode(SERV1, OUTPUT);

Wire.begin();

RTC.begin();

lcd.begin(16, 2); // Configura lcd numero columnas y filas

lcd.setCursor(0,0); //Show "TIME" on the LCD

lcd.print("TIME");

lcd.setCursor(0,1);

lcd.print("ON"); //Show "ON" on the lcd

lcd.setCursor(6,1);

lcd.print("OFF"); //Show "OFF" on the lcd

}

//----Debouncing function for all buttons----//

boolean debounce(boolean last, int pin)

{

boolean current = digitalRead(pin);

if (last != current)

{

delay(5);

current = digitalRead(pin);

}

return current;

}

void loop(){

//--------Show current Time On LCD--------//

DateTime now = RTC.now(); // Clock call

now = RTC.now();

lcd.setCursor(5,0);

if(now.hour() < 10)

{

lcd.print("0");

}

lcd.print(now.hour(), DEC); //Print hour

lcd.print(':');

if(now.minute() < 10)

{

lcd.print("0");

}

lcd.print(now.minute(), DEC); //Print min

lcd.print(':');

if(now.second() < 10)

{

lcd.print("0");

}

lcd.print(now.second(), DEC); //Print sec

//----Debounce buttons---//

currentDownONButton = debounce(lastDownONButton, DOWN_BUTTONon);

currentUpONButton = debounce(lastUpONButton, UP_BUTTONon);

currentDownOFFButton = debounce(lastDownOFFButton, DOWN_BUTTONoff);

currentUpOFFButton = debounce(lastUpOFFButton, UP_BUTTONoff);

//-----Turn down the set "ON"-----//

if (lastDownONButton== LOW && currentDownONButton == HIGH)

{

if(set_on>0){ //"ON" Set point down never lower than 0

set_on--;

}

else{

lcd.setCursor(3,1);

lcd.print("0");

}

}

//----Turn up the set "ON"----//

else if (lastUpONButton== LOW && currentUpONButton == HIGH)

{

if(set_on<23){ //"ON" Set point up never higher than 23

set_on++;

}

else{

lcd.setCursor(3,1);

lcd.print("23");

}

}

//---Print the set "ON"---//

lcd.setCursor(3,1);

if(set_on < 10){

lcd.print("0");

}

lcd.print(set_on);

lastDownONButton = currentDownONButton;

lastUpONButton = currentUpONButton;

//-----Turn down the set "OFF"-----//

if (lastDownOFFButton== LOW && currentDownOFFButton == HIGH)

{

if(set_off>0){ //"OFF" Set point down never lower than 0

set_off--;

}

else{

lcd.setCursor(10,1);

lcd.print("0");

}

}

//----Turn up the set "OFF"----//

else if (lastUpOFFButton== LOW && currentUpOFFButton == HIGH)

{

if(set_off<23){ //"OFF" Set point up never higher than 23

set_off++;

}

else{

lcd.setCursor(10,1);

lcd.print("23");

}

}

//---Print the set "OFF"---//

lcd.setCursor(10,1);

if(set_off < 10){

lcd.print("0");

}

lcd.print(set_off);

lastDownOFFButton = currentDownOFFButton;

lastUpOFFButton = currentUpOFFButton;

//----Servo Function----//

if(set_on == set_off){

s1.write(0);

}

if(set_on > set_off){

if(now.hour() >= set_on && now.hour() < set_off ){ //Start

s1.write(180);

}

else if(now.hour() >= set_off) {

s1.write(0);

}

else{

s1.write(0);

}

}

if (set_on < set_off){

if(now.hour() >= set_on && now.hour() <= 23){ //Start

s1.write(180);

}

else if(now.hour() < set_off){

s1.write(180);

}

else if(now.hour() >= set_off && now.hour() < set_on){

s1.write(0);

}

}

}// The End

0
None
steveastroukherneise

Answer 3 years ago

Does it do what yiou want when you set it ? It looks like you need better bracketing around the if expressions.

if ( (now.hour() >= set_off) & (now.hour() < set_on) ){

0
None
steveastrouk

3 years ago

Show us an example of the servo-internet code you have, and we can look at how to modify it.

0
None
steveastrouk

3 years ago

Its easy. All you need to do is put timer conditions on the servo statements that the internet commands activate.

0
None
icengsteveastrouk

Answer 3 years ago

I cant remember do you have to maintain the timing to hold position or does power off lock the position ?

0
None
Downunder35miceng

Answer 3 years ago

Depends on how you implement it and what library you use.
Some prefer to do a full swing and return to middle position before normal operation, others prefer to simply stay in the last position.
Had to play with this when I tried my skills on a small laser scanner...
I guess the standard is to stay where it was left.

0
None
herneiseDownunder35m

Answer 3 years ago

as i told on the top, i need to turn the knob at 23:00 to right. wait there.(hold position) and 8:00 turn back again. and hold position there. and repeat this loop everyday + wifi control whenever i want.

0
None
herneiseiceng

Answer 3 years ago

i need to turn the knob at 23:00 to right. wait there.(hold position) and 8:00 turn back again. and hold position there. and repeat this loop everyday + wifi control whenever i want.

0
None
herneise

3 years ago

to be more clear;

i need to control this knob. this is for our heating system. in russia, there are 2 shift for electrical payment cost.

from 8:00 to 23:00 it costs 0,04 usd/kw; 23:00 to 8:00 it costs 0,02 usd/kw

i can not arrange it by ordinary timer etc, because incoming power is 9kw( heating system uses 9 kw) so the simple way to control is , turn this knob.

thanks for answers. possible to be more detailed?

1 (2).jpeg