You are on page 1of 6

Lab Assessment

1.a
const int red=10;
const int blue=11;
const int green=9;
const int trig=6;
const int echo=7;
long duration,cm;
const int buzzer=2;
void setup(){
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);
pinMode(buzzer,OUTPUT);
Serial.print(9600);
}
void loop(){
pinMode(trig,OUTPUT);
digitalWrite(trig,LOW);
delay(10);
digitalWrite(trig,HIGH);
delay(10);
digitalWrite(trig,LOW);

pinMode(echo,INPUT);
duration=pulseIn(echo,HIGH);
cm=(duration/2)/29.1;
if(cm<50){
setColor(255,0,0);
tone(buzzer,1000);
}else if(cm<=250 ){
setColor(255,255,0);
tone(buzzer,500);
}else{
setColor(0,0,255);
noTone(buzzer);
}
}
void setColor(int r,int g,int b){
analogWrite(red,r);
analogWrite(green,g);
analogWrite(blue,b);
}
1.b Enhanced Arduino system that turns on two of three LEDs when the room is dark

Fig.3 One of the Three LEDs lighting up when the room is light

Fig.4 Two of the LEDs lighting up when the room is dark


const int sensorPin = 0;
const int ledPin1= 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
int lightLevel;
int calibratedlightLevel;
int maxThreshold = 0;
int minThreshold = 255;

void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT); // Set up the LED pin to be an output.
Serial.begin(9600);
}
void loop()
{
lightLevel = analogRead(sensorPin);
Serial.print(lightLevel);

calibratedlightLevel = map(lightLevel, 0, 1000, 0, 100);

Serial.print(calibratedlightLevel);

digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);

if(lightLevel == 0){

digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);

}else if(lightLevel < minThreshold &&


lightLevel > 0){

digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,LOW);

}else{

digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
}
}

You might also like