Thanks very much!, I've been trying testing your solutions and implementing what I can.
For point e), I tried to implement some changes in my code but i'm not sure if I understood fully. Could you please check my code below? (it still sometimes causes a malfunction with my version).
Can you also please tell me if there is a way to implement a safety to stop the motor if the Arduino malfunctions or if the motor gets stuck running? (I was planning to leave it on all day).
I tried using the watchdog timer but it seems to malfunction with the 'Arduino Micro' and then won't let you upload anything to it again without a fix. (I put wdt_enable(WDTO_8S)
in the setup and wdt_reset
in the loop (every few ms).
Thanks again for your help!
Paul
int motor_1_pins[] = {5, 6};
int speedpin = 4;
void setup()
{
Serial.begin(9600);
pinMode(speedpin, OUTPUT);
analogWrite(speedpin, 0);
pinMode(motor_1_pins[0], OUTPUT);
pinMode(motor_1_pins[1], OUTPUT);
}
//-------------------------------------------
//Motor Control
int motor_control(int directionz,int motorpin1, int motorpin2,int controlpin)
{
//Stop
analogWrite(controlpin, 0);
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, HIGH);
if(directionz==1) //Reverse
{
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
analogWrite(controlpin, 255);
Serial.println("Motor forwards");
}
if(directionz==3) //Forward
{
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
analogWrite(controlpin, 255);
Serial.println("Motor backwards");
}
}
//-------------------------------------------
//Main Program
void loop()
{
motor_control(1,motor_1_pins[0],motor_1_pins[1],speedpin);
delay(2000);
motor_control(0,motor_1_pins[0],motor_1_pins[1],speedpin);
delay(2000);
motor_control(3,motor_1_pins[0],motor_1_pins[1],speedpin);
delay(2000);
motor_control(0,motor_1_pins[0],motor_1_pins[1],speedpin);
delay(2000);
}