Platform: WinXP / Arduino UNO
Sketch: (see below) An edited Example 07 from Getting Started With Arduino.
I'm trying to find a way to make the LED at Pin 9 present a wider scope of intensity depending on recent exposure feedback from the LDR. So I've been setting up variables that might be helpful for something like that.
For some reason, my brightness variable keeps coming up zero.
A manual calculation quickly shows we're not in the ballpark for what brightness should be. I double-checked with a spreadsheet of outgoing variables. (See highlighting for quick reference.)
It compiles, so it's probably not a syntax error. I'm guessing it has something to do with 'int'. Can someone explain to me what's going on with that value?
Example serial data, pinout and sketch follow for reference.
Cheers
Example Values - Serial Port Monitor:
val = 203 | ravgval = 181 | hval = 203 | intensity = 50 | brightness = 0 | thisTime = 5154
val = 207 | ravgval = 193 | hval = 208 | intensity = 51 | [glow]brightness = 0[/glow] | thisTime = 5366
val = 202 | ravgval = 194 | hval = 208 | intensity = 50 | [glow]brightness = 0[/glow] | thisTime = 5236
val = 204 | ravgval = 194 | hval = 208 | intensity = 51 | [glow]brightness = 0[/glow] | thisTime = 5296
val = 131 | ravgval = 175 | hval = 208 | intensity = 32 | brightness = 0 | thisTime = 5008
val = 129 | ravgval = 136 | hval = 188 | intensity = 32 | brightness = 0 | thisTime = 5158
val = 128 | ravgval = 127 | hval = 168 | intensity = 32 | brightness = 0 | thisTime = 5146
val = 127 | ravgval = 126 | hval = 168 | intensity = 31 | brightness = 0 | thisTime = 5104
val = 127 | ravgval = 126 | hval = 168 | intensity = 31 | brightness = 0 | thisTime = 5076
val = 126 | ravgval = 125 | hval = 168 | intensity = 31 | brightness = 0 | thisTime = 5052
val = 126 | ravgval = 125 | hval = 168 | intensity = 31 | brightness = 0 | thisTime = 5058
val = 131 | ravgval = 125 | hval = 168 | intensity = 32 | brightness = 0 | thisTime = 5096
Board Pinout:
Sketch:
// Example 06A: Blink LED at a rate specified by the value of the analogue input.
#define LED 13 // The pin for the LED
#define LED2 9 // The pin for the LED
int val = 0; // Variable used to store the value coming from the sensor.
int hval = 0; // Keep tabs of the highest value since the last delayTime.
int ravgval = 0; // Keep rolling average.
int intensity = 0; // Buffered val
[glow]int brightness = 0;[/glow] // Calculated brightness value
int delayTime = 5000; // Five seconds in milliseconds.
int thisTime = 0; // A counter to compare with delayTime.
void setup() {
pinMode(LED, OUTPUT); // LED is an OUTPUT.
pinMode(LED2, OUTPUT); // LED2 is an OUTPUT.
// Note: Analogue pins are automatically set as inputs.
}
void loop(){
val = analogRead(0); // Read the value from the sensor.
ravgval = ((14*ravgval)+val)/15; // Mock Rolling Average.
if(val > hval){
hval = val;
}
intensity = val/4; // analog value transformed to variable within writable parameters
[glow]brightness = (intensity/254)*hval;[/glow]
digitalWrite(LED, HIGH); // Turn LED on.
analogWrite(LED2, intensity); // Turn LED2 on.
delay(val); // Stop the program for some time.
thisTime = thisTime + val; // Add val to current delayTime.
digitalWrite(13, LOW); // Turn LED off.
delay(val); // Stop the program for some time.
thisTime = thisTime + val; // Add val to current delayTime.
if (thisTime > delayTime){
// Write to serial port every time thisTime is greater than delayTime.
Serial.begin(9600);
Serial.print("val = ");
Serial.print(val);
Serial.print(" | ravgval = ");
Serial.print(ravgval);
Serial.print(" | hval = ");
Serial.print(hval);
Serial.print(" | intensity = ");
Serial.print(intensity);
Serial.print(" | brightness = ");
Serial.print(brightness);
Serial.print(" | thisTime = ");
Serial.println(thisTime);
thisTime = 0; // Reset our counter.
if (hval > val + 50){
hval = hval - 20; // Reduce hval incrementally.
}
}
}