Skip to content

Commit

Permalink
Add code snippets for Week 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Photon authored Sep 21, 2020
1 parent bcaedac commit f938d70
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 0 deletions.
22 changes: 22 additions & 0 deletions WK2/0_serialMonitor.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
________ ___ ___
/ __/ __ \/ _ | / _ \
_\ \/ /_/ / __ |/ , _/
/___/\____/_/ |_/_/|_|
Arduino Workshop Series
Session 2
> Serial Monitor output of a for loop index.
*/

void setup() {
Serial.begin(9600); //Set baudrate as 9600 bits per second (bps)
}

void loop() {
for(int i = 0 ; i <= 10; ++i) {
Serial.print(i); //Print current index
Serial.print(“\t”);
Serial.println(i*i); //Print i multiply i
delay(1000); // wait for 1 second
}
}
30 changes: 30 additions & 0 deletions WK2/1_ledFade.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
________ ___ ___
/ __/ __ \/ _ | / _ \
_\ \/ /_/ / __ |/ , _/
/___/\____/_/ |_/_/|_|
Arduino Workshop Series
Session 2
> Fading an LED.
*/

int ledPin = 3; // LED connected to digital pin 3

void setup() {
pinMode(ledPin, OUTPUT); //Declare as output mode
}

void loop() {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
analogWrite(ledPin, fadeValue); // sets the value (0 to 255)
delay(30); // wait for 30 milliseconds
}

// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
analogWrite(ledPin, fadeValue);
delay(30); // wait for 30 milliseconds
}
}

29 changes: 29 additions & 0 deletions WK2/1_readBtn.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
________ ___ ___
/ __/ __ \/ _ | / _ \
_\ \/ /_/ / __ |/ , _/
/___/\____/_/ |_/_/|_|
Arduino Workshop Series
Session 2
> Reading button input.
*/

const int buttonPin = 5; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

int buttonState = 0; // variable for reading the pushbutton status

void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
}

void loop() {
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED on
} else {
digitalWrite(ledPin, LOW); // turn LED off
}
}
48 changes: 48 additions & 0 deletions WK2/1_readBtnDebounce.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
________ ___ ___
/ __/ __ \/ _ | / _ \
_\ \/ /_/ / __ |/ , _/
/___/\____/_/ |_/_/|_|
Arduino Workshop Series
Session 2
> Reading button input, with debouncing.
*/

const int buttonPin = 5; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

int ledState = 0; // the current state of the output pin
int buttonState = 0; // variable for reading the pushbutton status
int lastButtonState = 0; // the previous reading from the input pin

unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
}

void loop() {
int reading = digitalRead(buttonPin); // read the state of the pushbutton value

// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debouncing timer
}

if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:

// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;

// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
}
28 changes: 28 additions & 0 deletions WK2/2_readPot.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
________ ___ ___
/ __/ __ \/ _ | / _ \
_\ \/ /_/ / __ |/ , _/
/___/\____/_/ |_/_/|_|
Arduino Workshop Series
Session 2
> Reading value of potentiometer.
*/

int potPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}

void loop() {
val = analogRead(potPin); // read the value from the sensor (0 <-> 1024)
Serial.println(val); // Print potentiometer value
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // stop the program for some time
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // stop the program for some time
}

37 changes: 37 additions & 0 deletions WK2/3_ldr_intensity_template.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
________ ___ ___
/ __/ __ \/ _ | / _ \
_\ \/ /_/ / __ |/ , _/
/___/\____/_/ |_/_/|_|
Arduino Workshop Series
Session 2
> Light Intensity Display (Template).
*/

//Pin setup
const int LEDS[4] = {12,11,10,9};
const int ldr_pin = A0;

int lightIntensity = 0; //LDR value, intensity of LDR
void setup()
{
Serial.begin(9600); //Start serial
//Setup pins
for(int i=0;i<4;++i){
pinMode(LEDS[i], OUTPUT);
}
pinMode(ldr_pin, INPUT);
}

void loop()
{
//Read LDR

//Process the LDR values

//Set the LEDs accordingly


//Print serial output
Serial.println(lightIntensity);
}

0 comments on commit f938d70

Please sign in to comment.