Coding help for MD10 R2 Motor shield please

LINIX Brushless, VEXTA Brushless, RC Servo, DC Geared, Linear, Stepper, Tamiya.....

Coding help for MD10 R2 Motor shield please

Postby RescueL » Mon Dec 03, 2018 11:55 am

HI,
Im running a fan motor for startup and shutdown each hour via a LDR value on the light sensor ( to simulate daytime).

( to test code, set value delay to tenSeconds and not onehour)

I have the motor running as per the demo sketch and have added the restraints for the LDR and values for the monitor to output values.
The sketch loops each ten seconds fine but if i cover the LDR and get a low reading the fans still running after the 1st delay and does not shut off after the 1st tenSeconds delay.

My Logic is:
[list=]
Sketch starts
Checks for daytime reading
if yes then runs fan for 1 hour
then checks again after 1 hour
has a standdown for 2 hours
restarts of daytime, or if low LDR then does not turn on
[/list]

Constraints are:
LDR reading = Pin A0
Motor = D3 PWM
LDR to shut off motor if below value of 100, (600 to 700 being HIGH).

Have I got the code right for the motor shield??
Cheers
Jason

Code: Select all
const long oneSecond = 1000;  // a second is a thousand milliseconds
const long tenSeconds = oneSecond* 10;
const long oneMinute = oneSecond * 60;
const long oneHour   = oneMinute * 60;
const long twoHour   = oneHour * 2;

int sensorPin = A0;   // select the input pin for LDR//
int sensorValue = 0;  // variable to store the value coming from the sensor

// PWM is connected to pin 3.
const int pinPwm = 3;

// DIR is connected to pin 2.
const int pinDir = 2;



// Speed of the motor.
static int iSpeed = 255;

// Acceleration of the motor.
static int iAcc = 0;




// The setup routine runs once when you press reset.
void setup() { 

  Serial.begin(9600);
  Serial.println("Fan Motor Shield");
 
  // Initialize the PWM and DIR pins as digital outputs.
  pinMode(pinPwm, OUTPUT);
  pinMode(pinDir, OUTPUT);
}


void loop() {
 
  //Run motor 1//
  // 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

  //Daytime running test to start motor//
  if(sensorValue < 300) //setting LDR threshold value//
  analogWrite(pinPwm,LOW); //turn off//
 
  else analogWrite(pinPwm,HIGH); //turn ON//
  delay(2);
 
  // Control the motor according to the speed value.
  // Positive speed value = CW,
  // Negative speed value = CCW.
  if (iSpeed >= 255) {
    analogWrite(pinPwm, iSpeed);
    digitalWrite(pinDir, LOW);
  }
  else {
    analogWrite(pinPwm, iSpeed);
    digitalWrite(pinDir, HIGH);
  }
 
  // Increase/Decrease the speed according to the acceleration.
  iSpeed += iAcc;
 
  // Change the acceleration sign when full speed is reached.
  if ((iSpeed >= 255) || (iSpeed <= 255)) {
    iAcc = -iAcc;
  }
   
  delay(tenSeconds);

  //Run motor 1//
  // 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

  //Daytime running test to start motor//
  if(sensorValue < 300) //setting LDR threshold value//
  analogWrite(pinPwm,LOW); //turn off//
 
  else analogWrite(pinPwm,HIGH); //turn ON//
  delay(2);
 
  // Control the motor according to the speed value.
  // Positive speed value = CW,
  // Negative speed value = CCW.
  if (iSpeed >= 0) {
    analogWrite(pinPwm, iSpeed);
    digitalWrite(pinDir, LOW);
  }
  else {
    analogWrite(pinPwm, iSpeed);
    digitalWrite(pinDir, HIGH);
  }
 
  // Increase/Decrease the speed according to the acceleration.
  iSpeed += iAcc;
 
  // Change the acceleration sign when full speed is reached.
  if ((iSpeed >= 0) || (iSpeed <= 0)) {
    iAcc = -iAcc;
  }
   
  delay(tenSeconds);
}
RescueL
Fledgling
 
Posts: 1
Joined: Mon Dec 03, 2018 11:33 am

Return to DC Motor

Who is online

Users browsing this forum: No registered users and 2 guests

cron