30 September 2019

Mapping: It's Finicky

Getting the RGB LED to respond like I wanted was more difficult than I thought it would be. My original idea was to have this project be a interactive example on how our country ignore the science of climate change using the light sensor and water sensor. As I put everything together it all worked, but it didn't work the way I was hoping so I had to table the idea for another time. I believe my main issue was with the light sensor, but I didn't have the time to redo what I have put together.

The result of all of this is an LED light that can be manipulated in three different ways: light sensor, potentiometer, and water sensor. The potentiometer can also be used to activate the active buzzer after crossing a certain threshold. 

Materials:
  • 17 Jumper Wires
  • 1 Arduino UNO
  • 1 RGB LED
  • 3 220ohm resistors
  • 1 photoresistor
  • 1 Water Sensor
  • 1 Potentiometer
  • 1 active buzzer



Active Buzzer Schematic

LED Schematic

Potentiometer Schematic

Water Sensor Schematic


Original Sketch



const int bluePin = 3;
const int greenPin = 5;
const int redPin = 6;
int index = 0;
int multiplier = 5;
//waterlevel
int waterLevel = 5;
int HistoryValue = 0;
char printBuffer[128];
//buzzer
int buzzer = 12;//the pin of the active buzzer
void setup() {
// put your setup code here, to run once:
pinMode(bluePin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
Serial.begin(9600);
}
void loop() {
//potentiometer
int potVal = analogRead(A0);
delay(1);
//light detector
int photoVal = analogRead(A2);
//mapping
//mapping potentiometer
int mappedVal = map(potVal, 0, 1023, 255, 0);
analogWrite(greenPin, mappedVal);
if(mappedVal >= 55){
digitalWrite(buzzer, HIGH);
}
else {
digitalWrite(buzzer, LOW);
}
//mapping photoresistor
int mappedVal2 = map(photoVal, 0, 1023, 0, 255);
analogWrite(bluePin, mappedVal2);
//map water level
int mappedVal5 = map(waterLevel, 0, 1023, 0, 255);
Serial.print(potVal);
Serial.print("\t");
Serial.println(mappedVal);
//water level
int value = analogRead(waterLevel); // get adc value
analogWrite(redPin, value);
//manipulation
//water level print
if(((HistoryValue>=value) && ((HistoryValue - value) > 10)) || ((HistoryValue<value) && ((value - HistoryValue) > 10)))
{
sprintf(printBuffer,"ADC%d level is %d\n",waterLevel, value);
Serial.print(printBuffer);
HistoryValue = value;
}
// put your main code here, to run repeatedly:
//on and off really quickly - pulse width modulation
/*analogWrite(redPin, index);
delay (10);
if(index > 230) {multiplier = multiplier * -1;}
if(index < 0) { multiplier = multiplier * -1;}
index = index + multiplier;
for(int x = 0; x > 230; x++){
analogWrite(redPin, x);
delay(10);
}
for(int x = 255; x > 0; x--){
analogWrite(redPin, x);
delay(10);
}*/
//Serial.println ("Blast!");
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.