Good thing I had a 9-volt battery in my garage. This project uses transistors and higher voltage to spin a pinwheel. When you press the button, the pinwheel spins.
constintswitchPin=2;constintmotorPin=9;intswitchState=0;voidsetup(){//Selecting as an input and output the switch and the motorpinMode(switchPin,INPUT);pinMode(motorPin,OUTPUT);}voidloop(){//Reading if the switch has been pushedswitchState=digitalRead(switchPin);if(switchState==HIGH){//If the switch has been pushed make the motor spindigitalWrite(motorPin,HIGH);}else{//If the switch hasn't been pushed don't make the motor spindigitalWrite(motorPin,LOW);}}
Comments