Unit 3 Arduino Project Tutorial
Analog code ________________________________________ const int buttonPin = 3; // The pins from which the code is activated from. const int motorPin = 9; // Motor pin is pin 9, pin where motor code is coming from int buttonState = 0; void setup() { pinMode(buttonPin, INPUT); // The push button is the input pinMode(motorPin, OUTPUT); // The motor is the output } void loop() { // The command buttonState = digitalRead(buttonPin); if (buttonState == LOW) { // Basically saying that if the button is on, the motor is on, and vice versa analogWrite(motorPin, 255); // The Motor will turn on to 255 speed } else analogWrite(motorPin, 0); // The motor will turn off to 0 speed } Digital Code __________________________________________________ const int buttonPin = 3; // The pins from which the code is activated from. const int motorPin = 9; // Motor pin is pin 9, pin where motor code is coming from int buttonState = 0; void setup() { pinMode(buttonPin, INPUT); // The push button is the input pinMode(motorPin, OUTPUT); // The motor is the output } void loop() { buttonState = digitalRead(buttonPin); // Is button on or off if (buttonState == LOW) { // If the button is on... digitalWrite(motorPin, HIGH); // The motor will turn on } else // Otherwise, the button is off, therefore the motor is off digitalWrite(motorPin, LOW); // The motor will turn off } Code Credits: http://arduino.cc/en/Reference/analogWrite http://arduino.cc/en/Reference/digitalWrite http://arduino.cc/en/Tutorial/IfStatement http://www.arduino.cc/en/Tutorial/Pushbutton ___________________________________________________ 3. Enjoy
|