I have a sketch that I can get to run off of built in delays, so I know the hardware works but my code is not sufficient to do what I want. The Code runs a motor that runs and changes direction based off of 2 proximity sensors that are on opposite ends of a track.
I.E. If sensor 1 is triggered.. the motor operates one direction until sensor 2 is triggered, which sends the motor back to sensor 1. There must be a short delay after the motor starts in either direction to clear the magnet that triggers the sensor.
I think I am poorly coding the If statements and the system will not move. The serial print out is attached as a small .png showing that the counter is being incremented and the sensors are working properly.
const int motor1Pin = 2; // H-bridge leg 1 (pin 7, 1A)
const int motor2Pin = 6; // H-bridge leg 2 (pin 8, 2A)
const int enablePin = 10; // H-bridge enable pin to power motor
const int SensorOpen = 0; //Proximity Sensor for Magnet near motor Analog 0
const int SensorClose = 1; //Proximity Sensor for Magnet at end of track Analog 1
int counter = -1;
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13, 9, 5, 4, 3, 12);
void setup() {
// make sure all pins do not start or "coast"
digitalWrite(enablePin, LOW);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(SensorOpen, INPUT);
pinMode(SensorClose, INPUT);
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD. (Will stay)
lcd.print("Number of Cycles");
delay(2000);
}
void loop() {
//Read the Proximity Sensor Values
int Open = analogRead(SensorOpen);
int Close = analogRead(SensorClose);
Serial.println(Open);
Serial.println(Close);
delay(500);
if (Close > 1000) {
//Will Stop the Motor upon trigger of sensor
//Delay and reverse direction (Open the Cover)
digitalWrite(enablePin, LOW);
delay(6000); // time for motor to cool down. (Change to 1 minute)
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
analogWrite(enablePin, 255);
delay(5000); // time to clear the sensor
}
if (Open > 1000) {
//Will Stop the Motor upon trigger of sensor
//Delay for 5 seconds and reverse direction (Close the Cover)
digitalWrite(enablePin, LOW);
delay(6000); // time for motor to cool
digitalWrite(motor2Pin, LOW);
digitalWrite(motor1Pin, HIGH);
analogWrite(enablePin, 255);
delay(5000); // time to clear the proximity sensor
counter++;
// delay(23000); // test using time instead of sensors
delay(4000);
// delay(15000);
}
Serial.println(counter);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of cycles since reset:
lcd.print(counter);
}
const int SensorOpen = 0; //Proximity Sensor for Magnet near motor Analog 0
const int SensorClose = 1; //Proximity Sensor for Magnet at end of track Analog 1
It's always a good idea to use Pin on the name of variables that hold pin numbers.
Analog pins are input only. It is not necessary (or possible) (and does not do what you expect) to set the mode of analog pins.
int Open = analogRead(SensorOpen);
int Close = analogRead(SensorClose);
Serial.println(Open);
Serial.println(Close);
delay(500);
Regardless of the value read from either pin, stick your head in the sand for half a second. Why?
analogWrite(enablePin, 255);
The enable pin defines whether the motor moves, or not. It is silly to use a PWM pin for this purpose, and it is silly to use analogWrite() instead of digitalWrite().
You need to pretend that the track has only one end. Read the state of the sensor on that end. if the device is near, send it away, wait some period of time to allow the device to get away.
Then, pretend that only the other end exists, and do the same with the other sensor.
Run the motor until the sensor at the end becomes triggered.
Then change the mor direction and run it until the sensor at the other end becomes triggered.
Repeat.
If you need the motor to cool down and if the code does nothing else then use a delay() to achieve it. Otherwise use millis() for timing the cool down period.
The enable pin defines whether the motor moves, or not. It is silly to use a PWM pin for this purpose, and it is silly to use analogWrite() instead of digitalWrite().
Not quite in my opinion. I agree that digitalWrite would work in this case as well. But if the OP ever wants to control the speed, it can be done with analogWrite on the enable pin.
This principle is used with the Arduino motorshield where the two enable pins are connected to PWM outputs of the Arduino.
I am posting the void loop() only for ease of reading. Does this seem reasonable?
void loop() {
//Read the Proximity Sensor Values
int Open = analogRead(SensorOpen);
int Close = analogRead(SensorClose);
// int OpenRange = map(Open, 0, 1023, 0, 5);
// int CloseRange = map(Close, 0, 1023, 0, 5);
Serial.println(Open);
Serial.print("\t");
Serial.print(Close);
Serial.println("-------");
counter++;
if (Close > 1000) {
//Will Stop the Motor upon trigger of sensor
//Delay and reverse direction (Open the Cover)
digitalWrite(enablePin, LOW);
delay(5000); // time for motor to cool down. (Change to 1 minute)
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(enablePin, HIGH);
delay(5000); // time to clear the sensor
}
if (Open > 1000) {
//Will Stop the Motor upon trigger of sensor
//Delay for 5 seconds and reverse direction (Close the Cover)
digitalWrite(enablePin, LOW);
delay(5000); // time for motor to cool
digitalWrite(motor2Pin, LOW);
digitalWrite(motor1Pin, HIGH);
digitalWrite(enablePin, HIGH);
delay(5000); // time to clear the proximity sensor
counter++;
delay(5000);
}
Serial.println(counter);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of cycles since reset:
lcd.print(counter);
}
Not quite in my opinion. I agree that digitalWrite would work in this case as well. But if the OP ever wants to control the speed, it can be done with analogWrite on the enable pin.
I think that enablePin is a misnomer. It is really setting the direction of the motor. One can hardly set the direction to 0.75.
The speed pin should be an analog pin, and analogWrite() should be used to control the speed.