Need help with Arduino 6amp fan motor controller sketch

Hi Everyone,

I’m needing to run a 6 amp x 12v fan motor over a time ( 1hr on and 1 hr off) during the day only.

Can someone point me in the right direction for code to turn off the sketch loop with an LDR with low light say <300.

So that the motor fan runs during the day only with the solar panel connected to the battery system.

I have the VMH5019 Pololo motor shield and Uno mounted and wired with test fan to check works for now and will connect to 6amp fan once this works well.

Now that I have this working correctly with 1hr on and 1 hour off ( see current sketch below), Im keen to get the LDR working to only turn the VMH5019 on when there is more than <300 on the LDR,
So if below <300 then turn off the Void Loop() and holds the sketch untill the light comes back ( ie daylight)

Come someone advise me which function I need to control the sketch on and off with LDR.

Best regards
Jason

My current sketch as of today:

include “DualVNH5019MotorShield.h”
const long oneSecond = 1000; // a second is a thousand milliseconds
const long oneMinute = oneSecond * 60;
const long oneHour = oneMinute * 60;
const long oneDay = oneHour * 24;

DualVNH5019MotorShield md;

int sensorPin = A0; // select the input pin for ldr, JON Need to find pin to turn off M1 motor Vcc?? //
int sensorValue = 0; // variable to store the value coming from the sensor

void setup()
{
Serial.begin(115200);
Serial.println(“Dual VNH5019 Motor Shield”);
md.init();
pinMode(2, OUTPUT); //pin connected to the VNH5019//
}

void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("LDR light reading of ");
Serial.println(sensorValue); //prints the values coming from the sensor on the screen

if(sensorValue < 300) //setting a threshold value
digitalWrite(2,HIGH); //turn VNH5019 ON//

else digitalWrite(2,LOW); //turn VNH5019 OFF//
delay(2);

//motor startup//
Serial.println(“M1 Speed 50% Forward”);// 400 is 100%//
md.setM1Speed(200);
Serial.print("M1 current: ");
Serial.println(md.getM1CurrentMilliamps());

Serial.print("Delay: ");
Serial.println(“delay for 1 hour”);
delay(oneHour);

// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("LDR light reading of ");
Serial.println(sensorValue); //prints the values coming from the sensor on the screen

if(sensorValue < 300) //setting a threshold value
digitalWrite(2,HIGH); //turn VNH5019 ON//

else digitalWrite(2,LOW); //turn VNH5019 OFF//
delay(2);

//motor shutdown//
Serial.println(“M1 Speed 0% stopped”);
md.setM1Speed(0);
Serial.print("M1 current: ");
Serial.println(md.getM1CurrentMilliamps());

Serial.print("Delay: ");
Serial.println(“delay for 1 hour”);
delay(oneHour);
}

Image of the arduino and shield

Hi,

google arduino ldr

Tom... :slight_smile:

f(sensorValue < 300) //setting a threshold value
digitalWrite(2,HIGH); //turn VNH5019 ON//

else digitalWrite(2,LOW); //turn VNH5019 OFF//

You already have code dependant on the value read from the LDR. Why not just make the motor code dependant on it too ?

As written the delay() function causes the code to stall for the delay period so the LDR cannot be read during that time. Look at the BlinkWithoutDelay example in the IDE to see how to use millis() for timing. Your “blinks” will be rather longer than the example but the principle is the same

Thanks for that, just trying to figure out how to make the motor dependant on the LDR for the code.

Not sure what code to use but still looking.

jason

just trying to figure out how to make the motor dependant on the LDR

Dependent in what way? Should the motor change color as more or less light falls in the LDR?

Realistic requirements are far easier to implement than wishful thinking.

Thanks Paul,
Say the motor shield is turned on at day break ie more than 300 on ldr then the sketch runs until ldr turns the motor shield off at below say 300.
So,
The loop function will run only if the ldr is >300 which is basically day time. And shuts off at night.
Cheers jason

One does not turn a motor shield on or off. One tells the motor shield to turn a motor in one direction or the other, at some speed.

Now, it is STILL not clear which direction the motor should turn when the LDR output is 567 or what the speed should be when the LDR output is 843.

Come on back when you CAN define both the direction and the speed, as functions of the LDR output.