Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Manvi Mittal
- * Photoresistor Project
- */
- const int pinPhoto = A0;
- const int pinLedRed = 9; // red
- const int pinLedGreen = 10; // green
- const int pinLedBlue = 11; // blue
- const int pinButt = 2;
- int minimum = 5000;
- int maximum = -5000;
- int lightLevel = 0;
- int brightness = 0;
- unsigned long timeVal;
- int newButt = 0;
- int lastButt = 0;
- void setup() {
- // put your setup code here, to run once:
- pinMode (pinPhoto, INPUT);
- pinMode (pinLedRed, OUTPUT);
- pinMode (pinLedGreen, OUTPUT);
- pinMode (pinLedBlue, OUTPUT);
- pinMode (pinButt, INPUT);
- Serial.begin (9600);
- analogWrite (pinLedRed, 100);
- delay (500);
- analogWrite (pinLedRed, 0);
- delay (500);
- analogWrite (pinLedGreen, 100);
- delay (500);
- analogWrite (pinLedGreen, 0);
- delay (500);
- analogWrite (pinLedBlue, 100);
- delay (500);
- analogWrite (pinLedBlue, 0);
- delay (500);
- }
- void calibrate()
- {
- maximum = -5000;
- minimum = 5000;
- analogWrite (pinLedBlue, HIGH);
- analogWrite (pinLedRed, HIGH);
- analogWrite (pinLedGreen, LOW);
- delay (300);
- analogWrite (pinLedBlue, LOW);
- analogWrite (pinLedRed, LOW);
- analogWrite (pinLedGreen, LOW);
- timeVal = millis();
- while (millis() - timeVal < 5000)
- {
- lightLevel = analogRead (pinPhoto);
- if (lightLevel > maximum)
- {
- maximum = lightLevel;
- }
- else if (lightLevel < minimum)
- {
- minimum = lightLevel;
- }
- }
- analogWrite (pinLedBlue, HIGH);
- analogWrite (pinLedRed, HIGH);
- analogWrite (pinLedGreen, LOW);
- delay (300);
- analogWrite (pinLedBlue, LOW);
- analogWrite (pinLedRed, LOW);
- analogWrite (pinLedGreen, LOW);
- Serial.println (minimum);
- Serial.println (maximum);
- }
- void loop() {
- newButt = digitalRead (pinButt);
- //Serial.print (newButt);
- if (newButt == HIGH && lastButt == LOW)
- {
- Serial.println ("Calibrating...");
- calibrate ();
- }
- lastButt = newButt;
- lightLevel = analogRead (pinPhoto);
- brightness = map (lightLevel, minimum, maximum, 255, 0);
- brightness = constrain (brightness, 0, 255);
- // Serial.println (brightness);
- if (lightLevel > minimum && lightLevel < (maximum - minimum) / 3)
- {
- analogWrite (pinLedRed, brightness/2);
- analogWrite (pinLedBlue, brightness);
- analogWrite (pinLedGreen, brightness/23);
- }
- else if (lightLevel > minimum && lightLevel < 2*(maximum - minimum) / 3)
- {
- analogWrite (pinLedRed, brightness);
- analogWrite (pinLedBlue, brightness);
- analogWrite (pinLedGreen, brightness/5);
- }
- else if (lightLevel > minimum && lightLevel < (maximum - minimum))
- {
- analogWrite (pinLedRed, brightness/5);
- analogWrite (pinLedBlue, brightness/5);
- analogWrite (pinLedGreen, brightness);
- }
- }
- boolean debounce(boolean last)
- {
- boolean current = digitalRead(pinButt);
- if (last != current)
- {
- delay(5);
- current = digitalRead(pinButt);
- }
- return current;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement