| 1 |
/* |
| 2 |
Arduino Starter Kit example |
| 3 |
Project 9 - Motorized Pinwheel |
| 4 |
|
| 5 |
This sketch is written to accompany Project 9 in the Arduino Starter Kit |
| 6 |
|
| 7 |
Parts required: |
| 8 |
- 10 kilohm resistor |
| 9 |
- pushbutton |
| 10 |
- motor |
| 11 |
- 9V battery |
| 12 |
- IRF520 MOSFET |
| 13 |
- 1N4007 diode |
| 14 |
|
| 15 |
created 13 Sep 2012 |
| 16 |
by Scott Fitzgerald |
| 17 |
|
| 18 |
https://store.arduino.cc/genuino-starter-kit |
| 19 |
|
| 20 |
This example code is part of the public domain. |
| 21 |
*/ |
| 22 |
|
| 23 |
// named constants for the switch and motor pins |
| 24 |
const int switchPin = 2; // the number of the switch pin |
| 25 |
const int motorPin = 9; // the number of the motor pin |
| 26 |
|
| 27 |
int switchState = 0; // variable for reading the switch's status |
| 28 |
|
| 29 |
void setup() { |
| 30 |
// initialize the motor pin as an output: |
| 31 |
pinMode(motorPin, OUTPUT); |
| 32 |
// initialize the switch pin as an input: |
| 33 |
pinMode(switchPin, INPUT); |
| 34 |
} |
| 35 |
|
| 36 |
void loop() { |
| 37 |
// read the state of the switch value: |
| 38 |
switchState = digitalRead(switchPin); |
| 39 |
|
| 40 |
// check if the switch is pressed. |
| 41 |
if (switchState == HIGH) { |
| 42 |
// turn motor on: |
| 43 |
digitalWrite(motorPin, HIGH); |
| 44 |
} else { |
| 45 |
// turn motor off: |
| 46 |
digitalWrite(motorPin, LOW); |
| 47 |
} |
| 48 |
} |