[password: physcomp]
I made an instrument with a SPST switch and a Photoresistor. I had a hard time debugging during this time, and I finally realized that Serial printing was causing the time to not flow precisely.
The code was a simple modification of the Event Loop program, but I really dissected it and have grown to understand it.
const int outputPin1 = 4;
const int photoval = A0;
int val = 0;
const int switchVal = 9;
/****************************************************************/
// This function is called once after reset to initialize the program.
void setup()
{
Serial.begin(9600); // for debugging
pinMode( outputPin1, OUTPUT );
pinMode( photoval, INPUT);
}
long next_output_time_1 = 0; // timestamp in microseconds for when next to update output 1
long output_interval_1 = 500; // interval in microseconds between output 1 updates
int output_state_1 = LOW; // current state of output 1
/****************************************************************/
// This function is called repeatedly as fast as possible from within the
// built-in library to poll program events.
void loop()
{
val = analogRead(photoval);
int newVal = map(val,300,800,1000,1500);
bool hey = digitalRead(switchVal);
// read the current time in microseconds
long now = micros();
// Polled task 1 for output 1. Check if the next_output_time_1 timestamp has
// been reached; if so then update the output 1 state.
if (now > next_output_time_1) {
// reset the timer for the next polling point
// next_output_time_1 = now + output_interval_1;
next_output_time_1 = now + newVal;
// toggle the output_state_1 variable
output_state_1 = !output_state_1;
// update output pin 1 with the new value
if (hey == HIGH) {
digitalWrite( outputPin1, output_state_1 );
}
}
}
/****************************************************************/
Leave a Reply
You must be logged in to post a comment.