Introduction:
Hi all, I have been reading through endless amounts of related or similar projects to mine. While I did learn a lot, I am still having some issues with coding it seems. I have read through the “How to get the best out of this forum (short version)” and will try to follow it religiously to help avoid generating headaches for anyone. I am a beginner and will be grateful if I was treated as such. I am eager to learn and hope to become a productive and useful member of the forum one day.
Note: I know the guided stated NOT to use “Fritzing diagrams.” However, I think the wiring schematic is simple enough that it should not cause any confusion. Also, please recommend a good software replacement. I have attached the Fritzing diagram along with the LM393 IR speed sensor as the one I can for the Fritzing software only had a 4-pin variant. I am willing to learn, just let me know if anything that I wrote was confusing or can be further clarify to help you guys out.
Choose the right section of the Forum:
I am pretty sure my issue is regarding the code hence I use the “Programing Questions”
Hardware:
-
Arduino Uno (Rev.3) (x1)
-
30 KG Digital Servo (x1)
-
Dual channel 5v relay module (x1)
-
DC to DC converter (x1)
-
30 watt DC motor (x1)
-
LM393 IR speed sensor module (3-pin type) (x2)
-
12V DC power supply (x1)
-
Push Button (2-pins) (x1)
Code problems:
Expected from code:
I am currently trying to build an actuator that can have its maximum and minimum stroke length adjusted by two limit switches located on the outside of the cylinder. The actuator will be powered by a dc motor which will then have its direction controlled by two relays depending on the state of the two limit switches and the push button (cycle button). A servo is also part of the assembly. One full “cycle” should look something like the following.
1: Cycle button pressed
2: Servo moves from 0* to 180* (short delay of 500ms)
3: Motor rotates CW to drive the actuator rod forwards
4: Front Limit switch is triggered
5: Motor STOPS
6: Servo Moves from 180* to 0* (short delay of 500ms)
7: Motor rotates CCW to drive the actuator rod backwards
8: Back Limit switch is triggered
9: Motor STOPS
10: Wait for Cycle button to be pressed
What actually happens:
The program actually works for the most part, however, it seems that at times the front limit sensor would trigger too slowly causing the triggering nub to smash right into the limit sensor. What is confusing to me is that it would work as coded for maybe 8 cycles and then it would suddenly just run past the sensor. The light on the sensor flickers on and off which shows that the sensor senses movement but does nothing or does not do it in time to prevent a collision. I would appreciate any kind of help; I am at my wits end. I am well versed in mechanical design and systems; programing has been quite a difficult process.
The following is the code
#include <Servo.h>
Servo servo;
//Variables
const int relay1 = 2;
const int relay2 = 3;
const int cycleButton = 4;
const int LMF = 6;
const int LMB = 7;
int m;
//PROGRAM FUNCTIONS
void motorCW() // function to make motor turn CW
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
}
void motorCCW() // function to make motor turn CCW
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, LOW);
}
void motorSTOP() // function to make motor stop
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}
void setup()
{
pinMode(cycleButton, INPUT_PULLUP); // set pin 4 as INPUT_PULLUP for cycle button
pinMode(LMF, INPUT); // set pin 6 as INPUT for Front IR Sensor Limit
pinMode(LMB, INPUT); // set pin 7 as INPUT for Front IR Sensor Limit
pinMode(relay1, OUTPUT); // set pin 2 as output for relay 1
pinMode(relay2, OUTPUT); // set pin 3 as output for relay 2
servo.attach(5); // attach Servo to pin 5
digitalWrite(relay1, HIGH); // set relay 1 OFF State
digitalWrite(relay2, HIGH); // set relay 2 OFF State
}
void loop(){
if (digitalRead(cycleButton) == LOW) {
m = 0;
}
if (digitalRead(LMF) == HIGH) {
m = 1;
}
if (digitalRead(LMB) == HIGH) {
m = 2;
}
if (digitalRead(cycleButton) == LOW && digitalRead(LMB) == HIGH) {
m = 3;
}
switch(m) {
case 0: {
servo.write(180);
delay(500);
motorCW();
break;
}
// Ideally for case 1 would be 1: motorSTOP 2: Servo to 0 3: Delay (500) to give time for servo 4: motor CCW; this does not work however. The moment I use motorSTOP it will not start again
case 1: {
motorCCW();
servo.write(0);
break;
}
case 2: {
motorSTOP();
break;
}
// Case 3 is here because the Cycle button wont work once it has run its first cycle as LMB will be triggered HIGH and hence it will trigger function motorSTOP()
case 3: {
servo.write(180);
motorCW();
break;
}
}
}