I have a super simple circuit and action I am trying to achieve but it doesn't work properly and I am not sure why.
I am trying to get a motor to turn on and rotate for a given amount of time (starting of with 1 or 2 seconds) by pressing a switch. I only want this occurrence to happen once when the switch has been pressed. Then it will only happen again if the switch is pressed again.
For some reason the motor completely ignores the action/question of if the button has been pressed or not and it repeatedly just spins on and off. I have tried all I can think of and have tested the motor separate to the push switch. I am able to turn it on and off without the switch. But no matter what as soon as i implement the switch it doesn't have any impact on the motor. I have also tested the switch circuit with an LED, getting the LED to do exactly what i want the motor to do and this went fine. So I am confused.
Components being used:
Momentary Push button with a 10k ohm resistor
3V DC motor with a PN2222 transistor, 220 ohm resistor and a 1N4001 diode.
My Code is as follows:
int buttonpin = 2;
int motor = 3;
void setup() { // initialize digital pin 13 as an output.
pinMode(motor, OUTPUT);
pinMode(buttonpin, INPUT);
}
// the loop function runs over and over again forever
void loop() {
if (digitalRead (buttonpin) == HIGH) // if button is pressed
{
Motor_on();
}
else
{
digitalWrite(motor,LOW);
}
}
void Motor_on(){
{
digitalWrite(motor, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second}
digitalWrite(motor, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
}
The motor and button is set up as followed:
Would love some assistance as I have done more complicated circuitry and projects than this on a PIC but I can't seem to figure out the solution to this, and this is only the first part of my project!