Tuesday 8 December 2015

Arduino Challenge 6: Light Sensor (Make Your Own Challenge)

Challenge Name: Light Sensor & RGB LED

Author: Christina He

Credit: New Innovators: Lab 6

Difficulty Level: Level 5

Time to Complete: Approximately 35 minute

Challenge Description: Make the RGB LED change colours using the light sensor. When the light sensor is covered, the RGB will switch to red (pink) and green from blue. When the light sensor is released, the RGB will switch back and stay at blue.

Answer:

Code

int lightPin = 0;
int redrgb = 11;
int greenrgb = 10;
int bluergb = 9;

void setup()
{
  pinMode (redrgb, OUTPUT);
  pinMode(greenrgb, OUTPUT);
  pinMode(bluergb, OUTPUT);
}
void loop()
{
  int lightLevel = analogRead(lightPin);
  lightLevel = map(lightLevel, 0, 900, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);
  analogWrite(redrgb, lightLevel);
  delay(100);
  analogWrite(greenrgb,lightLevel);
  delay(100);
  analogWrite(bluergb, lightLevel);
  delay(100);
}

Visual Answer



(colours not showing up properly, but the order is: blue, red(pink), green, and back to blue)

No comments:

Post a Comment