Hi Guys,
Here's a question that I think I know the answer to but would like to confirm if there is another solution
I have a test program running that is multiplexing a couple of LEDs and I need to be able to measure the actual time for one loop to occur because I need to insert a another specific time based action into the loop without affecting the overall speed, at least too drastically. To say it another way, I want to find how much free time I have available to do something.
I could use serial monitor to print out millis() or micros() but as we all know, that will slow the program loop down dramatically so the results would be meaningless.
As far as I know, the only other way I can do it is to measure the pulsed output to one of my LEDs using an oscilloscope.
Am I correct, or is there another way? For example, could I read the values internally and store them in EEPROM memory, then retrieve them after the test is done? Would that be possible? Would it also cause too much error?
Here is a piece of reference code - it is not the final code that will go into my project, just a simulator for testing some principles. If anyone has an oscilloscope and wouldn't mind measuring this through a standard Uno I would be very grateful.
// Arduino Uno Mutliplex test with attempt to crossfade
// Cannot use PWM or analog output from Arduino
// Cannot use delay()
const int Anode1 = 2; // Anode for LED pair 1
const int Anode2 = 3; // Anode for LED pair 2
const int Cathode1a = 4; // Fpr LED1a
const int Cathode1b = 5; // For LED1b
const int Cathode2a = 6; // For LED2a
const int Cathode2b = 7; // For LED2b
const int Butt1 = 8; // Button for LED pair 1
const int Butt2 = 9; // Button for LED pair 2
const uint16_t tube_delay = 5000; // multiplexing delay in microseconds: adjust as required
long tube_start = 0; // multiplex start time used in calculation
int display_status = 1; // which anode to display now
int state1a = HIGH, state1b = LOW, state2a = HIGH, state2b = LOW; // Initial LED conditions
int reading1, reading2; // For button presses
int previous1 = LOW, previous2 = LOW; // Default state of each button
// Button Variables
long prevtime1 = 0, prevtime2 = 0; // debounce start time used in calculation
long debounce = 200; // debounce value: adjust as required
void setup() {
pinMode (Anode1, OUTPUT);
pinMode (Anode2, OUTPUT);
pinMode (Cathode1a, OUTPUT);
pinMode (Cathode1b, OUTPUT);
pinMode (Cathode2a, OUTPUT);
pinMode (Cathode2b, OUTPUT);
pinMode (Butt1, INPUT);
pinMode (Butt2, INPUT);
} // Setup End
void loop() {
// For button 1
reading1 = digitalRead(Butt1); // Butt1 state will be LOW unless pressed
// Check for button press on button 1
if (reading1 == HIGH && previous1 == LOW && millis() - prevtime1 > debounce) {
if (state1a == HIGH){ // If Butt1 has been pressed
state1a = LOW; // LED1a off
state1b = HIGH; // LED1b on
} else { // else
state1a = HIGH; // LED1a on
state1b = LOW; // LED1b off
}
prevtime1 = millis(); // Reset the debounce timer
}
previous1 = reading1; // Resets Butt1 state to LOW when it is released
// For button 2
reading2 = digitalRead(Butt2); // Butt2 state will be LOW unless pressed
// Check for button press on button 2
if (reading2 == HIGH && previous2 == LOW && millis() - prevtime2 > debounce) {
if (state2a == HIGH){ // If Butt2 has been pressed
state2a = LOW; // LED2a off
state2b = HIGH; // LED2b on
} else { // else
state2a = HIGH; // LED2a on
state2b = LOW; // LED2b off
}
prevtime2 = millis(); // Reset the debounce timer
}
previous2 = reading2; // Resets Butt2 state to LOW when it is released
DisplayOutput(); // Calls display routine
} // Loop End
void DisplayOutput(){ // Actually gives outputs to pins
if (micros()-tube_start >= tube_delay) { // If time has passed for next display update to occur
switch (display_status) { // Increment through each case
case 1: // Clears anode 2, sets anode 1
digitalWrite(Anode2, LOW); // Turn off previous anode
CathodeOutput1(); // Get cathode state for LED 1
digitalWrite(Anode1, HIGH); // Turn on this anode
tube_start += tube_delay; // increment the delay value and
display_status++; // increment to next anode
break; // Ignore below cases, we are done here
case 2: // Clears anode 1, set anode 2
digitalWrite(Anode1, LOW); // Turn off previous anode
CathodeOutput2(); // Get cathode state for LED 2
digitalWrite(Anode2, HIGH); // Turn on this anode
tube_start += tube_delay; // increment the delay value
display_status = 1; // All done, return to first case
break; // Ignore below cases, we are done here
}
}
} //DisplayOutput End
void CathodeOutput1(){ // Set required cathode1 selection on or off
digitalWrite(Cathode1a, state1a);
digitalWrite(Cathode1b, state1b);
}
void CathodeOutput2(){ // Set required cathode2 selection on or off
digitalWrite(Cathode2a, state2a);
digitalWrite(Cathode2b, state2b);
}