Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Atiriya Iyer
- * Photocell Tests
- * 6-15-17
- * Version 2
- */
- const int pinPhoto= A0;
- int lightLevel =0;
- const int pinLEDblue= 9;
- const int pinLEDgreen= 10;
- const int pinLEDred= 11;
- const int pinButton=2;
- int brightness= 0;
- int lastButt= 0;
- int newButt= 0;
- unsigned long timeStart= 0; //long can hold a much larger number and unsigned is only positive
- int minimum= 5000;
- int maximum= -5000;
- void setup()
- {
- // put your setup code here, to run once:
- pinMode (pinPhoto, INPUT);
- pinMode (pinLEDblue, OUTPUT);
- pinMode (pinLEDgreen, OUTPUT);
- pinMode (pinLEDred, OUTPUT);
- pinMode (pinButton, INPUT);
- Serial.begin (9600);
- analogWrite (pinLEDblue, 255);
- delay(500);
- analogWrite (pinLEDblue, 0);
- delay(500);
- analogWrite (pinLEDgreen, 255);
- delay(500);
- analogWrite (pinLEDgreen, 0);
- delay(500);
- analogWrite (pinLEDred, 255);
- delay(500);
- analogWrite (pinLEDred, 0);
- }
- void loop()
- {
- // put your main code here, to run repeatedly:
- lightLevel= analogRead (pinPhoto);
- brightness= map(lightLevel, minimum, maximum, 255, 0);
- //established proportions, uses values we know to values we need
- brightness= constrain(brightness, 0,255);
- analogWrite (pinLEDblue, brightness);
- analogWrite (pinLEDred, brightness);
- //if (brightness > minimum && brightness < (maximum-minimum)/5)
- // {
- //analogWrite (pinLEDgreen, 50/255*brightness);
- //analogWrite (pinLEDred, 50/255*brightness);
- //analogWrite (pinLEDblue, 255/255*brightness);
- //}
- // analogWrite (pinLEDblue, brightness/2);
- // analogWrite (pinLEDred, brightness/2);
- // analogWrite (pinLEDgreen, brightness/2);
- newButt= debounce(lastButt);
- if (newButt== HIGH && lastButt== LOW)
- {
- Serial.println("Calibrating...");
- calibrate();
- }
- lastButt= newButt;
- Serial.print ("Light level= ");
- Serial.println (lightLevel);
- Serial.print ("brightness= ");
- Serial.println (brightness);
- }
- boolean debounce (boolean last)
- {
- boolean current = digitalRead(pinButton);
- if (last != current)
- {
- delay(5);
- current= digitalRead(pinButton);
- }
- return current;
- }
- void calibrate()
- {
- analogWrite (pinLEDred, 255);
- analogWrite (pinLEDblue, 0);
- analogWrite (pinLEDgreen, 0);
- delay (200);
- analogWrite (pinLEDred, 0);
- analogWrite (pinLEDblue, 0);
- analogWrite (pinLEDgreen, 0);
- timeStart= millis();
- while (millis()-timeStart < 5000)
- {
- lightLevel= analogRead (pinPhoto);
- if (lightLevel > maximum)
- {
- maximum= lightLevel;
- }
- else if (lightLevel < minimum)
- {
- minimum= lightLevel;
- }
- }
- Serial.println (maximum);
- Serial.println (minimum);
- analogWrite (pinLEDred, 160);
- analogWrite (pinLEDblue, 0);
- analogWrite (pinLEDgreen, 100);
- delay (200);
- analogWrite (pinLEDred, 0);
- analogWrite (pinLEDblue, 0);
- analogWrite (pinLEDgreen, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement