hello-
i have bank of intake solenoids have corresponding outtake solenoids.
for example if intake[0] on, outtake[0] must off. if outtake
the high times (inwhoosh[]) , high times (outwhoosh[]) different.
the high-in times longer high-out times. high-in times should take random values inwhoosh[] array , high-out times should take random values outwhoosh[] array.
i've tried work out logic , came psuedocode. not sure correct, or how go it. in advance.
my psuedocode
my actual code
i have bank of intake solenoids have corresponding outtake solenoids.
for example if intake[0] on, outtake[0] must off. if outtake
- high, intake
- must low.
the high times (inwhoosh[]) , high times (outwhoosh[]) different.
the high-in times longer high-out times. high-in times should take random values inwhoosh[] array , high-out times should take random values outwhoosh[] array.
i've tried work out logic , came psuedocode. not sure correct, or how go it. in advance.
my psuedocode
code: [select]
if intake state high intake(random [i])
set corresponding outtake state low (series of if statements?)
if intake state high , it's been longer on interval
grab currentmillis , set previousmillis
set intake state low
go beginning
if outtake state high outtake(random [i]) , it's been longer off interval
grab currentmillis , set previousmillis
set outtake state high
my actual code
code: [select]
#include <streaming.h> // library formatting printf
#define solenoidcount 8
unsigned long previousmillis[8]; // store last time solenoid toggled
unsigned long interval[] = {1700,2000,1500,700}; // interval time each solenoid on/off
int outtake[solenoidcount] = {1,2,3,4,5,6,7,8}; // intake pins
int intake[solenoidcount] = {9,10,11,12,14,15,16,17}; // exhaust pins
unsigned long inwhoosh[] = {4000,4900,4200,4800,4100,4200,4900,4400};
unsigned long outwhoosh[] = {2000,3000,1800,3700,2000,1100,3000,2000};
long int count = 0;
int led = 13;
void setup() { // set digital pin output , initialize previousmillis
serial.begin(9600);
unsigned long thismilli = millis();
for(int = 0; < 8; i++) {
pinmode(intake[i], output);
pinmode(outtake[i], output);
previousmillis[i] = thismilli;
pinmode(led, output);
digitalwrite(led, low);
}
}
void loop() {
unsigned long currentmillis = millis();
for(int = 0; < 8; i++) {
if(currentmillis - previousmillis[i] > inwhoosh[i]) { // switch states if current - previous > array value
//if current millis( ones have been counting since arduino turned on) - previous millis(points value in array) > interval [points value in array]
previousmillis[i] = currentmillis; // save last time flipped solenoid
if (digitalread(intake[i]) == low) {
digitalwrite(outtake[i], low);
digitalwrite(intake[i], high);
delay(200);
}
else{
digitalwrite(intake[i], low);
digitalwrite(outtake[i], high);
delay(200);
}
}
}
}
change interval array value @ each pin state change.
code: [select]
#include <streaming.h> // library formatting printf
#define solenoidcount 8
unsigned long previousmillis[8]; // store last time solenoid toggled
unsigned long interval[] = {1700,2000,1500,700}; // interval time each solenoid on/off
int outtake[solenoidcount] = {1,2,3,4,5,6,7,8}; // intake pins
int intake[solenoidcount] = {9,10,11,12,14,15,16,17}; // exhaust pins
unsigned long inwhoosh[] = {4000,4900,4200,4800,4100,4200,4900,4400};
unsigned long outwhoosh[] = {2000,3000,1800,3700,2000,1100,3000,2000};
long int count = 0;
int led = 13;
void setup() { // set digital pin output , initialize previousmillis
serial.begin(9600);
unsigned long thismilli = millis();
for(int = 0; < 8; i++) {
pinmode(intake[i], output);
pinmode(outtake[i], output);
previousmillis[i] = thismilli;
pinmode(led, output);
digitalwrite(led, low);
}
}
void loop() {
unsigned long currentmillis = millis();
for(int = 0; < 8; i++) {
if(currentmillis - previousmillis[i] > inwhoosh[i]) { // switch states if current - previous > array value
//if current millis( ones have been counting since arduino turned on) - previous millis(points value in array) > interval [points value in array]
previousmillis[i] = currentmillis; // save last time flipped solenoid
if (digitalread(intake[i]) == low) {
digitalwrite(outtake[i], low);
digitalwrite(intake[i], high);
// add this
interval[i] = inwhoosh[i];
}
else{
digitalwrite(intake[i], low);
digitalwrite(outtake[i], high);
// add this
interval[i] = outwhoosh[i];
}
}
}
}
Arduino Forum > Using Arduino > Programming Questions > Independent and random on and off times without delays
arduino
Comments
Post a Comment