# Arduino
# Workshop 1
# Opdracht 3C
Laat een groen ledlampje twee maal sneller knipperen dan een gele variant.
void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(12, LOW);
delay(100);
}
# Opdracht 4C
Laat twee ledlampjes tegelijkertijd contradictorisch aan- en uit faden
int greenLedPin = 10;
int yellowLedPin = 9;
int rood = 0;
void setup() {
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
}
void loop() {
rood++;
for (int brightness=0; brightness < 256; brightness++) {
analogWrite(greenLedPin, brightness);
analogWrite(yellowLedPin, 256 - brightness);
delay(5);
}
delay(50);
for (int brightness=0; brightness < 256; brightness++) {
analogWrite(greenLedPin, 256 - brightness);
analogWrite(yellowLedPin, brightness);
delay(5);
}
}
# Opdracht 6C
Laat twee ledlampjes tegelijkertijd contradictorisch aan- en uit faden onder invloed van een potentiometer (draaiknop)
int sensorPin = A0; // variable for sensor pin
int greenLedPin = 10;
int yellowLedPin = 9;
float sensorValue = 0; // variable for sensor value
void setup() {
pinMode(sensorPin, INPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(yellowLedPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value/voltage on the sensor pin and
sensorValue = map(sensorValue, 10, 1023, 10, 255);
// store that value in the variable sensorValue
Serial.println(sensorValue); // print out sensorValue to the Serial Monitor
delay(50); // delay for 0.2 seconds
analogWrite(greenLedPin, sensorValue);
analogWrite(yellowLedPin, 255 - sensorValue);
}
# Opdracht 7C
Maak gebruik van de potentiometer om het matrix display te animeren. Ik heb de website LED Matrix Editor gebruikt om een array met matrix getallen te krijgen.
#include "LedControl.h"
int currValue = 0;
const uint64_t IMAGES[] = {
0x1c2222222222221c,
0x1c08080808080c08,
0x3e0408102020221c,
0x1c2220201820221c,
0x20203e2224283020,
0x1c2220201e02023e,
0x1c2222221e02221c,
0x040404081020203e,
0x1c2222221c22221c,
0x1c22203c2222221c
};
LedControl lc = LedControl(12, 11, 10, 1);
void displayImage(uint64_t image) {
for (int i = 0; i < 8; i++) {
byte row = (image >> i * 8) & 0xFF;
for (int j = 0; j < 8; j++) {
lc.setLed(0, i, j, bitRead(row, j));
}
}
}
void setup() {
lc.shutdown(0, false); // Turn matrix on, no power saving
lc.setIntensity(0, 9); // Set brightness to a medium value
lc.clearDisplay(0); // Clear the display
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0); // read sensor, 0-1023
Serial.println(sensorValue);
sensorValue = map(sensorValue, 0, 1000, 0, 9);
// remap the value 0-7. (1000 eliminates noise)
if (currValue != sensorValue) {
lc.clearDisplay(0); // Clear the display
displayImage(IMAGES[sensorValue]);
currValue = sensorValue;
}
}
# Workshop 2
# Opdracht 9B
Verander de code zodat de Arduino een liedje afspeelt
int speakerPin = 8;
void setup() {
pinMode(speakerPin, OUTPUT);
}
void loop() {
tone(8, 660, 100);
delay(150);
tone(8, 660, 100);
delay(300);
tone(8, 660, 100);
delay(300);
tone(8, 510, 100);
delay(100);
tone(8, 660, 100);
delay(300);
tone(8, 770, 100);
delay(550);
tone(8, 380, 100);
delay(575);
tone(8, 510, 100);
delay(450);
tone(8, 380, 100);
delay(400);
tone(8, 320, 100);
delay(500);
tone(8, 440, 100);
delay(300);
tone(8, 480, 80);
delay(330);
tone(8, 450, 100);
delay(150);
tone(8, 430, 100);
delay(300);
tone(8, 380, 100);
delay(200);
}
# Opdracht 9D
Bestuur het piezzo-element doormiddel van de LDR (lichtsensor)
int speakerPin = 8;
void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
int sensorValue = analogRead(A0); // read sensor, 0-1023
Serial.println(sensorValue);
sensorValue = map(sensorValue, 0, 800, 100, 2000);
tone(speakerPin, sensorValue, 101);
}
# Opdracht 10D
Laat een led-lampje aangaan wanneer de waarde van de NTC (warmtesensor) hoger is dan normaal
int ledPin = 13;
int sensorPin = A0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(sensorPin); // read sensorpin
Serial.println(sensorValue); // print to serial monitor
if (sensorValue >= 935) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
delay(10); // wait
}
// Cold: 920
// Hot: 950
# Opdracht 11D
Maak een social-distance indicator. Zorg ervoor dat 3 ledlampjes aangaan volgens deze tabel:
Measured distance | Led on |
---|---|
< 140 cm | Red |
> 140 cm & < 160 cm | Yellow |
> 160 cm | Green |
const int echoPin = 6;
const int trigPin = 7;
int ledPinGreen = 8;
int ledPinRed = 9;
int ledPinYellow = 10;
void setup() {
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(ledPinGreen, OUTPUT);
pinMode(ledPinRed, OUTPUT);
pinMode(ledPinYellow, OUTPUT);
Serial.begin(9600);
}
void loop() {
// send a pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
// wait for the echo
long duration = pulseIn(echoPin, HIGH);
// convert the time into a distance, the speed of sound is 29 microseconds per
//the pulse traveled forth and back, so we divided the distance by 2
int cm = duration / 29 / 2;
Serial.print(cm);
Serial.println("cm");
//if the measured distance is lower than 50 cm, turn on LED
if (cm < 50) {
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinGreen, LOW);
digitalWrite(ledPinRed, HIGH);
}
//else turn off LED
else if (cm > 50 && cm < 100) {
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinGreen, LOW);
digitalWrite(ledPinYellow, HIGH);
} else {
digitalWrite(ledPinRed, LOW);
digitalWrite(ledPinYellow, LOW);
digitalWrite(ledPinGreen, HIGH);
}
delay(100);
}
# Workshop 3
# Opdracht 12E
Laat het programma Processing interacteren met een sensor op de Arduino
Ardunio
float sensorValue = 0; // variable for sensor value
int sensorPin = A0; // variable for sensor pin
void setup() {
Serial.begin(9600); // Start the Serial connection at a
// speed of 9600 bps
pinMode(sensorPin, INPUT); // Input pin for potmeter or LDR
}
void loop() {
sensorValue = analogRead(sensorPin); // Read the value/current on the sensor pin and
// store that value in the variable sensorValue
sensorValue = (sensorValue / 1023) * 255; // Rescale the sensor's value.
Serial.println(sensorValue); // Print the sensorValue to the serial
// connection
delay(10); // Wait 0.1 seconds
}
Processing
import processing.serial.*;
Serial myPort;
String sensorReading="";
void setup() {
size(400, 400);
myPort = new Serial(this, Serial.list()[0], 9600); // instead of 5, choose what ever
myPort.bufferUntil('\n');
}
float growLine (float progress, float round) {
progress = 63.75 / (progress - 63.75 * round);
float line;
if (round >=2 ) line = 350 - 300 / progress;
else line = 50 + 300 / progress;
return constrain(line, 50, 350);
}
void draw() {
float value = float(sensorReading);
background(255);
fill(0);
ellipse(width/2, height/2, value, value);
float round = floor(value/63.75);
text("Sensor Reading: " + sensorReading + "R" + str(round), 20, 20);
strokeWeight(10 / (255 / value));
stroke(value, value, value);
if (round >= 0) line(50, 50, growLine(value, 0), 50);
if (round >= 1) line(350, 50, 350, growLine(value, 1));
if (round >= 2) line(350, 350, growLine(value, 2), 350);
if (round >= 3) line(50, 350, 50, growLine(value, 3));
}
void serialEvent (Serial myPort) {
sensorReading = myPort.readStringUntil('\n');
}
# Opdracht 13E
Laat het lampje branden als één van de twee buttons is ingedrukt
int ledPin = 13;
int buttonPinL = 3;
int buttonPinR = 2;
int stateL = 0;
int stateR = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPinL, INPUT);
pinMode(buttonPinR, INPUT);
Serial.begin(9600);
}
void loop(){
stateL = digitalRead(buttonPinL);
stateR = digitalRead(buttonPinR);
if (stateL || stateR) digitalWrite(ledPin, HIGH);
else digitalWrite(ledPin, LOW);
}
# Opdracht 14C
Bedien de servo-motor met een draaiknop
#include <Servo.h>
Servo myServo; // Declare a servo object for us to control
int servoPin = 9; // Var for the pin the servo connects to
int pos = 0; // Var to keep track of the servo's position
int sensorPin = A0; // variable for sensor pin
void setup() {
myServo.attach(servoPin); // Tell the servo to what pin it's connected to
}
void loop() {
int sensorValue = analogRead(sensorPin);
int servVal = map(sensorValue, 10, 1023, 0, 160);
myServo.write(servVal);
}
# Opdracht 15C
Maak een piano van (semi-)geleidend materiaal
#include <CapacitiveSensor.h>
CapacitiveSensor touchSwitchA =
CapacitiveSensor(13, 12);
//1 megohm resistor between pins 13 & 12,
CapacitiveSensor touchSwitchB =
CapacitiveSensor(11, 10);
//1 megohm resistor between pins 11 & 10, pin 10 is sensor pin
CapacitiveSensor touchSwitchC = CapacitiveSensor(9, 8);
//1 megohm resistor between pins 9 & 8, pin 8 is sensor pin
CapacitiveSensor touchSwitchD = CapacitiveSensor(7, 6);
//1 megohm resistor between pins 7 & 6, pin 6 is sensor pin
CapacitiveSensor touchSwitchE = CapacitiveSensor(5, 4);
//1 megohm resistor between pins 5 & 4, pin 4 is sensor pin
CapacitiveSensor touchSwitchF = CapacitiveSensor(3, 2);
//1 megohm resistor between pins 3 & 2, pin 2 is sensor pin
int speakerPin = A0;
void setup() {
Serial.begin(9600);
pinMode(speakerPin, OUTPUT);
}
void loop() {
//read sensor A
long valueA = touchSwitchA.capacitiveSensor(30);
valueA = constrain(valueA, 150, 3000);
valueA = map(valueA, 150, 3000, 0, 255);
if (valueA > 100) { //if sensor value is above threshold, play tone
tone(speakerPin, 349.23, 200); // plays a tone of 440Hz for 200ms
Serial.println("B");
}
//read sensor B
long valueB = touchSwitchB.capacitiveSensor(30);
valueB = constrain(valueB, 150, 3000);
valueB = map(valueB, 150, 3000, 0, 255);
if (valueB > 100) { //if sensor B value is above threshold, play tone
tone(speakerPin, 370, 200); // plays a tone of 493Hz for 200ms
Serial.println("A");
}
//read sensor C
long valueC = touchSwitchC.capacitiveSensor(30);
valueC = constrain(valueC, 150, 3000);
valueC = map(valueC, 150, 3000, 0, 255);
if (valueC > 100) { //if sensor value is above threshold, play tone
tone(speakerPin, 311, 200); // plays a tone of 262Hz for 200ms
Serial.println("C");
}
//read sensor D
long valueD = touchSwitchD.capacitiveSensor(30);
valueD = constrain(valueD, 150, 3000);
valueD = map(valueD, 150, 3000, 0, 255);
if (valueD > 100) { //if sensor value is above threshold, play tone
tone(speakerPin, 277, 200); // plays a tone of 330Hz for 200ms
Serial.println("D");
}
//read sensor E
long valueE = touchSwitchE.capacitiveSensor(30);
valueE = constrain(valueE, 150, 3000);
valueE = map(valueE, 150, 3000, 0, 255);
if (valueE > 100) { //if sensor value is above threshold, play tone
tone(speakerPin, 185, 200); // plays a tone of 262Hz for 200ms
Serial.println("E");
}
//read sensor F
long valueF = touchSwitchF.capacitiveSensor(30);
valueF = constrain(valueF, 150, 3000);
valueF = map(valueF, 150, 3000, 0, 255);
if (valueF > 100) { //if sensor value is above threshold, play tone
tone(speakerPin, 69, 200); // plays a tone of 262Hz for 200ms
Serial.println("F");
}
}