Pages: [1]   Go Down
Author Topic: driving servo motors with a clock  (Read 395 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am workin on a clock drived by many servos, i will need 9 for the hours, 14 for the minuts ( 5 for the decimal digits and 9 for the others ) , and they will be 2 marks on every servo ( one at the 0 degree and one on the 45 degree ), so lets say the time is
"6:45" then 6 of the 9 servos ( for the hours ) will be on 45 Dg, while the others on 0 , and 4 of the 5 will be active ( for decimals ) and then 5 out of 9.

Code:
// include libraries

#include "Wire.h"
#include "Servo.h"

Servo  hour_servo1
Servo  hour_servo2
Servo  hour_servo3
Servo  hour_servo4
Servo  hour_servo5
Servo  hour_servo6
Servo  hour_servo7
Servo  hour_servo8
Servo  hour_servo9
Servo  hour_servo10

#define DS1307_I2C_ADDRESS 0x68

// defining variables

int realhour = 0;
int hours = 0;
int minutes = 0;

// 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) );
}

void setDateDs1307(byte second,byte minute,byte hour,byte dayOfWeek,byte dayOfMonth,byte month,byte year)
{
  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();
}

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);
  *second     = bcdToDec(Wire.receive() & 0x7f);
  *minute     = bcdToDec(Wire.receive());
  *hour       = bcdToDec(Wire.receive() & 0x3f); 
  *dayOfWeek  = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month      = bcdToDec(Wire.receive());
  *year       = bcdToDec(Wire.receive());
}

// Function to Set time
/////
/////
////
/////

void setup() {

hour_servo1.attach(1);
hour_servo1.attach(2);
hour_servo1.attach(3);
hour_servo1.attach(4);
hour_servo1.attach(5);
hour_servo1.attach(6);
hour_servo1.attach(7);
hour_servo1.attach(8);
hour_servo1.attach(9);
hour_servo1.attach(10);

  second = 0;
  dayOfWeek = 1;
  dayOfMonth = 24;
  month = 5;
  year = 10;

// Set up the time using the buttons then ->

setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);


}

void loop() {

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  realhour = int(hour);

  if (realhour==0) // if it is 0000h, that should be 1200h in 12 hour time
  {
    realhour=12;
  }

  if (hour>=13)
  {
      realhour=realhour-12; // convert 24h hours to 12h hours
  }

  hours=realhour*14;
  minutes=minute*3;
 
    switch(hours) {
      case '1':
        hour_servo1.write(45); // if active set position to 45 degree
        hour_servo2.write(0); // if inactive set position to 0 degree
hour_servo3.write(0);
hour_servo4.write(0);
hour_servo5.write(0);
hour_servo6.write(0);
hour_servo7.write(0);
hour_servo8.write(0);
hour_servo9.write(0);
hour_servo10.write(0);
        break;
      case '2':
.
.
.
.

      case '12':
        .

        break;
    }

  delay(100);     // servos need time to move

    switch(minutes) {
      case '1':
        .

        break;
      case '2':
.
.
.
.

      case '60':
.

        break;
    }
  delay(100);

}

1 ) - i have problems setting time with buttons ... i cant figure out how to do it ?
2 ) - As you can see the case method will be quite long, is there another way to do it ? i was reading about arrays, but can anyone show me an example, because i am not sure how to do it neither.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8967
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Time to learn about arrays of objet pointers.  Something like this will do the whole hours case statement:

Code:
Servo *hourServos[] = {hour_servo1,  hour_servo2,  hour_servo3,  hour_servo4,  hour_servo5,  hour_servo6,  hour_servo7,  hour_servo8,  hour_servo9,  hour_servo10};

for (i=0; i<10; i++)
    {
    if (i < hours)
        hourServos[i].write(45);
    else
        hourServos[i].write(0);
    }

The tens of minutes and minutes can be done similarly:

minutes / 10 for the ten's digit
minutes % 10 for the one's digit
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank for you help .. reading your answer, i should know that it is beginner stuff ... but okay, i suits my level smiley
Logged

Pages: [1]   Go Up
Jump to: