Hi
I am trying to use Arduino PWM to control motor speed. The aim is that when someone turns on the switch, this sends a high input to one of the Arduino pins (12). Then when pin 12 goes high, I want pin 11 to also go high in order to turn on the transistor which controls the motor. I want to keep pin 11 on full voltage for around 5 secs to give a “push” to the motor, then I want to use PWM to moderate the speed. My code is shown below:
int switchPin = 12;// this is the arduino pin which is attached to the switch
int TransistorPin =11;
void setup()
{
pinMode(TransistorPin, OUTPUT);//this is the pin which turns on the transistor
pinMode(switchPin, INPUT);
}
void loop()
{
while(true){
if (digitalRead(switchPin)==LOW){//if the switch is off, keep the motor off
digitalWrite(TransistorPin, LOW);
}
if(digitalRead(switchPin)==HIGH){//if switch is high, turn on motor
digitalWrite(TransistorPin, HIGH);
delay(5);//keep motor on at full voltage for 5 seconds
while (digitalRead (switchPin)==HIGH){
digitalWrite(TransistorPin, HIGH);//then begin pwm
delayMicroseconds(3000); //this ratio gave roughly the right speed
digitalWrite(TransistorPin, LOW);
delayMicroseconds(1000);
}
digitalWrite(TransistorPin, LOW);//turning off again
}
}
}
However, this is not working, and the chassis on which the motor is mounted needs a small shove to start moving. Any ideas where I might be going wrong? Thanks!