Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *Divya Inaganti
- *June 14, 2017
- *different RGB colors
- */
- const int photoPin = A0;
- const int PinRed= 9;
- const int PinGreen=10;
- const int PinBlue= 11;
- int photoValue= 0;
- int lightVal= 0;
- const int pinButton = 4;
- int newButton = 0;
- int lastButton = 0;
- unsigned long timeVal;
- int minimum= 1000;
- int maximum= -1000;
- void setup()
- {
- Serial.begin (9600);
- pinMode(photoPin, INPUT);
- pinMode (PinRed, OUTPUT);
- pinMode (PinGreen, OUTPUT);
- pinMode (PinBlue, OUTPUT);
- pinMode(pinButton, INPUT);
- analogWrite(PinRed, 255);
- analogWrite(PinGreen, 0);
- analogWrite(PinBlue,0);
- delay(200);
- analogWrite(PinRed, 0);
- analogWrite(PinGreen, 255);
- analogWrite(PinBlue,0);
- delay(200);
- analogWrite(PinRed, 0);
- analogWrite(PinGreen, 0);
- analogWrite(PinBlue,255);
- delay(200);
- analogWrite(PinRed, 0);
- analogWrite(PinGreen, 0);
- analogWrite(PinBlue,0);
- }
- void calibrate()
- {
- analogWrite(PinGreen, HIGH);
- timeVal = millis();
- while(millis()- timeVal < 5000)
- {
- photoValue = analogRead(photoPin);
- if(photoValue > maximum)
- {
- maximum = photoValue;
- }
- else if(photoValue < minimum)
- {
- minimum = photoValue;
- }
- }
- Serial.println(minimum);
- Serial.println(maximum);
- analogWrite(PinGreen, LOW);
- }
- boolean debounce(boolean last)
- {
- boolean current = digitalRead(pinButton);
- if (last != current)
- {
- delay(5);
- current = digitalRead(pinButton);
- }
- return current;
- }
- void loop()
- {
- newButton = debounce (lastButton);
- if (newButton == HIGH && lastButton == LOW)
- {
- calibrate();
- }
- lastButton = newButton;
- photoValue= analogRead (photoPin);
- lightVal= map(photoValue, minimum, maximum, 255, 0);
- lightVal= constrain(lightVal, 0, 255);
- analogWrite(PinBlue, lightVal);
- analogWrite(PinRed, lightVal);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement