Hi Folks,
Im looking for help with my project as I am new to this and completely stuck. I am simply trying to power the DC Motor with the click of my button (switch in this case) but it wither runs automatically by itself once I run the code or it does not run at all, the switch makes zero difference. The code itself is fine , its the actual build of the circuit I am doing something wrong but I cannot figure it out by myself and help would be greatly appreciated. I have attached the specification. Thanks.
CODE
// named constants for the switch and motor pins
const int switchPin = 2; // the number of the switch pin
const int motorPin = 8; // the number of the motor pin
int switchState = 0; // variable for reading the switch’s status
void setup() {
// initialize the motor pin as an output:
pinMode(motorPin, OUTPUT);
// initialize the switch pin as an input:
pinMode(switchPin, INPUT);
}
void loop(){
// read the state of the switch value:
switchState = digitalRead(switchPin);
// check if the switch is pressed.
if (switchState == HIGH) {
// turn motor on:
digitalWrite(motorPin, HIGH);
}
else {
// turn motor off:
digitalWrite(motorPin, LOW);
}
}