Hello. I posted about this yesterday, if any of you are reading this again. But although I fixed my previous problem I have a new one haha. Ok, here it is.
So my final project for my engineering class is an Arduino-based automated blinds system. By "blinds" I'm referring to the blinds on house windows. The general idea is that a phototransistor takes continuous readings of the ambient light in a room. When the light level gets too bright, the Arduino sends a signal to a DC motor to turn the blinds closed, thus reducing the light in the room. And vice versa when the light is too dim (blinds open to allow light in). In order to accomplish this, I needed an H-bridge (L239D) to reverse polarity on the motor to allow the motor to spin in both directions when needed. And that's essentially it. It's a very simple circuit, nothing complicated, as I'm a beginner with Arduinos and coding. One phototransistor, one H-bridge, one DC motor, one Arduino . I've attached a sketch of the wiring diagram below.
Now here is the new problem I'm having.
So, to be clear, the concept that enables the system to operate properly is that there is a threshold level of light that when exceeded initiates the motor to close the blinds (because the light is now too bright). And when the light is below the threshold, the motor is initiated to open the blinds. So, the concept hinges upon the threshold. Here is the problem, though. Instead of initiating the motor when the light crosses the threshold, instead it is initiating the motor every time the light level changes value. So, when the light level changes from, for example, 600 to 601, the motor initiates. If the light level changes from 601 to 602, it initiates again. And so on. The motor is at least turning in the appropriate direction (when the light is low it the motor turns so it closes the blinds). But again, it keeps doing this over and over every time the value changes at all. I set my threshold at 700 in the code below. So, if the light changes from 600 to 599, it turns the motor to close the blinds. And again when it goes down to 598.. etc. And if the light is above 700, then it will open the blinds. But then again when it is 701, and again at 702.. etc. Obviously, this is a problem because the motor will constantly be moving and, remember, the motor will be attached to the blinds, so the blinds will basically be torn off its hinges. Can someone help guide me in the right direction and help save my final project? What am I missing?
Also, before anyone says anything about the PWM-ing -- I am PWMing the motor so it turns at an appropriate speed (not too fast, not too slow). And I added a delay operation so the motor only turns for appropriate amount of time (if the motor turns for too long, again the blinds will be torn off the walls). But all of that is irrelevant to my problem. What is my code missing?
And here is the code:
const int controlPin1 = 5;
const int controlPin2 = 3;
const int enablePin = 9;
const int lightPin = A0;
const int thresHold = 700;
int motorSpeed = 0;
int lightState = 0;
int lastLS = 0;
void setup() {
Serial.begin(9600);
pinMode(lightPin, INPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);// put your setup code here, to run once:
}
void loop() {
lightState = analogRead(lightPin);
delay(1);
motorSpeed = (1023)/100;
Serial.println(lightState);
if (lightState != lastLS){
if(lightState > thresHold){
digitalWrite(enablePin, HIGH);
analogWrite(controlPin1, motorSpeed);
digitalWrite(controlPin2, LOW);
delay(1000);
digitalWrite(enablePin, LOW);
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, LOW);
}
if (lightState < thresHold){
digitalWrite(enablePin, HIGH);
digitalWrite(controlPin1, LOW);
analogWrite(controlPin2, motorSpeed);
delay(1000);
digitalWrite(enablePin, LOW);
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, LOW);
}
delay(50);
}
lastLS = lightState;
}