Hey all!
I was hoping somebody could take a look at some code I wrote and possibly help me figure out where I may be going wrong. I've looked at this for hours and have redone this code over and over again, but I honestly just can't see where I'm going wrong!
Scenario:
I wrote some code that would start off by rotating two stepper motors, then when a limit switch is pressed, then motors would switch direction. I used an RTC clock module to set time instances for this code to run once when each "alarm" happens. I'll attach the code below! I ran this code and it worked just fine. My connections are all correct for my motors, drivers and limit switches as well.
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
// Pins for pulley motor
const int dirPin = 3;
const int stepPin = 4;
const int enPin = 5;
const int limitSwitchPin = 8;
const int dirPin1 = 6;
const int stepPin1 = 7;
const int enPin1 = 9;
void setup() {
Serial.begin(9600);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(limitSwitchPin , INPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
digitalWrite(dirPin,LOW); // Enables the pulley motor to move in a particular direction
pinMode(stepPin1,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(enPin1,OUTPUT);
digitalWrite(enPin1,LOW);
digitalWrite(dirPin1,LOW); // Enables the brush motor to move in a particular direction
// get and set the time from the RTC
setSyncProvider(RTC.get);
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
Alarm.alarmRepeat(13,15,0, motors);
attachInterrupt(digitalPinToInterrupt(2),limitswitchpressed, CHANGE);
}
void loop() {
digitalClockDisplay();
Alarm.delay(1000);
}
void motors(){
int i=0;
while (i<1){
int limitSw = digitalRead(limitSwitchPin);
if( (limitSw == HIGH && (digitalRead(dirPin) == LOW)) || (limitSw == HIGH && (digitalRead(dirPin) == HIGH))){
motorStep(1);
}
else if( (limitSw == LOW && (digitalRead(dirPin) == LOW))){
digitalWrite(dirPin,LOW);
digitalWrite(dirPin1,LOW);
delay(2000);
}
else if( limitSw == LOW && (digitalRead(dirPin) == HIGH ) ){
digitalWrite(enPin,HIGH);
digitalWrite(enPin1,HIGH);
break;
}
}
void motorStep( int MAX){
for(int x = 0; x < MAX; x++) {
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
digitalWrite(stepPin1, LOW);
delayMicroseconds(500);
}
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
Now I switched the code to this scenario:
Limit switch is already pressed or released (the motor should spin regardless), motor spins one direction, then when it hits the other limit switch, it changes direction and stops when it hits the first limit switch again.
I tried mimicking my new code to somewhat match my previous working code, but for some reason, the code doesn't work. It will either stay stuck in the while loop, or it will only spin if the limit switch is pressed the entire time, once it is released the motor stops.
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
// Pins for pulley motor
const int dirPin = 42;
const int stepPin = 44;
const int enPin = 40;
const int limitSwitchPin = 50;
const int limitSwitchPin1 = 52;
// Pins for brush motor
const int dirPin1 = 51;
const int stepPin1 = 53;
const int enPin1 = 49;
void setup() {
Serial.begin(9600);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(limitSwitchPin , INPUT);
pinMode(limitSwitchPin1, INPUT);
pinMode(enPin,OUTPUT);
pinMode(stepPin1,OUTPUT);
pinMode(dirPin1,OUTPUT);
pinMode(enPin1,OUTPUT);
}
void loop() {
tmElements_t tm;
RTC.read(tm);
Serial.print(tm.Hour);
Serial.print(":");
Serial.println(tm.Minute);
if(RTC.read(tm) && (tm.Hour==12) && (tm.Minute==55)){
motors();
}
}
// Function to be called when rtc time triggers:
void motors(){
digitalWrite(enPin,LOW); //Turn motor drivers on
digitalWrite(enPin1,LOW);
digitalWrite(dirPin,LOW); // Enables the pulley motor to move in a particular direction
digitalWrite(dirPin1,LOW); // Enables the brush motor to move in a particular direction
int i=0;
while (i<1){
int limitSw = digitalRead(limitSwitchPin);
int limitSw1 = digitalRead(limitSwitchPin1);
if(digitalRead(dirPin) == LOW || digitalRead(dirPin) == HIGH){
int x=0;
while (x<1) {
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin1, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
digitalWrite(stepPin1, LOW);
delayMicroseconds(500);
// If top limit switch (D52) is pressed, bottom limit switch (D50)is not pressed, and motor going up -> delay 2s & motor go down
if (limitSw1 == LOW && limitSw == HIGH && (digitalRead(dirPin) == LOW)){
digitalWrite(dirPin, HIGH);
digitalWrite(dirPin1, HIGH);
delay(2000);
}
// If top limit switch (D52) is not pressed, bottom limit switch (D50)is pressed, and motor going down -> motor stop & break out of x-while loop
else if (limitSw1 == HIGH && limitSw == LOW && (digitalRead(dirPin) == HIGH)){
digitalWrite(enPin, HIGH);
digitalWrite(enPin1, HIGH);
break;
}
}
}
break;
}
}
Any help would be appreciated! Is there any way to just check if a limit switch has been activated? Like if it is pressed and released, the motors should turn?
Thanks again for any help! If you need me to clarify anything, I'll be happy to elaborate!