Skip to content

Instantly share code, notes, and snippets.

@mrichardson23
Created June 14, 2013 17:45
Show Gist options
  • Save mrichardson23/5783841 to your computer and use it in GitHub Desktop.
Save mrichardson23/5783841 to your computer and use it in GitHub Desktop.
Sample Code for Arduino Programming Basics Class
// Sample Code for Arduino Programming Basics Class
// Matt Richardson http://mattrichardson.com/
// With this code, use a button to try out analogWrite.
// It will dim an LED when you press it.
// Wire up a button to digital pin 2:
#define BUTTON_PIN 2
// Wire up an LED to pin 3:
#define GREEN_LED 3
// Everything in the setup() function runs once when the Arduino starts up.
void setup() {
pinMode(GREEN_LED, OUTPUT); // Set the LED pin to digital output.
pinMode(BUTTON_PIN, INPUT); // Set the button to digital input.
}
// Everything in the loop() function runs over and over again after setup() is finished.
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) { // Is the button pressed?
analogWrite(GREEN_LED, 10); // then make the LED dim.
}
else { // Otherwise,
analogWrite(GREEN_LED, 255); // make the LED bright.
}
}
// Sample Code for Arduino Programming Basics Class
// Matt Richardson http://mattrichardson.com/
// With this code, use a potentiometer (dial) to activate
// three LEDs and write the value via serial.
// Wire up a button to digital pin 2:
#define BUTTON_PIN 2
// Wire up three LEDs to digital pins 3, 4, and 5:
#define GREEN_LED 3
#define YELLOW_LED 4
#define RED_LED 5
// Wire up a potentiometer to analog input pin 0:
#define POTENTIOMETER 0
// Create a new variable with the type integer, call it value,
// and set it to zero.
int value = 0;
// Everything in the setup() function runs once when the Arduino starts up.
void setup() {
// Set the LED pins to digital output:
pinMode(GREEN_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(RED_LED, OUTPUT);
// Set the button to digital input:
pinMode(BUTTON_PIN, INPUT);
// Set up the serial communication at 115200 baud:
Serial.begin(115200);
}
// Everything in the loop() function runs over and over
// again after setup() is finished.
void loop() {
// Take the reading from the potentiometer's analog input pin,
// and store it in the variable value:
value = analogRead(POTENTIOMETER);
// Remap the value, which had the range 0 - 1023 to 0 - 100:
value = map(value, 0, 1023, 0, 100);
// Print the number stored in value to serial:
Serial.println(value);
if (value > 25 ) { // is value greater than 25?
digitalWrite(GREEN_LED, HIGH); // then turn the green LED on.
}
else { // otherwise, turn it off.
digitalWrite(GREEN_LED, LOW);
}
if (value > 50) { // is value greater than 50?
digitalWrite(YELLOW_LED, HIGH); // then turn the yellow LED on.
}
else {
digitalWrite(YELLOW_LED, LOW); // otherwise, turn it off.
}
if (value > 75) { // is value greater than 75?
digitalWrite(RED_LED, HIGH); // then turn the red LED on.
}
else {
digitalWrite(RED_LED, LOW); // otherwise turn it off.
}
}
// Sample Code for Arduino Programming Basics Class
// Matt Richardson http://mattrichardson.com/
// With this code, make an LED fade up and down using a
// for loop!
// Wire up an LED to pin 3 (or any PWM pin, they're marked with a ~)
#define GREEN_LED 3
// Everything in the setup() function runs once when the Arduino starts up.
void setup() {
pinMode(GREEN_LED, OUTPUT); // set the LED pin to an output.
}
// Everything in the loop() function runs over and over
// again after setup() is finished.
void loop() {
// syntax: for (INITIALIZER, CONDITION, AFTERTHOUGHT) { CODE }
// pseudocode: At the start of this loop, set up a spot in memory called i
// and set it to 0. Run this loop as long as i is less than or equal to 255.
// At the end of every iteration, increment i by one.
// This will fade the LED up slowly:
for (int i = 0; i <= 255 ; i++) {
analogWrite(GREEN_LED, i); // analog write the value of i to the green LED.
delay(5); // slow things down a touch.
}
// When that loop is done, go into the next loop to fade the LED down slowly:
for (int j = 255; j > 0; j--) {
analogWrite(GREEN_LED, j);
delay(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment