I've got a confession, I don't have an Arduino and have never programmed one but http://www.allaboutcircuits.com/projects/build-a-desktop-industrial-automation-trainer/ has caught my eye, as have many examples in here. This post uses an Arduino to sequence through a hard-coded output list, with each transition delayed by a second. Unless interrupts are involved, the controller is just stuck there during that second, doing nothing and responding to less. All this is a general problem relating to the difference between electronics and the real world. In real life, things move sloooowly!
What I've done here is replace the delay() block with a simple test. If the time isn't up yet, the processor can go and take can of other business. As part of a full solution, this can make the difference between a sulky, sluggish unit and a lively, responsive one. Imagine looking for a button press during the delayed part of the process.
While I was at it, I used an array of timers, allowing a different time for each step. What I haven't done is actually try it, I wonder if someone could give it a go and point out any glaring errors?
What I've done here is replace the delay() block with a simple test. If the time isn't up yet, the processor can go and take can of other business. As part of a full solution, this can make the difference between a sulky, sluggish unit and a lively, responsive one. Imagine looking for a button press during the delayed part of the process.
While I was at it, I used an array of timers, allowing a different time for each step. What I haven't done is actually try it, I wonder if someone could give it a go and point out any glaring errors?
Code:
/* 3 INPUT LOGIC GATE SIMULATOR
* APRIL 8, 2016
* BY DON WILCHER
* modified May 13th by Steve Root
*
* This code will provide digital inputs to the Velocio ACE PLC (Programmable
* Logic Controller) using the following Truth Table Inputs.
* The output of the Logic Gate will be visible by the integrated
* LED of the ACE PLC.
*
* 3 Input Logic Gate Simulator
* INPUTS
* A | B | C
* ___|____|____
* 0 | 0 | 0
* 0 | 0 | 1
* 0 | 1 | 0
* 0 | 1 | 1
* 1 | 0 | 0
* 1 | 0 | 1
* 1 | 1 | 0
* 1 | 1 | 1
*
*/
// Arduino Pin assignments
int A = 13;
int B = 12;
int C = 11;
// Timer declaration
bool SeqTimer(int, bool *);
// Timer definitions
#define NUMTIMERS 8
unsigned long TimeDelays[NUMTIMERS}={1000000, 500000, 200000, 100000, 100000, 200000, 500000, 1000000);
int tIndex = 0;
bool tReset = true;
bool SeqTimer(int tIndex, bool *tRst)
{
static unsigned long TargetList[NUMTIMERS];
if(*tRst== true)
{
TargetList[tIndex] = micros() + TimeDelays[tIndex];
*tRst= false;
}
else
{
if(micros() > TargetList[tIndex])
{
return true;
}
return false;
}
void setup()
{
// initialize digital pins 1, 2, and 3 as outputs
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
}
void loop()
{
if (SeqTimer(tIndex, &tReset) == true)
{
tIndex++; // Next timer in list
if(tIndex >= NUMTIMERS)
{
tIndex = 0;
}
// write the outputs
if(tIndex & 0x1) DigitalWrite(C, HIGH);
else DigitalWrite(C, LOW);
if(tIndex & 0x2) DigitalWrite(B, HIGH);
else DigitalWrite(B, LOW);
if(tIndex & 0x4) DigitalWrite(A, HIGH);
else DigitalWrite(A, LOW);
// Timer will reset on next scan
tReset = true;
}
// else do other stuff!
}
Last edited: