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()
- {
- maximum = -5000;
- minimum = 5000;
- analogWrite(PinGreen, 0);
- analogWrite(PinRed, 0);
- analogWrite(PinBlue, 0);
- delay(200);
- analogWrite(PinRed, 255);
- delay(200);
- analogWrite(PinRed, 0);
- 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);
- delay(200);
- analogWrite(PinRed, 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);
- if (photoValue>minimum && photoValue<(maximum-minimum)/2.5)
- {
- analogWrite(PinBlue, lightVal);
- analogWrite(PinRed, lightVal);
- analogWrite(PinGreen, 0);
- }
- {
- if (photoValue>(maximum-minimum)/2.5 && photoValue<2*(maximum-minimum)/2.5 )
- analogWrite(PinBlue, lightVal);
- analogWrite(PinRed, 0);
- analogWrite(PinGreen, lightVal);
- }
- {
- if (photoValue>2*(maximum-minimum)/2.5&&photoValue<maximum-minimum)
- analogWrite(PinBlue, 0);
- analogWrite(PinRed, lightVal);
- analogWrite(PinGreen, 0);
- }
- // analogWrite(PinBlue, lightVal);
- // analogWrite(PinRed, lightVal);
- // Serial.print ("Photoresistor value = ");
- // Serial.print(photoValue);
- // Serial.print ("lightVal = ");
- // Serial.println(lightVal);
- // analogWrite(PinRed, lightVal);
- // analogWrite(PinBlue, lightVal);
- // analogWrite(PinGreen,0);
- // delay(100);
- //
- //
- //Read (photoPin);
- // lightVal= map(photoValue, 0, 1020, 255, 0);
- // Serial.print ("Photoresistor value = ");
- // Serial.print(photoValue);
- // Serial.print ("lightVal = ");
- // Serial.println(lightVal);
- // delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement