Precies Band Watch
I made a watch which keeps time using the ds3231 for good accuracy.
It displays the time on a moving tooth belt in 4 digits 12:34 for example .
The 4 leds let you know which digit is being displayed.
Setting the time is also very easy.
You press a button and the belt moves in steps ,by pressing another button it puts the displayed digit in the correct register.After selecting all 4 digits it goes back in the basic position.
When you press the display button the belt runs forward to the first digit ,waits a short time then reverses to the start position.Then moves forward to the second digit and so on.
Parts Needed
As you can see it is based on the rp2040zero.
It uses the drv8838 motor control board.
The tiny TPS63802 3.3V power supply board
The Hall Effect KY-003 magnetic sensor.
A small 3v 150rpm geared motor.
For the rest a few leds and push buttons and a tooth belt and pullys.Plus ofcourse 11 small 6mm magnets glued to the belt and coverd with colorful stickers.The colours are the resistor colour code so black being zero and white being nine.
Not showed on the foto is the ds3231 which sits above the rp2040 zero board.I removed the standard battery and connector to get it to fit in nicely and wired pin 3 the sqw output to the n/c spot on the board.
This all fits into a hand carved stone cover to give it a nice look.
It is powered by a 3,7v lipo 850mah battery.
This gives you 1,5 days battery life.Which is plenty if you like me charge every day.
Ofcourse you can also use the sleep mode in arduino to get months of use on one charge if you prefer.
Wiring It Up
Ofcourse the first step is wiring everything up .
That is pretty easy the motor controle board sits below the rp2040 as you can see on the foto.
And connects directly to power and i/o pins of the rp2040.The output pins of the motor board connect to the motor.
The hall sensor sits also under the rp2040 and the sensor rests on the inside of the belt.
The small power supply board is wired to the lipo and the 3v3 output to the rp2040 3v3 input.
I removed the led on the sensor board and on the rp2040 to save power.The sensor power is also wired to the 3v3 supply.
The 4 leds are wired to the rp2040 i/o with ofcourse series resistors and the 4 push buttons to the rp2040 i/o directly pulling the inputs to ground.
As last the ds3231 is wired to inputs 0,1and 2 and the 3v3 supply and sits on top of the rp2040 board
I removed the two reset and boot push buttons to fit it all in.
Ofcourse this was after programming and checking everything worked.
Then as for the program..Its a pretty long code but works great.
The Code
#include <Adafruit_NeoPixel.h>
#include"hardware/gpio.h"
#define RGB_PIN 16
#define NUM_LEDS 1
#include <Wire.h>
volatile int secondCounter = 0; // Wordt ALLEEN opgehoogd in de interrupt
int processedSeconds = 0; // Wordt ALLEEN in loop() bijgehouden
const int hallp=6;
const int hallpin=3;
const int b1=9;//set time
const int b2=10;//display time
const int b3=11;//+
const int b4=14;//-
const int led1=7;
const int led2=8;
const int led3=12;
const int led4=13;
const int PIN_MOTOR_PWM=27;
const int ph=28;
const int sl=29;
unsigned long lasttime=0;
unsigned long remstarttijd=0;
unsigned long previousmillis=0;
const unsigned long interval=60000;
bool minuiteelapsed=false;
int remduur=10;
int brakestate=0;
int m1=0;
int m2=5;
int m3=5;
int m4=5;
int status=0;
//int state=0;
int wait=0;
int mt=0;
int pos=0;
int eind=0;
int lasttel=-1;
bool buttonpressed=false;
//unsigned long lasttimer=0;
//const unsigned long intervalt=500;
unsigned long lastdebouncetime=0;
unsigned long lastdebouncetime1=0;
unsigned long debouncedelay1=100;
unsigned long lastpresstime=0;
const unsigned long bouncecooldown=200;
bool stable_b3_state=HIGH;
const unsigned long debouncedelay=200;
volatile int tel=-1;
volatile int lastprocessedtel=-1;
volatile unsigned long lastInterruptTime=0;
int buttonstate=HIGH;
int lastbuttonstate=HIGH;
bool b3_pressed_in_this_stop=false;
bool last_b3_state=HIGH;
unsigned long timerstart=0;
const unsigned long
pause_duration=900;
enum state{
state_idel,
state_moving,
state_stop_and_check,
state_final_run,
state_done};
state currentstate=state_idel;
void hallInterrupt()
{unsigned long currentTime=millis();
if(currentTime-lastInterruptTime>60)
{
if(digitalRead(ph)==HIGH)
{tel--;}
else if(digitalRead(ph)==LOW)
{tel++;}
if(tel>=10)
{tel=-1;
//analogWrite(PIN_MOTOR_PWM,0);
}
lastInterruptTime=currentTime;}
}
Adafruit_NeoPixel pixel(NUM_LEDS, RGB_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Wire.setSDA(0);
Wire.setSCL(1);
Wire.begin();
// Zet de DS3231 SQW-pin direct op 1 Hz
Wire.beginTransmission(0x68);
Wire.write(0x0E);
Wire.write(0x00);
Wire.endTransmission();
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), countSecond, FALLING);
Serial.begin(115200);
// while(!Serial);
pixel.begin();
pixel.clear();
pixel.show();
pinMode(hallpin,INPUT);
pinMode(b1,INPUT_PULLUP);
pinMode(b2,INPUT_PULLUP);
pinMode(b3,INPUT_PULLUP);
pinMode(b4,INPUT_PULLUP);
pinMode(hallp,OUTPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(PIN_MOTOR_PWM,OUTPUT);
pinMode(ph,OUTPUT);
pinMode(sl,OUTPUT);
digitalWrite(hallp,HIGH);
gpio_set_drive_strength(6,GPIO_DRIVE_STRENGTH_12MA);
attachInterrupt(digitalPinToInterrupt(hallpin),hallInterrupt,FALLING);
}
void loop() {
begin:
//unsigned long currentmillis=millis();
//minuiteelapsed=false;
//if(currentmillis-previousmillis>=interval)
//{previousmillis=currentmillis;minuiteelapsed=true;}
//if(minuiteelapsed==true)
//{updatemin();}
// Lees de teller veilig uit (geen noInterrupts() nodig)
int currentCount = secondCounter;
// Controleer of er 60 nieuwe seconden zijn bijgekomen
if (currentCount - processedSeconds >= 60) {
processedSeconds += 60; // Schuif onze referentie 60 seconden op
updatemin();
}
// digitalWrite(hallp,HIGH);
digitalWrite(sl,HIGH);
if(digitalRead(b4)==LOW)
{m1=-1;m2=-1;m3=-1;m4=-1;currentstate=state_moving;}
if(status==0 && digitalRead(b1)==LOW)
{goto status;}
if(currentstate!=state_idel)
{runstate();}
goto begin;
status:
{
//attachInterrupt(digitalPinToInterrupt(hallpin),hallInterrupt,FALLING);
if(status==0)
{ tel=-1;
digitalWrite(led1,HIGH);
digitalWrite(ph,LOW);digitalWrite(sl,HIGH);analogWrite(PIN_MOTOR_PWM,225);//turn on led1 and start motor
status=1;remstarttijd=0;
}
if(status==1 && tel==m1)//if m1 berijkt
{
if(remstarttijd==0)
{remstarttijd=millis();
digitalWrite(ph,HIGH);
}
if(millis()-remstarttijd>=remduur)
{
analogWrite(PIN_MOTOR_PWM,0);//stop motor
lasttime=millis();
status=2;}}
if(status==2)
{
if(millis()-lasttime>=800)//wait 0,5sec
{
digitalWrite(ph,HIGH);analogWrite(PIN_MOTOR_PWM,215);status=3;remstarttijd=0;//rev motor tot begin
}}
if(status==3 && tel==-1)
{if(remstarttijd==0)
{remstarttijd=millis();
digitalWrite(ph,LOW);}
if(millis()-remstarttijd>=remduur)
{
analogWrite(PIN_MOTOR_PWM,0);
lasttime=millis();status=4;}}
if(status==4)
{
if(millis()-lasttime>=300)//wait 0,5sec
{
tel=-1;
digitalWrite(led2,HIGH);
digitalWrite(ph,LOW);digitalWrite(sl,HIGH);analogWrite(PIN_MOTOR_PWM,225);//turn on led1 and start motor
status=5;remstarttijd=0;}}
if(status==5 && tel==m2)//if m1 berijkt
{if(remstarttijd==0)
{remstarttijd=millis();
digitalWrite(ph,HIGH);}
if(millis()-remstarttijd>=remduur)
{analogWrite(PIN_MOTOR_PWM,0);//stop motor
lasttime=millis();
status=6;}}
if(status==6)
{
if(millis()-lasttime>=800)//wait 0,5sec
{
digitalWrite(ph,HIGH);analogWrite(PIN_MOTOR_PWM,215);status=7;remstarttijd=0;//rev motor tot begin
}}
if(status==7 && tel==-1)
{if(remstarttijd==0)
{remstarttijd=millis();
digitalWrite(ph,LOW);}
if(millis()-remstarttijd>=remduur)
{ analogWrite(PIN_MOTOR_PWM,0);
lasttime=millis();status=8;}}
if(status==8)
{
if(millis()-lasttime>=300)//wait 0,5sec
{
tel=-1;
digitalWrite(led3,HIGH);
digitalWrite(ph,LOW);digitalWrite(sl,HIGH);analogWrite(PIN_MOTOR_PWM,225);//turn on led1 and start motor
status=9;remstarttijd=0;}}
if(status==9 && tel==m3)//if m1 berijkt
{if(remstarttijd==0)
{remstarttijd=millis();
digitalWrite(ph,HIGH);}
if(millis()-remstarttijd>=remduur)
{analogWrite(PIN_MOTOR_PWM,0);//stop motor
lasttime=millis();
status=10;}}
if(status==10)
{
if(millis()-lasttime>=800)//wait 0,5sec
{
digitalWrite(ph,HIGH);analogWrite(PIN_MOTOR_PWM,215);status=11;remstarttijd=0;//rev motor tot begin
}}
if(status==11 && tel==-1)
{if(remstarttijd==0)
{remstarttijd=millis();
digitalWrite(ph,LOW);}
if(millis()-remstarttijd>=remduur)
{analogWrite(PIN_MOTOR_PWM,0);
lasttime=millis();status=12;}}
if(status==12)
{
if(millis()-lasttime>=300)//wait 0,5sec
{
tel=-1;
digitalWrite(led4,HIGH);
digitalWrite(ph,LOW);digitalWrite(sl,HIGH);analogWrite(PIN_MOTOR_PWM,225);//turn on led1 and start motor
status=13;remstarttijd=0;}}
if(status==13 && tel==m4)//if m1 berijkt
{if(remstarttijd==0)
{remstarttijd=millis();
digitalWrite(ph,HIGH);}
if(millis()-remstarttijd>=remduur)
{ analogWrite(PIN_MOTOR_PWM,0);//stop motor
lasttime=millis();
status=14;}}
if(status==14)
{
if(millis()-lasttime>=800)//wait 0,5sec
{
digitalWrite(ph,HIGH);analogWrite(PIN_MOTOR_PWM,215);status=15;remstarttijd=0;//rev motor tot begin
}}
if(status==15 && tel==-1)
{if(remstarttijd==0)
{remstarttijd=millis();
digitalWrite(ph,LOW);}
if(millis()-remstarttijd>=remduur)
{analogWrite(PIN_MOTOR_PWM,0);status=16;}}
if(status==16)
{digitalWrite(led1,LOW);digitalWrite(led2,LOW);digitalWrite(led3,LOW);digitalWrite(led4,LOW);status=0;digitalWrite(sl,LOW);goto begin;}
goto status;
}}
void runstate(){
switch(currentstate){
case state_moving:
//attachInterrupt(digitalPinToInterrupt(hallpin),hallInterrupt,FALLING);
{ digitalWrite(ph,LOW);analogWrite(PIN_MOTOR_PWM,120);}
if(tel<lastprocessedtel)
{lastprocessedtel=-1;}
if(tel>lastprocessedtel && tel<=9){
analogWrite(PIN_MOTOR_PWM,0);
timerstart=millis();
currentstate=state_stop_and_check;
}
break;
case state_stop_and_check:
{
bool current_b3_state=digitalRead(b3);
if(last_b3_state==HIGH && current_b3_state==LOW){
if(millis()-lastpresstime>bouncecooldown){
lastpresstime=millis();
Serial.print("b3 pressed! current tel value:");
Serial.println(tel);
if(tel>=0 && tel<=9){
if(m1==-1){digitalWrite(led1,HIGH);m1=tel;Serial.println("saved to m1");}
else if(m2==-1){digitalWrite(led2,HIGH);m2=tel;Serial.println("saved to m2");}
else if(m3==-1){digitalWrite(led3,HIGH);m3=tel;Serial.println("saved to m3");}
else if(m4==-1){digitalWrite(led4,HIGH);m4=tel;Serial.println("saved to m4");}
}
}
}
last_b3_state=current_b3_state;
if(millis()-timerstart>=pause_duration){
lastprocessedtel=tel;
if(m4!=-1){
currentstate=state_final_run;
}else{
lastprocessedtel=tel;remstarttijd=0;
currentstate=state_moving;
}
}}
break;
case state_final_run:
{
if(tel!=-1)
{
digitalWrite(ph,LOW);analogWrite(PIN_MOTOR_PWM,175);
}
else
if(tel==-1)
{digitalWrite(led1,LOW);digitalWrite(led2,LOW);digitalWrite(led3,LOW);digitalWrite(led4,LOW);
if(remstarttijd==0)
{
remstarttijd = millis();
digitalWrite(ph,HIGH);
}
if(millis()-remstarttijd>=remduur)
{
analogWrite(PIN_MOTOR_PWM,0);lastprocessedtel=-1;
currentstate=state_done;remstarttijd=0;
}
}
}
break;
case state_done:
{
analogWrite(PIN_MOTOR_PWM,0);
digitalWrite(led1,LOW);digitalWrite(led2,LOW);digitalWrite(led3,LOW);digitalWrite(led4,LOW);}
currentstate=state_idel;
break;
}}
void updatemin(){
m4++;
if(m4>9)
{m4=0;m3++;}
if(m3>5)
{
m3=0;
if(m1==1 && m2==2){
m1=0;
m2=1;
}
else
{m2++;
if(m2>9)
{m2=0;m1++;}
}}}
void countSecond() {
secondCounter++;}
I guess i could have put more comments in the program but didnt get round to it.
}
The End Product
And thats about it u guess.
Apart from the video which shows 12:13
Oh i also set the clock freq to 50mhz in tools menu before loading the program to reduce power consumption.
https://youtube.com/shorts/PGgoroTp3G8?is=MD7R5uh27b7jsRtC
Click on link and open.