Skip to content

Commit

Permalink
added wave
Browse files Browse the repository at this point in the history
  • Loading branch information
byronxu99 committed Jun 22, 2018
1 parent f0afd5c commit c860e07
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/Wave/README.md
@@ -0,0 +1,10 @@
## Wave
**Boards:** Master and at least 2 valve boards
**Tube connections:** Connect all supply ports to the air supply. Connect each output port to an inflatable.

![](../images/Wave.jpg)

In this example, we demonstrate an advanced valve timing sequence. We want each valve to turn on shortly after its neighbor has turned on, thus propagating a "wave" motion down the row of inflatables. The code for this example is extendable to arbitrary numbers of valve boards.

We first designate the length of an entire wave cycle. Every time a new cycle begins, we compute the on and off times for each individual valve, which depends on the valve's position, the time offset between valves, and the duration we want each valve to stay on. This information is stored and used on subsequent `loop()` iterations to open and close valves when appropriate. When the end of the wave cycle occurs, the whole process repeats.

70 changes: 70 additions & 0 deletions examples/Wave/Wave.ino
@@ -0,0 +1,70 @@
#include <PneuDuino.h>

PneuDuino p;

// number of valve boards
#define N 2


// length of the wave cycle
unsigned long cycle_length = 1000L;
// time that the most recent cycle began
unsigned long cycle_start_time;

// times to turn on/off each valve
unsigned long on_times[2*N];
unsigned long off_times[2*N];

// duration to keep each valve on
unsigned long on_duration = 100L;


void setup() {
Serial.begin(9600);

p.begin();
p.setAddressMode(PNEUDUINO_ADDRESS_VIRTUAL);
}


void loop() {
p.update();

// get the current time
unsigned long m = millis();

// cycle is over, reset everything for the next cycle
if(m > cycle_start_time + cycle_length) {
// loop through all 2*N valves
for(int n=0; n<2*N; n++) {
on_times[n] = m + (n*cycle_length / (2*N));
off_times[n] = on_times[n] + on_duration;
}

// loop through all N valve boards
// turn off all valves (indexed from 1, not 0)
for(int n=0; n<N; n++) {
p.out(n+1, LEFT);
p.out(n+1, RIGHT);
}

// reset cycle start time
cycle_start_time = m;
}

// loop through all N valve boards
for(int n=0; n<N; n++) {
// if turn on time is reached, open the valve
if(m > on_times[2*n])
p.in(n+1, LEFT);
if(m > on_times[2*n + 1])
p.in(n+1, RIGHT);

// if turn off time is reached, close the valve
if(m > off_times[2*n])
p.out(n+1, LEFT);
if(m > off_times[2*n + 1])
p.out(n+1, RIGHT);
}
}

Binary file added examples/images/Wave.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c860e07

Please sign in to comment.