My project is to create a system to automate the currently manual luggage cover on my car. When the boot(trunk) is opened the cover retracts and when the boot closes it extends. It is needed to ensure that luggage is not encroaching on the space needed for the roof to fold into.
The system works by using a magnetic switch to sense if the boot is open or closed and limit switches on the bulkhead (between the boot and passenger space) and on the rear valance. These limit switches detect cover position and signal the motor to switch off when the cover has either fully extended or retracted. I’m also using the current sensing capabilities of the 5019 to detect if the cover has jammed and if so, switch it off.
I am a complete newbie to programming (unless you count a bit of BBC Basic aeons ago), but have watched a number of Youtube courses/videos and read many blogs and articles over the last few weeks.
My hardware is a UNO rev3 and a Pololu 5019 motor control board to deliver the current needed.
I’ve written a sketch, see below, which compiles and uploads OK but is not working as expected.
I imagined that I would be able to control the motor direction and switch off by setting the input pins to the switches to combinations of either high(5v) or low(ground).
However, having built the circuit, I find that both motor direction pins , inA and inB, deliver 5v constantly (high) and changing the configuration of the switch pins has no effect.
I’m puzzled as to why this won’t work and would really appreciate it if someone could review my code and give me a clue as to where I’m going wrong. Also apologies for the excessive commenting but this is for my benefit.
* Switch on motor from proximity switch input, check limit switch settings, set speed, switch off when action completed
*/
int prox = 12; //the input from trunk proximity switch (No)
int inA = 11; // motor direction
int inB = 10; // motor direction
int motorStalled = 9; // current sense input to detect jammed motor
int lsBH = 8; //limit switch retract(bulkhead) (NO)
int lsRV = 7; // limit switch extend (rear valance) (NO)
int motorspeed = 6; //speed of motor (0-255)
int proxval = 0; // variable to store prox value
int lsBHval = 0; // variable to store lsBH value
int lsRVval = 0; // variable to store LSRV value
int motorStalledval = 0; // current draw value
void setup() {
pinMode(prox, INPUT);
pinMode(inA, OUTPUT);
pinMode(inB, OUTPUT);
pinMode(motorStalled, INPUT);
pinMode(lsBH, INPUT);
pinMode(lsRV, INPUT);
pinMode(motorspeed, OUTPUT);
}
void loop()
{
{
proxval = digitalRead(prox); // read the prox switch
digitalWrite(prox, proxval); // save prox value
}
{
lsBHval = digitalRead(lsBH); // read the lsBH limit switch
digitalWrite(lsBH, lsBHval); // save lsBH value
}
{
lsRVval = digitalRead(lsRV); // read the lsRV limit switch
digitalWrite(lsRV, lsRVval); // save lsRV value
}
// retract cover as boot lid opens
if (proxval == HIGH && lsBHval == LOW );//proximity switch open and bulkhead switch open.
{
analogWrite(motorspeed, 10); //set motor speed
digitalWrite(inA, HIGH); //set motor direction
digitalWrite(inB, LOW); //set motor direction
}
if (lsBHval == HIGH);// switch engaged
{
digitalWrite(inA, LOW); //stop motor
digitalWrite(inB, LOW);
}
if (motorStalledval >= 840); //equates to a current draw of 6A
{
digitalWrite(inA, LOW); //stop motor
digitalWrite(inB, LOW);
// Extend cover as boot lid closed
if (proxval == LOW && lsRVval == LOW );//proximity switch open and bulkhead switch open.
{
analogWrite(motorspeed, 10); //set motor speed
digitalWrite(inA, LOW); //set motor direction
digitalWrite(inB, HIGH); //set motor direction
}
if (lsRVval == HIGH);// switch engaged
{
digitalWrite(inA, LOW); //stop motor
digitalWrite(inB, LOW);
}
if (motorStalledval >= 840); //equates to a current draw of 6A
{
digitalWrite(inA, LOW); //stop motor
digitalWrite(inB, LOW);
}
}}