Hello, all..
I have the task of making a tool using stepper motor, LDR sensor, and 2 limit switch ..
The way it works is if the LDR sensor gets the light then the stepper motor to the right, if it is dark then the stepper will go to the left ..
Now the stepper motor code has been successfully moved according to the command from the LDR sensor to the right, how to stop stepper motor when pressing limit switch (1)? And rewind to the left when the light is dark, then stop when pressing limit switch (2)?
Code problem only in stepper motor and limit switch ...
Please help
#include <LiquidCrystal.h>
int ldr = A1;
int light = 0;
int limit_switch_a = 6;
int limit_switch_a = 7;
int IN1=8;
int IN2=9;
int IN3=10;
int IN4=11;
int count=0;
int delayTime = 3;
int delayTime1 = 3;
LiquidCrystal lcd (13, 12, 5, 4, 3, 2);
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
}
void loop() {
light = analogRead(ldr);
if(light < 300 ){
lcd.print("NIGHT");
left(5,delayTime);
}
else if(light < 1000){
lcd.print("AFTERNOON");
right(5,delayTime);
}
delay(delayTime);
}
void right(int stepps, int delayTime){
for (count = 0; count < stepps; count++)
{
//step a
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(delayTime1);
//step b
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(delayTime1);
//step c
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(delayTime1);
//step d
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(delayTime1);
} //End For
}
void left(int stepps, int delayTime){
for (count = 0; count < stepps; count++)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(delayTime1);
//step c
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(delayTime1);
//step b
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(delayTime1);
//step a
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
delay(delayTime1);
}
}