New to Coding and Arduino, I picked up a starter kit and i have been trying to write the code for the fan module that came with the kit, but it is not performing as intended.
When code below is ran; the motor immediately turns on and I am unable to control it with the POT or the buttonpin.
After a while, motor speed will pick up the orange light on my board lights up and I basically cannot upload any new sketches to the project.
I am not sure as to what I have done wrong, I have followed the example given and have tried other possible solutions to the code online but none have worked.
The delay is set so I have a few seconds to upload blank sketch after plugging/unplugging board in. Thank you so much in advance!
const int buttonPin =11;
const int STATUS_LED =13;
int MOTOR_FORWARD =9;
int MOTOR_REVERSE =10;
int potPin =A0;
int MOTOR_MIN_SPEED =0;
int MOTOR_SPEED;
int MOTOR_MAX_SPEED =150;
int POT_VAL =0;
int buttonState =0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin,INPUT);
pinMode(MOTOR_REVERSE,OUTPUT);
pinMode(MOTOR_FORWARD,OUTPUT);
pinMode(STATUS_LED,OUTPUT);
}
void loop() {
buttonState =digitalRead(buttonPin);
POT_VAL = analogRead (potPin);
MOTOR_SPEED = map (POT_VAL, 0, 1023, MOTOR_MIN_SPEED, MOTOR_MAX_SPEED);
analogWrite (MOTOR_SPEED, potPin);
if (buttonState ==LOW){
analogWrite(MOTOR_FORWARD,MOTOR_MAX_SPEED);
analogWrite(MOTOR_REVERSE,MOTOR_MIN_SPEED);
digitalWrite(STATUS_LED,HIGH);
}
if (buttonState ==HIGH){
analogWrite(MOTOR_REVERSE,MOTOR_MAX_SPEED);
analogWrite(MOTOR_FORWARD,MOTOR_MIN_SPEED);
digitalWrite(STATUS_LED,LOW);
}
delay(8000);
}