hey yall im building a variable speed windshield wiper w the following components (also see attachments):
- arduino uno
- L298N h-bridge
- 12V gear head motor
- water level sensor
- 2 limit switches (pins NO, NC, C)
- 2 6.5V batteries
it worked relatively decently when we controlled the wiping/halting by time intervals (same setup but without limit switches) but the motor doesn't always turn the same amount & it gets uncalibrated.
original code:
const int in1 = 9;
const int in2 = 8;
const int waterSensor = A1;
const int travelTime = 355;
int waterValue;
int haltTime;
void setup() {
Serial.begin(115200);
pinMode(waterSensor, INPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
}
void clockwise() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
delay(travelTime);
}
void counterClockwise() {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
delay(travelTime);
}
void halt() {
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
delay(haltTime);
}
void wipe() {
clockwise();
counterClockwise();
halt();
}
void loop() {
waterValue = analogRead(waterSensor);
Serial.print("Value: ");
Serial.println(waterValue);
if (waterValue < 20)
halt();
else {
if (waterValue < 250)
haltTime = 2000;
else if (waterValue < 390)
haltTime = 1000;
else
haltTime = 0;
Serial.print("Halt: ");
Serial.println(haltTime);
wipe();
}
}
we got a couple limit switches to put on the bottom of the windshield & when triggered, would change the motor direction, but we haven't been able to get the code to work right since we attached the limit switches. we're all very inexperienced coders, but i had a CS major friend write the original code and the draft below:
const int switchL = 4;
const int switchR = 7;
const int waterSensor = A1;
const int in1 = 9;
const int in2 = 8;
bool switchLState = 0;
bool switchRState = 0;
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(switchL, INPUT);
pinMode(switchR, INPUT);
pinMode(waterSensor, INPUT);
Serial.begin(9600);
}
void clockwise() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
void counterClockwise() {
digitalWrite(in1, LOW);
digitalWrite(in2,HIGH);
}
void halt(int haltTime) {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
delay(haltTime);
}
bool getWaterValue() {
int waterValue;
waterValue = analogRead(waterSensor);
Serial.print("Value: ");
Serial.println(waterValue);
if (waterValue < 20 )
return false;
else if (waterValue < 250)
halt(1500);
else if (waterValue < 390)
halt(1000);
else
halt(500);
return true;
}
void loop() {
switchLState = digitalRead(switchL);
switchRState = digitalRead(switchR);
if (switchLState = HIGH) {
bool isWet = getWaterValue();
if (isWet)
clockwise();
}
else if (switchRState = HIGH)
counterClockwise();
}
(there was also a version of this code with SwitchL and SwitchR defined as const byte instead of const int, i don't really understand the difference though)
so now when i hook everything up nothing happens whatsoever and i have no idea what i'm doing any input whatsoever would be greatly appreciated thank u
UPDATE: the negative terminal on our motor had broken from numerous novice soldering jobs, but we got a new one and eventually got the code working. schematics are still the same. if anyone ever needs it:
const int switchL = 4;
const int switchR = 7;
const int waterSensor = A1;
int waterValue;
const int in1 = 9;
const int in2 = 8;
const int period = 500;
unsigned long time_now = 0;
bool switchLState = 0;
bool switchRState = 0;
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(switchL, INPUT);
pinMode(switchR, INPUT);
pinMode(waterSensor, INPUT);
Serial.begin(115200);
counterClockwise();
}
void clockwise() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
void counterClockwise() {
digitalWrite(in1, LOW);
digitalWrite(in2,HIGH);
}
void halt(int haltTime) {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
delay(haltTime);
}
bool getWaterValue(bool wasPaused) {
waterValue = analogRead(waterSensor);
Serial.print("Value: ");
Serial.println(waterValue);
if (!wasPaused) {
if (waterValue < 20 )
return false;
else if (waterValue < 290)
halt(2000);
else if (waterValue < 350)
halt(1000);
else
halt(0);
}
else { //wiper was paused, will resume with no delay once waterValue > 20
if (waterValue < 20)
return false;
}
return true;
}
void intervalReading() {
bool isWet = getWaterValue(true);
while (!isWet) {
delay(500);
isWet = getWaterValue(true);
}
clockwise();
}
void loop() {
switchLState = digitalRead(switchL);
switchRState = digitalRead(switchR);
if (switchRState == LOW) {
counterClockwise();
}
else if (switchLState == LOW) {
bool isWet = getWaterValue(false);
if (isWet)
clockwise();
else
intervalReading();
}
}