kóðar - Hirtur/skilaverkefni5 GitHub Wiki

5.1 int buttonState = 0; void setup() { pinMode(2, INPUT); pinMode(13, OUTPUT); }

void loop() { // read the state of the pushbutton buttonState = digitalRead(2); // check if pushbutton is pressed. if it is, the // button state is HIGH if (buttonState == LOW) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); } delay(10); // Delay a little bit to improve simulation performance }

5.2 int sensorValue = 0; void setup() { pinMode(A0, INPUT); pinMode(13, OUTPUT); } void loop() { // read the value from the sensor sensorValue = analogRead(A0); // turn the LED on digitalWrite(13, HIGH); // pause the program for millseconds delay(sensorValue); // Wait for sensorValue millisecond(s) // turn the LED off digitalWrite(13, LOW); // pause the program for millseconds delay(sensorValue); // Wait for sensorValue millisecond(s) }

5.5 int distanceThreshold = 0; int cm = 0; int inches = 0; long readUltrasonicDistance(int triggerPin, int echoPin) { pinMode(triggerPin, OUTPUT); // Clear the trigger digitalWrite(triggerPin, LOW); delayMicroseconds(2); // Sets the trigger pin to HIGH state for 10 microseconds digitalWrite(triggerPin, HIGH); delayMicroseconds(10); digitalWrite(triggerPin, LOW); pinMode(echoPin, INPUT); // Reads the echo pin, and returns the sound wave travel time in microseconds return pulseIn(echoPin, HIGH); }

void setup() { Serial.begin(9600);

pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); }

void loop() { // set threshold distance to activate LEDs distanceThreshold = 350; // measure the ping time in cm cm = 0.01723 * readUltrasonicDistance(7, 6); // convert to inches by dividing by 2.54 inches = (cm / 2.54); Serial.print(cm); Serial.print("cm, "); Serial.print(inches); Serial.println("in");

if (cm > distanceThreshold) { digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); } if (cm <= distanceThreshold && cm > distanceThreshold - 100) { digitalWrite(2, HIGH); digitalWrite(3, LOW); digitalWrite(4, LOW); } if (cm <= distanceThreshold - 100 && cm > distanceThreshold - 250) { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, LOW); } if (cm <= distanceThreshold - 250 && cm > distanceThreshold - 350) { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); } if (cm <= distanceThreshold - 350) { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); } delay(100); // Wait for 100 millisecond(s) }