Arduino Programming, from novice to ninja

Official website of the course

Quiz 01
-------
1. What are the two blocks of code which must be present for every Arduino program (hint : two answers are correct!)?
- setup
- loop
2. An LED is connected to pin 2 (with the other leg connected to GND). Which of the functions below will light up the LED?
- digitalWrite(2, HIGH);
3. If I want to control an LED on pin 6, what line must be in the setup block?
- pinMode(6, OUTPUT);
4. If I type delay(10000) and then run the program, this will create a delay of how many second(s)?
- 10
5. If I want to define a new variable called pinLed, which is an integer variable with a value of 13, what is the correct syntax?
- int pinLed = 13;

Lab 1
-----
// Traffic light
void setup(){
pinMode(2,OUTPUT); // red
pinMode(3,OUTPUT); // orange
pinMode(4,OUTPUT); // green
}

void loop(){
digitalWrite(4,HIGH);
delay(3000);
digitalWrite(4,LOW);
digitalWrite(3,HIGH);
delay(1000);
digitalWrite(3,LOW);
digitalWrite(2,HIGH);
delay(3000);
digitalWrite(2,LOW);
}

/*
Lab 1 : three-colour traffic light

Switch on and off three LEDs to build a traffic light as follow:
- green LED for 3 seconds
- orange LED for 1 second
- red LED for 3 seconds

created 6 June 2018
by Baptiste Gaultier

This example code is in the public domain.
*/

// our three pin numbers to which LEDs are attached to
int red = 2;
int orange = 3;
int green = 4;

// the setup function runs once when you press reset or power the board
void setup() {
// initialize three digital pins as outputs:
pinMode(red, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(green, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(green, HIGH); // turn the LED on (HIGH is the voltage level)
delay(3000); // wait for 3 seconds
digitalWrite(green, LOW); // turn the LED off by making the voltage LOW

digitalWrite(orange, HIGH);
delay(1000);
digitalWrite(orange, LOW);

digitalWrite(red, HIGH);
delay(3000);
digitalWrite(red, LOW);
}

Quiz 02
-------
1. The digitalRead function reads the state of a digital pin. What can this function return (note: this question has several answers)?
- HIGH
- LOW
2. Let's consider the theory we covered this week. Given the following circuit:
- 340
3. If I want to read a button on pin 6, what code should the 'setup' block contain?
- pinMode(6, INPUT);
4. I want to use an LED connected to pin 12. What is the correct syntax to declare my constant?
- const int ledPin = 12;
5. In the following program: ... How many seconds will pass between one reading of buttonState and the next?
- 5

Lab 2
-----
/*
Three-colour traffic light with pedestrian light

Week 2 lab assignment for

Assembly:
* One red LED on pin 12 in series with a 220Ί resistor
* One orange LED on pin 11 in series with a 220Ί resistor
* One green LED on pin 10 in series with a 220Ί resistor

* One red LED on pin 9 in series with a 220Ί resistor
* One green LED on pin 8 in series with a 220Ί resistor

* Push-button with one pin connected to pin 2 and the other to +5V
* A 1KΊ resistor connected across pin 2 and GND

crĂŠĂŠ le 9 Avril 2014
par Baptiste Gaultier
avec les conseils de fb251

translated by Sam Roots, July 2018

Licensed under CC0 1.0 Universal

https://www.france-universite-numerique-mooc.fr/courses/MinesTelecom/040...
*/

// Initialise LED constants ('ped' = pedestrian)
const int red = 12;
const int orange = 11;
const int green = 10;

const int pedRed = 9;
const int pedGreen = 8;

// Push button on pin 2
const int buttonPin = 2;

// Declaring a variable for the button state
int buttonState = 0;

///////////////// SETUP FUNCTION - Executed once on start-up
void setup() {
// Tell the board which pins are input and output:
pinMode(red, OUTPUT);
pinMode(orange, OUTPUT);
pinMode(green, OUTPUT);

pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);

pinMode(buttonPin, INPUT);
}

///////////////// LOOP FUNCTION - executed over and over
void loop() {
// During normal functioning, the pedestrian light is always red
digitalWrite(pedRed, HIGH);

// Normal function for the 'car' traffic light (part 1)
// - turn on the green light and wait 3 seconds
digitalWrite(green, HIGH);
delay(3000);
digitalWrite(green, LOW);

// read state of the button and store the result in buttonState:
buttonState = digitalRead(buttonPin);

// If buttonState is equal to HIGH
// then the button is pressed
if (buttonState == HIGH) {
digitalWrite(orange, HIGH);
delay(1000);
digitalWrite(orange, LOW);

digitalWrite(red, HIGH);

// the pedestrian light turns green for 5s
digitalWrite(pedRed, LOW);
digitalWrite(pedGreen, HIGH);

delay(5000);

// put the pedestrian light back to red
digitalWrite(pedRed, HIGH);
digitalWrite(pedGreen, LOW);

// Make sure the red car traffic light is off
digitalWrite(red, LOW);
}
else {
// Otherwise, normal functioning of the car traffic light continues
digitalWrite(orange, HIGH);
delay(1000);
digitalWrite(orange, LOW);

digitalWrite(red, HIGH);
delay(3000);
digitalWrite(red, LOW);
}
}

Quiz 03
-------
1. I store the result of the analogRead function in a variable called myVariable. I want to use the map function to scale it into the following interval: 0 → 100. The result will be stored in the variable called myNewVariable. What is the correct syntax?
- myNewVariable = map(myVariable, 0, 1023, 0, 100);
2. The analogRead function allows us to read the state of an analog pin. What could this function return, in theory (several possible choices)?
- 0
- 255
- 10
3. I want my Arduino program to send text to the computer (at a speed of 9600 bauds). What should the setup block contain?
- Serial.begin(9600);
4. The LED has a polarity, or direction. From your knowledge, the course and the wiki, what will happen if the LED called LED1 in the circuit below is connected in reverse, as shown?
- Neither LED will light up
5. Given the following program and the circuit we saw in the "Button" example last week: At each cycle of the loop code, what value will be sent to the LED connected to pin 13 if I press the button connected to pin 2
- LOW

Lab 3
-----
/*
Light theremin

Assembly:
* Piezo buzzer on pin 8
* Photoresistor attached to pin A2 and also +5V
* 10kΊ resistor attached to A2 and also GND

crĂŠĂŠ le 9 Avril 2014
par Baptiste Gaultier
Created under license CC0 1.0 Universal
Translated by Sam Roots July 2018

https://www.france-universite-numerique-mooc.fr/courses/MinesTelecom/040...
*/

// Variable to store the value detected on pin A2
int sensorValue;

void setup() {
}

void loop() {
// Read the value on the photoresistor and then store it in sensorValue
// Note that on the assembly diagram above, it is on A0 not A0
sensorValue = analogRead(A2);

// re-scale the variable to something that makes more sense as a buzzer input
int pitch = map(sensorValue, 0, 1023, 50, 30000);

// play the calculated 'pitch' value from the buzzer on pin 8
tone(8, pitch);

// wait 10 ms for the pitch to play
delay(10);
}

Quiz 04
-------
1. What will happen if LEDs are connected to all the digital pins (numbered from 0 to 13)?
- The LEDs connected to pins 2 to 7 will light up
2. I want to use a servomotor connected to pin 9 and that I call myServo. What must my program contain?
- myServo.attach(9);
3. In an assembly with a capacitor placed in parallel with a push button. If instead of a capacitor of 100nF I put a 100ÂľF instead, what happens?
- You have to press the button for longer before it is detected
4. How many times will "meuh" be displayed in the serial monitor?
- 8
5. What will we see in the serial monitor when this code is executed?
- bip bip meuh bip bip meuh

Lab 4
-----
/*
Feu bicolore et barrière

Le montage :
* Une LED rouge sur la broche 3 en serie avec une resistance de 220Ί
* Une LED verte sur la broche 4 en serie avec une resistance de 220Ί

* Un servomoteur branchĂŠ sur les broches 9, +5V et GND

* Bouton poussoir branchĂŠ sur la broche 2 depuis +5V
* Une rÊsistance de 1KΊ bracnhÊ sur la broche 2 depuis GND

crĂŠĂŠ le 18 Avril 2014
par Baptiste Gaultier

Ce code est en CC0 1.0 Universal

https://www.france-universite-numerique-mooc.fr/courses/MinesTelecom/040...

*/

#include

Servo servo; // crĂŠation de l'objet servo issu
// du moule Servo

// Initialisation des constantes
const int bouton = 2;

const int ledRouge = 3;
const int ledVerte = 4;

// DĂŠclaration des variables :
int etatBouton = 0;
int pos = 0;

// le code dans cette fonction est exĂŠcutĂŠ une fois au dĂŠbut
void setup() {
// on souhaite communiquer avec l'ordinateur
Serial.begin(9600);

// indique que les broches des LED
// sont des sorties :
pinMode(ledRouge, OUTPUT);
pinMode(ledVerte, OUTPUT);

// indique que la broche bouton est une entrĂŠe :
pinMode(bouton, INPUT);

// on accroche notre servomoteur branchĂŠ sur la broche 9
servo.attach(9);

// allume le feu rouge
digitalWrite(ledRouge, HIGH);

// positionne la barrière horizontalement
servo.write(0);
}

// le code dans cette fonction est exĂŠcutĂŠ en boucle
void loop(){
// read the state of the pushbutton value:
etatBouton = digitalRead(bouton);

// si le bouton est appuyĂŠ
if (etatBouton == HIGH) {
// alors on envoie un message sur le moniteur sĂŠrie
Serial.print("Bouton appuye");

// puis on remonte la barrière de 90°
for(pos = 0; pos <= 90; pos++) {
servo.write(pos);
delay(15);
}

// puis on allume le feu vert durant 5 secondes
digitalWrite(ledRouge, LOW);
digitalWrite(ledVerte, HIGH);
delay(5000);

// et on repasse au rouge
digitalWrite(ledVerte, LOW);
digitalWrite(ledRouge, HIGH);

// enfin, on redescend la barrière
for(pos = 90; pos>=0; pos--) {
servo.write(pos);
delay(15);
}
}
}