Sunday, November 5, 2017

Lab 6 - Voltage Dividers, CdS, and PIR Sensors

CLASSWORK:
Simple PIR Sensor
const int LED=9;//The LED is connected to pin 9
const int PIR=2;//The PIR is connected to pin 2
void setup()
{
 pinMode (LED, OUTPUT);
 pinMode (PIR, INPUT);
 Serial.begin(9600);
}
void loop()
{
 if (digitalRead(PIR) == LOW)
 {
 Serial.println("OFF");
 delay(500);
 digitalWrite(LED, LOW);
 }
 if (digitalRead(PIR) == HIGH)
 {
 Serial.println("ON");
 delay(500);
 }

}

Automatic Night Light with RGB LED and PIR Sensor
//Automatic Nightlight 

const int RLED=9; //Red LED on pin 9 (PWM) 
const int LIGHT=0; //Lght Sensor on analog pin 0 
const int MIN_LIGHT=200; //Minimum expected light value 
const int MAX_LIGHT=900; //Maximum Expected Light value 
int val = 0; //variable to hold the analog reading 

void setup() { 
 pinMode(RLED, OUTPUT); //Set LED pin as output 


void loop() { 
 val = analogRead(LIGHT); //Read the light sensor 
 val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0); //Map the light reading 
 val = constrain(val, 0, 255); //Constrain light value 
 analogWrite(RLED, val); //Control the LED 
}

HOMEWORK:
Photoresistor Controls LED & Speaker
int SPEAKER_1=9;
int SPEAKER_2=8;
int SPEAKER_3=7;
int SPEAKER_4=6;
const int RLED=10;//Red LED on pin 10 (PWM)
const int GLED=11;
const int LASER_1=12;
const int LASER_2=13;
const int LIGHT=A0;//Light Sensor on analog pin 0
const int LIGHT_2=A1;
const int MIN_LIGHT=150; //Minimum expected light value
const int MAX_LIGHT=600;//Maximum Expected Light value
const int MIN_LIGHT_2=150;
const int MAX_LIGHT_2=600;
const int MIN_LIGHT_3=150;
const int MAX_LIGHT_3=600;
const int MIN_LIGHT_4=150;
const int MAX_LIGHT_4=600;
int val = 0; //variable to hold the analog reading
int val_2 = 0;
int val_3 = 0;
int val_4 = 0;
void setup()
{
 pinMode(RLED, OUTPUT); //Set LED pin as output
 pinMode(GLED, OUTPUT);
 pinMode(LIGHT, INPUT);//Set Photoresistor as input
 pinMode(LIGHT_2,INPUT);
 Serial.begin(9600);
}
void loop()
{
 val = analogRead(LIGHT); //Read the light sensor
 val = map(val, MIN_LIGHT, MAX_LIGHT, 255,0 );//Map the light reading
 val = constrain(val, 0, 255); //Constrain light value
 analogWrite(RLED, val); //Control the LED
 Serial.println(val);
 delay(500);
    if(val > 150)
    {
    tone(SPEAKER_1, 1500,500);
    //digitalWrite(RGB_LED. HIGH)
    delay(500);

    }
//////////////////////////////////////////////////////////////////////////////////////////
{
 val_2 = analogRead(LIGHT_2); //Read the light sensor
 val_2 = map(val_2, MIN_LIGHT_2, MAX_LIGHT_2, 255,0 );//Map the light reading
 val_2 = constrain(val_2, 0, 255); //Constrain light value
 analogWrite(GLED, val_2); //Control the LED
 //digitalWrite(GLED, HIGH);
 Serial.println(val_2);
 delay(500);
    if(val_2 > 140)
     {
      tone(SPEAKER_2, 1500,500);
    delay(500);
     }
  }
//////////////////////////////////////////////////////////////////////////////////////////
 {
 val_3 = analogRead(LASER_1); //Read the light sensor
 val_3 = map(val_3, MIN_LIGHT_3, MAX_LIGHT_3, 255,0 );//Map the light reading
 val_3 = constrain(val_3, 0, 255); //Constrain light value
 analogWrite(LASER_1, val_3); //Control the LED
 //digitalWrite(GLED, HIGH);
 Serial.println(val_3);
 delay(500);
    if(val_3 > 140)
     {
      tone(SPEAKER_3, 1500,500);
    delay(500);
     }
  }
//////////////////////////////////////////////////////////////////////////////////////////
   {
 val_4 = analogRead(LASER_2); //Read the light sensor
 val_4 = map(val_3, MIN_LIGHT_4, MAX_LIGHT_4, 255,0 );//Map the light reading
 val_4 = constrain(val_4, 0, 255); //Constrain light value
 analogWrite(LASER_2, val_4); //Control the LED
 //digitalWrite(GLED, HIGH);
 Serial.println(val_4);
 delay(500);
    if(val_4 > 140)
     {
      tone(SPEAKER_4, 1500,500);
    delay(500);
     }
  }

}

No comments:

Post a Comment