Assignment 2 - Working circuit
Working circuit with led and sensor
Crystal analog switch



Reflection
Last updated
Working circuit with led and sensor



Last updated
/*
created by David Cuartielles
modified 30 Aug 2011
By Tom Igoe
modified 13 March 2020
By Loes Bogers
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int sensorPin = A0; // select the input pin for the potentiometer
int ledPinRood = D1; // select the pin for the LED that has PWM (~) e.g pin D1 on Node MCU
int sensorValue = 0; // variable to store the value coming from the sensor
int mappedValue = 0; //store the mapped values
int ledPinGroen = D2;
void setup() {
// initialize serial communication with computer:
Serial.begin(115200); //make sure it matches with the number in the serial port
// declare the ledPin as an OUTPUT:
pinMode(ledPinRood, OUTPUT);
pinMode(sensorPin, INPUT);
pinMode(ledPinGroen, OUTPUT);
}
void loop() {
delay(1000);
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
//// FIND MIN & MAX RANGE FIRST, THEN COMMENT OUT
// print values to serial to find lowest and highest values (min and max) in serial monitor
Serial.println(sensorValue); // met 10K voltage divider: bijv. range 100-850 zonder een fietslampje erbij (beter voor de video)
//PUT THE VALUES YOU FOUND (YOUR_MIN, YOUR_MAX) INTO THE LINE BELOW (line 36)
//put your min value and max value (as seen in the monitor) and map to a range 0- 255 for output
mappedValue = map(sensorValue, 242, 1024, 0, 255);
// print values for debugging
Serial.print("Old Value (no mapping) = ");
Serial.print(sensorValue);
Serial.print("\t"); // add a tab between the numbers
Serial.print("New value (mapped) = ");
Serial.println(mappedValue);
if (sensorValue > 300) {
digitalWrite (ledPinRood, HIGH);
digitalWrite (ledPinGroen, LOW);
}
else {
digitalWrite (ledPinRood, LOW);
digitalWrite (ledPinGroen, HIGH);
}
}