Grumpy_Mike:
The proper name is a uniselector, and back in the 60's I used them for switching lights on an off in a pattern for discos. In fact I once got a job where they wanted me to manually switch lights on and off in a disco. I simply showed up, wired my uniselector into the lighting desk and let the thing run all night.
And the method that the writer of the code choose to do it was crap. That is all I am saying. The main reason is that it fails to consider the time the rest of the code takes to run.
To do it properly you need to implement it like this:-
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
Demonstration code for several things at the same time - Project Guidance - Arduino Forum
Mike,
I followed your advise and changed how the code is written.
Still having an issue trying to get time between when pin A goes high and when pin B goes high.
(time B- time A).
I believe its in the logic (or lack of);
Does anything obvious jump out to you ?
// -------------------------------------------LIBRARIES-------------------------------------------------------------------------------
#include <TimerOne.h>
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 10, d7 = 9;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// -----------------------------------------CONSTANTS (won't change)------------------------------------------------------------------
const int pwmPin = 22; // the pin numbers for the LEDs
const int PulseLowTime = 970; // number of millisecs that the PWM signal is LOW
const int PulseHighTime = 30; // number of millisecs that the PWM signal is HIGH
const int Signal_A_Pin = 2;
const int Signal_B_Pin = 3;
//byte ReadA= digitalRead(Signal_A_Pin);
//byte ReadB= digitalRead(Signal_B_Pin);
// --------------------------------------VARIABLES (will changes)---------------------------------------------------------------------
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousPWM_Millis = 0; // will store last time the PWM signal was updated
byte pwmState = LOW; // LOW = initally LOW
byte Signal_A_State = LOW; // LOW = initally LOW
byte Signal_B_State = LOW; // LOW = initally LOW
unsigned long Signal_A_Time =0;
unsigned long Signal_B_Time =0;
unsigned long Delta_T =0;
// ------------------------------------------VOID SETUP--------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println("The program is executing tasks"); // so we know what sketch is running
// set the digital pins as an input or output:
pinMode(pwmPin, OUTPUT);
pinMode(Signal_A_Pin, INPUT_PULLUP);
pinMode(Signal_B_Pin, INPUT_PULLUP);
}
// ------------------------------------------VOID LOOP--------------------------------------------------------------------------------
void loop() {
// none of the actions happen in loop() apart from reading millis()
// it just calls the functions that have the action code
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized
update_PWM_State();
update_Signal_A_State();
update_Signal_B_State();
update_time();
}
// ------------------------------------------------FUNCTIONS--------------------------------------------------------------------------
// ----------update_PWM_State FUNCTION----------------
void update_PWM_State() {
if (pwmState == LOW) {
// if the PWM is LOW, we must wait for the interval to expire before turning it HIGH
if (currentMillis - previousPWM_Millis >= PulseLowTime) {
// time is up, so change the state to HIGH
pwmState = HIGH;
digitalWrite (pwmPin, HIGH);
// and save the time when we made the change
previousPWM_Millis += PulseLowTime;
// NOTE: The previous line could alternatively be
//previousPWM_Millis = currentMillis
//Which is the style used in the BlinkWithoutDelay example sketch
//Adding on the interval is a better way to ensure that succesive periods are identical
//Serial.println (pwmState);
}
}
else { // i.e. if pwmState is HIGH
// if the PWM is HIGH, we must wait for the duration to expire before turning it LOW
if (currentMillis - previousPWM_Millis >= PulseHighTime) {
// time is up, so change the state to LOW
pwmState = LOW;
digitalWrite (pwmPin, LOW);
// and save the time when we made the change
previousPWM_Millis += PulseHighTime;
//Serial.println (pwmState);
}
}
}
// ----------update_Signal_A_State FUNCTION----------------
void update_Signal_A_State() {
if (digitalRead(Signal_A_Pin) != Signal_A_State) {
// if current reading (ReadA) does not equal old reading (Signal_A_State)
if (digitalRead(Signal_A_Pin) ==HIGH) {
// if current reading is greater than zero then Signal has went from low to high
Signal_A_Time = millis();
// Serial.println ("A high");
}
}
else { // i.e. if ReadA is LOW
if (digitalRead(Signal_A_Pin)==LOW){
Signal_A_State = LOW;
// Serial.println ("A low");
}
}
}
// ----------update_Signal_B_State FUNCTION----------------
void update_Signal_B_State() {
if (digitalRead(Signal_B_Pin) != Signal_B_State) {
// if current reading does not equal old reading (Signal_A_State)
if (digitalRead(Signal_B_Pin) ==HIGH) {
// if current reading is greater than zero then Signal has went from low to high
Signal_B_Time = millis();
//Serial.println ("B high");
}
}
else { // i.e. if ReadA is LOW
if (digitalRead(Signal_B_Pin)==LOW){
Signal_B_State = LOW;
//Serial.println ("B low");
}
}
}
// ----------update_time FUNCTION----------------
void update_time() {
Delta_T=((Signal_B_Time) - (Signal_A_Time));
Serial.println (Delta_T);
Delta_T=0;
Signal_A_Time=0;
Signal_B_Time=0;
}