Guider‎ > ‎Hardware‎ > ‎Arduino‎ > ‎

Photoresistor & RGB LEDs


Board: Teensy++ - http://www.pjrc.com/teensy/index.html
Kode:
// Pinmappings  
int greenPin1 =  14; //RGB LED 1
int redPin1 =  16; //RGB LED 1
int bluePin1 =  15; //RGB LED 1
int redPin2 =  24; //RGB LED 2
int greenPin2 =  26; //RGB LED 2
int bluePin2 =  25; //RGB LED 2

// Settings
int Photoresistor = 0; // Photoresistor / Lightsensor set to 0
int ledDelay = 50; // Xms delay

// The setup() method runs once, when the sketch starts defining serial baudrate and pin type.
void setup()   {
  // setup serial communication rate
  Serial.begin(38400);
  // initialize the pins as an outputs
  pinMode(redPin1, OUTPUT);
  pinMode(greenPin1, OUTPUT);
  pinMode(bluePin1, OUTPUT);
  pinMode(redPin2, OUTPUT);
  pinMode(greenPin2, OUTPUT);
  pinMode(bluePin2, OUTPUT);
}

// The loop starts running the program
void loop()  {
  // Read photoresistor
  Photoresistor = analogRead(3);
  
  if ( Photoresistor < 830 ) {
  Serial.print("Too dark - LED ON - value is: ");
  Serial.println(Photoresistor);
  // Turn the LEDs on
  analogWrite(redPin1, 10);
  analogWrite(bluePin1, 10);
  analogWrite(greenPin1, 10);
  analogWrite(greenPin2, 10);
  analogWrite(bluePin2, 10);
  analogWrite(redPin2, 10);
  delay(1000); }
  if ( Photoresistor >= 830 && Photoresistor <= 960 ) {
  Serial.print("The light is on - LED OFF - value is: ");
  Serial.println(Photoresistor);
  // Turn all LEDs off
  digitalWrite(greenPin1, LOW);
  digitalWrite(bluePin1, LOW);
  digitalWrite(redPin1, LOW);
  digitalWrite(greenPin2, LOW);
  digitalWrite(bluePin2, LOW);
  digitalWrite(redPin2, LOW);
  delay(1000); }
  if ( Photoresistor > 960 ) {
  Serial.print("Agh too much light i am blind! Is the police here? - value is: ");
  Serial.println(Photoresistor);
  // Make LED blinking police lights
  digitalWrite(redPin1, HIGH); // turn the red light on LED 1
  digitalWrite(bluePin2, HIGH); // turn the blue light on LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(redPin1, LOW); // turn the red light off LED 1
  digitalWrite(bluePin2, LOW); // turn the blue light off LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(redPin1, HIGH); // turn the red light on LED 1
  digitalWrite(bluePin2, HIGH); // turn the blue light on LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(redPin1, LOW); // turn the red light off LED 1
  digitalWrite(bluePin2, LOW); // turn the blue light off LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(redPin1, HIGH); // turn the red light on LED 1
  digitalWrite(bluePin2, HIGH); // turn the blue light on LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(redPin1, LOW); // turn the red light off LED 1
  digitalWrite(bluePin2, LOW); // turn the blue light off LED 2
  delay(ledDelay); // wait 50 ms
  delay(100); // delay midpoint by 100ms
  digitalWrite(bluePin1, HIGH); // turn the blue light on LED 1
  digitalWrite(redPin2, HIGH); // turn the red light on LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(bluePin1, LOW); // turn the blue light off LED 1
  digitalWrite(redPin2, LOW); // turn the red light off LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(bluePin1, HIGH); // turn the blue light on LED 1
  digitalWrite(redPin2, HIGH); // turn the red light on LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(bluePin1, LOW); // turn the blue light off LED 1
  digitalWrite(redPin2, LOW); // turn the red light off LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(bluePin1, HIGH); // turn the blue light on LED 1
  digitalWrite(redPin2, HIGH); // turn the red light on LED 2
  delay(ledDelay); // wait 50 ms
  digitalWrite(bluePin1, LOW); // turn the blue light off LED 1
  digitalWrite(redPin2, LOW); // turn the red light off LED 2
  delay(ledDelay); // wait defined ms 
  }
 }