Witam
Mam problem z małym projektem który zaczołem. Mianowicie chcę zrobić zegar na dwóch silnikach krokowych, Jeden na minuty drugi na godziny. Silnik na minuty to standardowy 28BYJ-48, na godziny silnik krokowy wydarty z drukarki EM-183.
Po uruchomieniu zegara minuty bazują się na czujniku halla a godziny na krańcówce. Wszystko działa, bazowanie ok. Problem jest z ruchem godzin, silnik nie pracuje tylko "buczy" w miejscu. W kodzie jest trochę zbędnych śmieci.
Mam problem z małym projektem który zaczołem. Mianowicie chcę zrobić zegar na dwóch silnikach krokowych, Jeden na minuty drugi na godziny. Silnik na minuty to standardowy 28BYJ-48, na godziny silnik krokowy wydarty z drukarki EM-183.
Po uruchomieniu zegara minuty bazują się na czujniku halla a godziny na krańcówce. Wszystko działa, bazowanie ok. Problem jest z ruchem godzin, silnik nie pracuje tylko "buczy" w miejscu. W kodzie jest trochę zbędnych śmieci.
Kod:
#include <Wire.h>
#include "RTClib.h"
#include <AccelStepper.h>
RTC_DS1307 rtc;
#include <elapsedMillis.h>
elapsedMillis przesun;
#define HALFSTEP 8
#define FULLSTEP 4
// Hour Motor pin definitions
#define motorHour1Pin1 11 // IN4 on the ULN2003 driver 1
#define motorHour1Pin2 10 // IN3 on the ULN2003 driver 1
#define motorHour1Pin3 9 // IN2 on the ULN2003 driver 1
#define motorHour1Pin4 8 // IN1 on the ULN2003 driver 1
// Minute pin definitions
#define motorMin1Pin1 5 // IN1 on the ULN2003 driver 2
#define motorMin1Pin2 6 // IN2 on the ULN2003 driver 2
#define motorMin1Pin3 7 // IN3 on the ULN2003 driver 2
#define motorMin1Pin4 8 // IN4 on the ULN2003 driver 2
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
//AccelStepper stepperHour1(HALFSTEP, motorHour1Pin1, motorHour1Pin3, motorHour1Pin2, motorHour1Pin4);
AccelStepper stepperHour1(FULLSTEP, 4, 3, 2);
AccelStepper stepperMin1(FULLSTEP, motorMin1Pin1, motorMin1Pin2, motorMin1Pin3, motorMin1Pin4);
int currPosition;
long positionHour;
long positionMin;
long pozycja = 200;
long pozycja2 = 2048;
//int posFlag = 0;
//int posFlag2 = 0;
int minFlag = 0;
int hrFlag = 0;
//int minuteStep = 2048 / 60; //68 the number of steps the minute motor moves every minute
//int hourStep = 4096 / 144; //28 the number of steps the hour motor moves every minute
int hourSwitch = 9;
int minuteSwitch = A1;
int hourStop = A0;
int hallMin;
int state = LOW;
int previous = LOW;
int buttonReading;
int state2 = LOW;
int previous2 = LOW;
int buttonReading2;
// Initialize time variables
int8_t hours;
int8_t hours24;
int8_t minutes;
int8_t seconds;
int8_t days;
int8_t months;
int8_t hours12;
int minuteSteps;
int hrSteps;
//long moveTime;
int minS;
int hrS;
int hourHome = -1;
int minHome = -1;
void setup() {
Serial.begin(9600);
//RTC CLOCK
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
pinMode(hourSwitch, INPUT_PULLUP);
pinMode(minuteSwitch, INPUT);
pinMode(hourStop, OUTPUT);
hallMin = analogRead(minuteSwitch);
//digitalRead(hourSwitch);
//digitalRead(minuteSwitch);
//digitalWrite(4,LOW);
//Uncomment one of them to set time when change batteries
rtc.adjust(DateTime(2020, 9, 27, 3, 56, 20));//this sets time manually
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//this sets time based on the compilation time of the computer
Serial.print("Time: ");
Serial.print(hours24);
Serial.print(":");
Serial.print(minutes);
Serial.print(":");
Serial.println(seconds);
///////////////////////////////////////////////HOME
stepperHour1.setMaxSpeed(1000); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperHour1.setAcceleration(50); // Set Acceleration of Stepper
stepperHour1.setSpeed(100);
stepperMin1.setMaxSpeed(200); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperMin1.setAcceleration(20); // Set Acceleration of Stepper
stepperMin1.setSpeed(100);
delay(5);
while (digitalRead(hourSwitch))
{ // Make the Stepper move CCW until the switch is activated
//stepperHour1.enableOutputs();
stepperHour1.moveTo(hourHome); // Set the position to move to
hourHome--; // Decrease by 1 for next move if needed
stepperHour1.run(); // Start moving the stepper
delay(5);
}
//digitalWrite(4,HIGH);
//digitalWrite(hourStop, LOW);
while (analogRead(minuteSwitch) > 200)
{ // Make the Stepper move CCW until the switch is activated
stepperMin1.enableOutputs();
stepperMin1.moveTo(minHome); // Set the position to move to
minHome--; // Decrease by 1 for next move if needed
stepperMin1.run(); // Start moving the stepper
delay(5);
Serial.println(analogRead(minuteSwitch));
}
//stepperMin1.disableOutputs();
stepperHour1.setCurrentPosition(0); // Set the current position as zero for now
hourHome = 1;
stepperMin1.setCurrentPosition(0); // Set the current position as zero for now
minHome = 1;
}
void loop() {
DateTime now = rtc.now();
hours = now.hour();
hours12 = hours/2;
minutes = now.minute();
seconds = now.second();
days = now.day();
months = now.month();
minS = minutes;
hrS = hours12*12;
if (hours12 == 0) {hrS = minS;}
if (hours12 == 1) {hrS = minS + 60;}
if (hours12 == 2) {hrS = minS + (60*2);}
if (hours12 == 3) {hrS = minS + (60*3);}
if (hours12 == 4) {hrS = minS + (60*4);}
if (hours12 == 5) {hrS = minS + (60*5);}
if (hours12 == 6) {hrS = minS + (60*6);}
if (hours12 == 7) {hrS = minS + (60*7);}
if (hours12 == 8) {hrS = minS + (60*8);}
if (hours12 == 9) {hrS = minS + (60*9);}
if (hours12 == 10) {hrS = minS + (60*10);}
if (hours12 == 11) {hrS = minS + (60*11);}
minuteSteps = map(minutes, 0, 60, 1, 2048);
hrSteps = map(hrS, 0, 720, 0, 6000);
Serial.print("Time: ");
Serial.print(hours);
Serial.print(":");
Serial.print(minutes);
Serial.print(":");
Serial.println(seconds);
////////////////////////////////////// krancowki
buttonReading = digitalRead(hourSwitch);
if (buttonReading == LOW && previous == HIGH)
{ if (state == LOW)
{ state = HIGH;
}
else {
state = LOW;
}
}
previous = buttonReading;
buttonReading2 = digitalRead(minuteSwitch);
if (buttonReading2 == LOW && previous2 == HIGH)
{ if (state2 == LOW)
{ state2 = HIGH;
}
else {
state2 = LOW;
}
}
previous2 = buttonReading2;
////////////////////////////////////// krancowki
/////////////////////////////////////////minuty
if (seconds == 0)
{
minFlag = 1;
}
if (minFlag == 1)
{
stepperMin1.setMaxSpeed(100); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperMin1.setAcceleration(50); // Set Acceleration of Stepper
stepperMin1.setSpeed(100);
//stepperMin1.enableOutputs ();
stepperMin1.moveTo(minuteSteps);
stepperMin1.runToPosition();
}
if (stepperMin1.currentPosition() == minuteSteps)
{
minFlag = 0;
stepperMin1.stop();
}
if (minutes == 0)
{
///ponowne bazowanie minut
while (analogRead(minuteSwitch) > 200)
{ // Make the Stepper move CCW until the switch is activated
stepperMin1.moveTo(1800);
stepperMin1.run();
if (stepperMin1.currentPosition() == 1800)
{
stepperMin1.moveTo(minHome); // Set the position to move to
minHome--; // Decrease by 1 for next move if needed
stepperMin1.run(); // Start moving the stepper
delay(5);
Serial.println(analogRead(minuteSwitch));
}
}
stepperMin1.setCurrentPosition(0);
stepperMin1.stop();
}
////////////////////////////////////////minuty
////////////////////////////////////////godziny
if (seconds == 0)
{
while (stepperHour1.currentPosition() != hrSteps)
{
stepperHour1.setMaxSpeed(100); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperHour1.setAcceleration(50); // Set Acceleration of Stepper
stepperHour1.setSpeed(50);
//digitalWrite(4,LOW);
stepperHour1.moveTo(hrSteps);
stepperHour1.run();
}
//digitalWrite(4,HIGH);
//stepperHour1.stop();
}
//if (minutes == 0)
//{
// while (analogRead(minuteSwitch) > 200)
// { // Make the Stepper move CCW until the switch is activated
// stepperMin1.moveTo(-60);
// stepperMin1.run();
// if (stepperMin1.currentPosition() == -60)
// {
// stepperMin1.moveTo(minHome); // Set the position to move to
// minHome--; // Decrease by 1 for next move if needed
// stepperMin1.run(); // Start moving the stepper
// delay(5);
// Serial.println(analogRead(minuteSwitch));
// }
// }
// stepperMin1.setCurrentPosition(0);
//}
////////////////////////////////////////godziny
Serial.print("minuteSteps ");
Serial.println(minuteSteps);
Serial.print("MINUTRcurrent ");
Serial.println(stepperMin1.currentPosition());
Serial.print("HRSteps ");
Serial.println(hrSteps);
Serial.print("HRcurrent ");
Serial.println(stepperHour1.currentPosition());
}