Skip to content

Instantly share code, notes, and snippets.

@vysogot
Last active May 29, 2021 06:31
Show Gist options
  • Save vysogot/780e16da215a3aa6fc0a to your computer and use it in GitHub Desktop.
Save vysogot/780e16da215a3aa6fc0a to your computer and use it in GitHub Desktop.
Arduino – multiple output control with timeline
/* Algorithm based on delay for running different outputs in given LOW/HIGH intervals
This code is meant to control the relay grid behind a washing machine
and it can be used for many other devices. By Jakub Godawa (vysogot) */
/* addresses for Arduino Uno (!setup properly for Arduino Mega or other!) */
const int r1out = A0;
const int r2out = A1;
const int s3out = A2;
const int s4out = A3;
const int s5out = A4;
const int v1out = A5;
const int v2out = 11;
const int v3out = 10;
const int v4out = 9;
const int p1out = 8;
const int p2out = 7;
const int p3out = 6;
const int d1out = 5;
const int d2out = 4;
const int d3out = 3;
const int p4out = 2;
/* every array like t1, t2... has three config fields and the rest is for the timings */
const int initStep = 3;
/* after the timings of an array are finished that flag changes */
boolean isActive = true;
/* for setting delay and debbuging */
int startFrom = 0;
int currentCycle = 0;
/*
|-- these two are presetted --|
timings for each, fields: <output>, intial step, activation flag, <low offset>,
(<amount cycles high>, <amount cycles low>), ... , <amount cycles high>
in other words: output, init, flag, cLow, cHigh, cLow, cHigh...
EXAMPLE DESCRIPTION: s3out waits low for 11 minutes and 58 seconds,
then it runs high for 62 seconds, then waits low for 11 minutes and 58 seconds
and finally runs high for 62 seconds
int s3[] = { s3out, initStep, isActive, 12*60-2,
60+2, 12*60-2, 60+2 };
*/
/* Rotation clockwise [R1] */
int r1[] = { r1out, initStep, isActive, 0,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 194,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14,
10, 14, 10, 14, 10, 14, 10, 14, 10 };
/* Rotation counter clockwise [R2] */
int r2[] = { r2out, initStep, isActive, 12,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 194,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14,
10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14, 10, 14,
10, 14, 10, 14, 10, 14, 10, 14, 10 };
/* Spin slow [S3] */
int s3[] = { s3out, initStep, isActive, 12*60-2,
60+2, 12*60-2, 60+2 };
/* Spin medium [S4] */
int s4[] = { s4out, initStep, isActive, 13*60,
60, 12*60, 60 };
/* Spin fast [S5] */
int s5[] = { s5out, initStep, isActive, 14*60,
60, 12*60, 3*60 };
/* Drain valve [V1] */
int v1[] = { v1out, initStep, isActive, 3*60,
60, 60, 60, 60, 60, 60, 60, 60, 4*60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 6*60 };
/* Pre-wash drain valve [V2] */
int v2[] = { v2out, initStep, isActive, 3*60,
60 };
/* Main wash recirculation valve [V3] */
int v3[] = { v3out, initStep, isActive, 5*60,
60, 60, 60, 60, 60, 60, 4*60 };
/* Final ozonated wash valve [V4] */
int v4[] = { v4out, initStep, isActive, 16*60,
60, 60, 60, 60, 60, 60, 60, 60, 6*60 };
/* Recylculating pomp one [P1] */
int p1[] = { p1out, initStep, isActive, 0,
3*60, 60, 7*60, };
/* Recylculating pomp two [P2] */
int p2[] = { p2out, initStep, isActive, 15*60,
9*60 };
/* Refilling pomp [P3] */
int p3[] = { p3out, initStep, isActive, 3*60,
3*60 };
/* Soap dozator [D1s] */
int d1[] = { d1out, initStep, isActive, 60,
10, 14, 10, 14, 10, 14, 10, 14, 10 };
/* Bleach dozator [D2bl] */
int d2[] = { d2out, initStep, isActive, 60+12,
10, 14, 10, 14, 10, 14, 10, 14, 10 };
/* Acid dozator [D3a] */
int d3[] = { d3out, initStep, isActive, 17*60,
2*60 };
/* Air pomp [P4] */
int p4[] = { p4out, initStep, isActive, 0,
30*60 };
/* calculate limits for the arrays */
const int intSize = sizeof(int);
const int r1lim = (sizeof(r1)/intSize) - 1;
const int r2lim = (sizeof(r2)/intSize) - 1;
const int s3lim = (sizeof(s3)/intSize) - 1;
const int s4lim = (sizeof(s4)/intSize) - 1;
const int s5lim = (sizeof(s5)/intSize) - 1;
const int v1lim = (sizeof(v1)/intSize) - 1;
const int v2lim = (sizeof(v2)/intSize) - 1;
const int v3lim = (sizeof(v3)/intSize) - 1;
const int v4lim = (sizeof(v4)/intSize) - 1;
const int p1lim = (sizeof(p1)/intSize) - 1;
const int p2lim = (sizeof(p2)/intSize) - 1;
const int p3lim = (sizeof(p3)/intSize) - 1;
const int d1lim = (sizeof(d1)/intSize) - 1;
const int d2lim = (sizeof(d2)/intSize) - 1;
const int d3lim = (sizeof(d3)/intSize) - 1;
const int p4lim = (sizeof(p4)/intSize) - 1;
void setup() {
/* setup outputs */
pinMode(r1out, OUTPUT);
pinMode(r2out, OUTPUT);
pinMode(s3out, OUTPUT);
pinMode(s4out, OUTPUT);
pinMode(s5out, OUTPUT);
pinMode(v1out, OUTPUT);
pinMode(v2out, OUTPUT);
pinMode(v3out, OUTPUT);
pinMode(v4out, OUTPUT);
pinMode(p1out, OUTPUT);
pinMode(p2out, OUTPUT);
pinMode(p3out, OUTPUT);
pinMode(d1out, OUTPUT);
pinMode(d2out, OUTPUT);
pinMode(d3out, OUTPUT);
pinMode(p4out, OUTPUT);
}
/* logic for sending LOW or HIGH on the outputs depending on step position and counter */
void switcher(int t[], int limit) {
/* only if the array is active */
if (t[2]) {
/* increase step position if within the limit but with no more steps to perform */
if (t[1] <= limit) {
if ( t[t[1]] <= 0 ) {
t[1]++;
}
/* proceed only if starting delay is reached
if step position (array index) is EVEN write LOW if ODD write HIGH */
if ( startFrom <= currentCycle ) {
int state = LOW;
state = (t[1] % 2 == 1) ? LOW : HIGH;
digitalWrite(t[0], state);
}
/* decrease step counter in the position given only
if position is still within the limit (it could be increased before) */
if ( t[1] <= limit ) {
t[t[1]]--;
}
/* turn the output off and deactivate the array
if step position overflows the limit */
} else {
digitalWrite(t[0], LOW);
t[2] = false;
}
}
}
void loop() {
/* set the starting point, zero means from the beginning */
startFrom = 0;
currentCycle++;
/* run switcher function for each array */
switcher(r1, r1lim);
switcher(r2, r2lim);
switcher(s3, s3lim);
switcher(s4, s4lim);
switcher(s5, s5lim);
switcher(v1, v1lim);
switcher(v2, v2lim);
switcher(v3, v3lim);
switcher(v4, v4lim);
switcher(p1, p1lim);
switcher(p2, p2lim);
switcher(p3, p3lim);
switcher(d1, d1lim);
switcher(d2, d2lim);
switcher(d3, d3lim);
switcher(p4, p4lim);
/* speed the simulation up and calculate silently before running delay */
if ( startFrom <= currentCycle ) {
delay(1000);
}
}
@vysogot
Copy link
Author

vysogot commented Feb 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment