Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * James Brown
- * Version 1
- *
- *
- */
- const int pinPhoto = A0;
- const int pinred= 9;
- const int pingreen = 10;
- const int pinblue = 11;
- int lightLevel = 0;
- int bright = 0;
- const int pinButt = 2;
- int lastButt = 0;
- int newButt = 0;
- unsigned long timeStart;
- int minimum = 5000;
- int maximum = -5000;
- void setup() {
- // put your setup code here, to run once:
- pinMode (pinPhoto, INPUT);
- pinMode (pinred, OUTPUT);
- pinMode (pingreen, OUTPUT);
- pinMode (pinblue, OUTPUT);
- pinMode (pinButt, INPUT);
- Serial.begin (9600);
- analogWrite (pinred, 255);
- delay (500);
- analogWrite (pinred, 0);
- analogWrite (pingreen, 255);
- delay (500);
- analogWrite (pingreen, 0);
- analogWrite (pinblue, 255);
- delay (500);
- analogWrite (pinblue, 0);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- //Serial.print (digitalRead(pinButt));
- lightLevel = analogRead(pinPhoto);
- bright = map (lightLevel, minimum, maximum, 255, 0);
- bright = constrain (bright, 0, 255);
- analogWrite (pinblue, bright);
- analogWrite (pinred, bright);
- analogWrite (pingreen, 0);
- //Serial.print (" Light Level = ");
- //Serial.print (lightLevel);
- //Serial.print ("bright = ");
- //Serial.println (bright);
- newButt = debounce (lastButt);
- //Serial.print (newButt);
- if (lastButt == LOW && newButt == HIGH)
- {
- Serial.println("Calibrating...");
- calibrate();
- }
- lastButt = newButt;
- lightLevel = analogRead(pinPhoto);
- bright = map(lightLevel, minimum, maximum, 255, 0-(maximum/10));
- bright = constrain(bright, 0, 255);
- if (bright > minimum && bright < (maximum-minimum /5))
- {
- analogWrite (pingreen, 50/255*bright);
- analogWrite (pinblue, 50/255*bright);
- analogWrite (pinred, 255/255*bright);
- }
- }
- boolean debounce(boolean last)
- {
- boolean current = digitalRead(pinButt);
- if (last != current)
- {
- delay(5);
- current = digitalRead(pinButt);
- }
- return current;
- }
- void calibrate ()
- {
- maximum = -5000;
- minimum = 5000;
- analogWrite (pinred, 255);
- analogWrite (pinblue, 0);
- analogWrite (pingreen, 0);
- delay (200);
- analogWrite (pinred, 0);
- analogWrite (pinblue, 0);
- analogWrite (pingreen, 0);
- timeStart = millis();
- while (millis() - timeStart <5000)
- {
- lightLevel = analogRead (pinPhoto);
- if (lightLevel > maximum)
- {
- maximum = lightLevel;
- }
- else if (lightLevel < minimum)
- {
- minimum = lightLevel;
- }
- }
- Serial.println (minimum);
- Serial.println (maximum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement