Concept
For this assignment, I imagined to simulate a kind of animals using just LEDs. It took me some time to decide on which animal to imitate because the information communicated by LEDs can be very limited. Then, suddenly an idea came to my mind: a chameleon that is sensitive to light! To do this, I used a light sensor and a RGB LED as the analog input and output. The RGB LED will change color as the sensor detects different amount of light, just like chameleon changing color with its environment. Besides this, I used a button and a normal LED as the digital input and out. This simulates the situation where the chameleon is found out by its predator and got eaten. So, if the button is pressed, the red normal LED will be on and the RGB LED will become white, regardless of the light sensor.
(Due to the quality of the video, the color change might not be so obvious in the video. But it works really well in reality.)
Highlight of the code
The code is relatively easy. It essentially uses map() to take different ranges of RBG values and pass them to the RGB LED respectively.
#define RED_PIN 3 #define GREEN_PIN 5 #define BLUE_PIN 6 #define LED_PIN 12 void setup() { Serial.begin(9600); pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); pinMode(A0, INPUT); //light sensor input pinMode(2, INPUT); //button input pinMode(LED_PIN, OUTPUT); } int value = 0; int buttonState = 0; int lastButtonState = 0; int LEDState = LOW; void loop() { value = analogRead(A0); lastButtonState = buttonState; buttonState = digitalRead(2); // the LED is on when button is pressed, and off when button is pressed again if (lastButtonState == 1 && buttonState == 0) { LEDState = !LEDState; digitalWrite(LED_PIN, LEDState); } if (LEDState == HIGH) { // if the red LED is on, RGB LED is always white setColor(255, 255, 255); } else { // make the range of R, G, B different from each other int R = map(value, 100, 1000, 0, 255); int G = map(value, 100, 1000, 255, 0); int B = map(value, 0, 1000, 0, 100); setColor(R, G, B); Serial.println(R); // for debug } } void setColor(int R, int G, int B) { analogWrite(RED_PIN, R); analogWrite(GREEN_PIN, G); analogWrite(BLUE_PIN, B);
Reflection and Improvement
I think the challenge of this assignment for me is to convey meaningful messages through LEDs and their own inputs, which are relatively limited. However, the use of RGB LED gives me more flexibility and makes the project a little more interesting. If I were to improve it, I would make an actual chameleon using some materials like cardboards. This might make the concept of the assignment more visually concrete.