So we discussed strategies to imitate PWM effect by changing on-time/off-time of the led on arduino. Keeping the on-time as 20 milliseconds and gradually decreasing it produces the smooth transition from high intensity to low intensity. (The total time on+off remains constant)
Thereafter, I placed the smartphone sensor to capture the light intensity value during this transition. Ofcourse, its not a controlled experiments, but this initial tinkering led to some observations which are exciting! This provides a context to discuss many concepts including data handling, persistence of vision etc… Here is the graph of the same,
Do share what do you think are some interesting questions to explore/experiments to design and your observations when you attempted to do something similar
Add a tact switch. By pressing the switch increase the brightness.
Then add one more tact switch. Pressing this one should reduce brightness.
Now instead of pin 13 only simultaneously turn on or pin 13 and 12.
On pin 12 connect a resistor of 1K. Connect the other end to +ve of 1uf capacitor and another resistor of 4k7. Connect other end of 1uf capacitor and resistor to gnd. Now measure the voltage on the +ve pin of capacitor. You should get a higher voltage everytime you press increment and a lower voltage everytime you press decrement. You now have a crude Digital to Analog converter.
The intensity measurement with phone sensor is excellent.
Here is some code for PWM with a button press controlled decrement.
Notice the use of delayMicroseconds() and change of the count from 20 to 4096.
Everyone take shot at explaining why this is being done.
"
int SW = 3;
int SWState;
int PwmVal=4096;
int PwmValoff;
One can use a multimeter to get an average voltage reading. The voltage changes smoothly from 0 to 4.8 volts. So does the LED intensity.
This is now a single bit Digital to Analog Converter.
"int SWdec = 3; //input for reducing brightness
int SWinc = 2; //input for reducing brightness
int SWState; //state of pin 3
int PwmIncDec; // set +1 if SWinc is pressed or set +1 if Swdec is pressed or 0 if both are high or low
int PwmVal=4096; //max value of on+offtime in u sec
int PwmValon; //on time of Pwm
int PwmValoff; // off time Pwm
void setup()
{
pinMode(13, OUTPUT); //set pin 13 to output - using onboard LED
pinMode(SWdec, INPUT); //set pin 3 to input for key press
pinMode(SWinc, INPUT); //set pin 2 to input for key press
Serial.begin(9600);
}