Hi all
This is my first post here, im a newbie In programming, and im stuck In this code, and i hope that one of you experienced programmers Can help me out:)
The board is an Uno, and i will like to activate dc motor with a potentiometer and when the output value from potentiometer is 0, i will like to activate the dc motor with the pushbuttom.
Right now the dc motor is getting activated by the buttom but it’s only buzzing, not turning.
I hope the code is uploaded correctly:)
</>
const int switchPin = 2;
const int motorPin = 9;
const int potPin = A0; // test analog pin potentiometer
int switchState = 0;
int sensorValue = 0; // test sensor value pot
int outputValue = 0; // test analog output to the PWM
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(switchPin, INPUT);
Serial.begin(9600); // test initialize serial com at 9600
}
void loop() {
sensorValue = analogRead(potPin); // test read analog in value
outputValue = map(sensorValue, 0, 1023, 0, 255); // test range of analog output
analogWrite(motorPin, outputValue); // test changing output value motorspeed
// test print result to serial monitor
Serial.print("sensor=");
Serial.print(sensorValue);
Serial.print("\t output");
Serial.println(outputValue);
delay(2);
switchState = digitalRead(switchPin);
if (switchState == HIGH) {
digitalWrite(motorPin, HIGH);
}
else {
digitalWrite(motorPin, LOW);
}
}
</>
