Hello,
I'm creating an self opening mechanism that is supposed to start opening once the sun is detected (even if its a bit cloudy) . I am using a stepper motor (Hanpose 17HS) for the opening pourpous and photoresistor for sun detection. My stepper motor is run through TMC2209 driver and also will include a switch on top of mechanism that will 'signalize' that its on the right spot and is supposed to stop turning at that point. I've managed to create a code that starts to spin motor in 1 direction then after a while it goes to another direction but the idea of photoresistor making stepper motor spin once the sun is detected isn't working out, in fact no matter if its dark or sun it will spin left/right no matter what. I am not an expert in coding nor I have ever had any kind of classes, besides what I watched on internet and what I managed to figure out so far, which is why I'd like to ask someone if you can point out my mistakes so I can try to improve on my own. Thank you !
#define step_pin 6
#define dir_pin 5
#define home_switch 3
const int minReading = 55;
const int maxReading = 25;
int photoresistorPin = A0;
int decValue = 0;
unsigned int x=65535;
int analogValue = 0;
int delay_master = 1000;
int steps;
void setup() {
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(home_switch, INPUT_PULLUP);
pinMode(photoresistorPin, INPUT);
Serial.begin(9600);
while (digitalRead(home_switch)) {
digitalWrite(dir_pin, HIGH);
digitalWrite(step_pin, HIGH);
delayMicroseconds(1000);
digitalWrite(step_pin, LOW);
delayMicroseconds(1000);
}
steps = 0;
delay(10000);
}
void loop() {
decValue = analogRead(photoresistorPin);
x = map(decValue, 0, 1023, 0, 65000);
x = round(x/650.0);
Serial.println(x);
int y = 0;
for(int y = 0; y < 14600; y++) {
if (( y <= 7300) && (x < minReading)) {
digitalWrite(dir_pin,LOW);
}
else {
digitalWrite(dir_pin,HIGH);
}
digitalWrite(step_pin,HIGH);
delayMicroseconds(delay_master);
digitalWrite(step_pin,LOW);
delayMicroseconds(delay_master);
}
//delay(1000);
}