prototyping a new medical device

I am prototyping a new design for a mobile medical device to benefit patients awaiting kidney transplant/donors, dialysis patients, diabetics, and possibly even those on chemotherepy.

Arduino Uno
i have 3 peristaltics on digital pins 2,3,4.
1 red led on digital pin 5
1 green led on digital pin 6
1 button on analog pin 3 to trigger the program loop

the hardware and electronics are complete esentially what i need is help with the sketch/program to run the hardware

when the momentary button is pressed pumps need to all start/run together for the predetermined times
pump 1 to run for 240 seconds.
pump 2 to run for 30 seconds.
pump 3 to run for 5 seconds.
red led lights when treatment cycle starts/ button presed to start program loop and stays lit until treatment cycle ends
green led blinks for 25 seconds when cycle completes then turns off
system awaits another button press

my pump/arduino interface is transistor based and activates when given a 5volt signal
my led's run directly from the arduino pins 5,6.

Any suggestions on how to write this program or example sketches for my arduino uno would be greatly appreciated by myself, my daughter, and possibly thousands of future patients who recieve this device

Just a friendly "heads up". Getting a medical device approved by the FDA (a requirement if you intend to market/use your product in the USA) is very complicated and expensive. Even if you make it through the initial steps, you are looking at several years of compliance testing.

I'm not trying to talk you out of it, I'm just passing along what I learned when I had ideas for some new medical equipment.

You might want to get in touch with a medical device testing lab for advice on how best to proceed.

This may be a very good idea but electronic devices have to meet very stringent conditions for medical use.

If you are serious get legal advice.And medical advice.

Medical electroniccs can be lucrative but you have to be sure you are right and have lots of lawyers you can aford

i understand the long term requirements for a project like this, im just trying to get a working prototype for proof of concept

If you are thinking of using an Atmega 328, etc. it is not rated for medical applications.

the arduino uno will not be used in the final design, only in prototyping stages for ease of use/expandability

There are a couple examples of timing code here.

DuaneDegn:
There are a couple examples of timing code here.

could this be easiy adapted to suit my application requirements? i am dyslexic and have quite a bit of trouble with programming (or reading for that matter Lol) my daughter usually helps me with the programming of my projects but she is unwell at the time
(she compares teaching me to program to teaching a 4 year old algebra) so please go easy on me

See Demonstration code for several things at the same time - Project Guidance - Arduino Forum

Mark

Zaksutro:
could this be easiy adapted to suit my application requirements?

I think so.

This program is a bit more complex than it really needs to be because I adapted the code I linked to earlier to write this program.

The code compiles but I don't have time to test it. Let me know if it does something unexpected.

const unsigned long MILLISECONDS_PER_SECOND = 1000;
const unsigned long MILLISECONDS_PER_MINUTE = MILLISECONDS_PER_SECOND * 60;
const unsigned long MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUTE * 60;
const unsigned long MILLISECONDS_PER_DAY = MILLISECONDS_PER_HOUR * 24;
const unsigned long ON_TIME_INTERVAL[] = {MILLISECONDS_PER_SECOND * 240, MILLISECONDS_PER_SECOND * 30, MILLISECONDS_PER_SECOND * 5};
const int GREEN_ON_TIME = 250;
const int GREEN_OFF_TIME = 250;
const byte SECONDS_TO_BLINK = 25;
const byte TIMES_TO_BLINK = SECONDS_TO_BLINK * MILLISECONDS_PER_SECOND / (GREEN_ON_TIME + GREEN_OFF_TIME);
const byte PUMPS_IN_USE = 3;

const byte PUMP_PIN[] = {2, 3, 4};
const boolean ACTIVE_STATE = 1;
const boolean INACTIVE_STATE = 0; //!ACTIVE_STATE; // would this work?
const boolean ACTIVE_BUTTON = 0;
const byte BUTTON_PIN = A3;
const byte RED_LED_PIN = 5;
const byte GREEN_LED_PIN = 6;
byte pumpsActive;
unsigned long lastTransitionTime[PUMPS_IN_USE];


boolean startedFlag[PUMPS_IN_USE];
boolean pumpOnFlag[PUMPS_IN_USE];
unsigned long programStartTime;

void setup()
{
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);

  for (int i = 0; i < PUMPS_IN_USE; i++)
  {
    digitalWrite(PUMP_PIN[i], INACTIVE_STATE);
    pinMode (PUMP_PIN[i], OUTPUT); 
    startedFlag[i] = 0;
  }
}

void loop()
{
  while (digitalRead(BUTTON_PIN) != ACTIVE_BUTTON) {} // wait for button press
  pumpsActive = PUMPS_IN_USE;
  digitalWrite(RED_LED_PIN, HIGH);
  unsigned long now = millis();
  for (int i = 0; i < PUMPS_IN_USE; i++)
  {
    turnOn(i);
    lastTransitionTime[i] = now;
    pumpOnFlag[i] = 0;
  }
  while (pumpsActive)
  {
    for (int i = 0; i < PUMPS_IN_USE; i++)
    {
      if (pumpOnFlag[i])
      {
        if (now - lastTransitionTime[i] > ON_TIME_INTERVAL[i])
        {
          turnOff(i);
          pumpsActive--;
        }
      }
    }
  }
  digitalWrite(RED_LED_PIN, LOW);
  blinkLed(GREEN_LED_PIN, GREEN_ON_TIME, GREEN_OFF_TIME, TIMES_TO_BLINK);
}
void turnOn(byte pumpIndex)
{
  pumpOnFlag[pumpIndex] = 1;
  digitalWrite(PUMP_PIN[pumpIndex], ACTIVE_STATE);
}

void turnOff(byte pumpIndex)
{
  pumpOnFlag[pumpIndex] = 0;
  digitalWrite(PUMP_PIN[pumpIndex], INACTIVE_STATE);
}

void blinkLed(byte ledPin, int onTime, int offTime, int timesToBlink)
{
  for (int i = 0; i < timesToBlink; i++)
  {
    digitalWrite(ledPin, HIGH);
    delay(onTime);
    digitalWrite(ledPin, LOW);
    delay(offTime);
  }
}

Edit: I had some of the times wrong earlier. I've found several bugs since I originally posted this. I'm not aware of any bugs presently but use with caution.

holmes4:
See Demonstration code for several things at the same time - Project Guidance - Arduino Forum

Mark

I think this is one program which can be written sequentially. This program could use "delay()" for the timing.

Here's a simpler version of the code.

const unsigned long MILLISECONDS_PER_SECOND = 1000;
const unsigned long ON_TIME_LONGEST = MILLISECONDS_PER_SECOND * 240;
const unsigned long ON_TIME_MIDDLE = MILLISECONDS_PER_SECOND * 30;
const unsigned long ON_TIME_SHORTEST = MILLISECONDS_PER_SECOND * 5;
const unsigned long PIN_LONGEST = 2;
const unsigned long PIN_MIDDLE = 3;
const unsigned long PIN_SHORTEST = 4;
const int GREEN_ON_TIME = 250;
const int GREEN_OFF_TIME = 250;
const byte SECONDS_TO_BLINK = 25;

// "TIMES_TO_BLINK" is calculated from the above three constants.
const byte TIMES_TO_BLINK = SECONDS_TO_BLINK * MILLISECONDS_PER_SECOND / (GREEN_ON_TIME + GREEN_OFF_TIME);

const boolean ACTIVE_STATE = HIGH;    // Pin state to turn on the pump
const boolean INACTIVE_STATE = LOW;   //!ACTIVE_STATE; // would this work?
const boolean ACTIVE_BUTTON = LOW;    // Assumes the pin will be pulled high.
const byte BUTTON_PIN = A3;
const byte RED_LED_PIN = 5;
const byte GREEN_LED_PIN = 6;

void setup()
{
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);

  digitalWrite(PIN_SHORTEST, INACTIVE_STATE);
  pinMode (PIN_SHORTEST, OUTPUT);
  digitalWrite(PIN_MIDDLE, INACTIVE_STATE);
  pinMode (PIN_MIDDLE, OUTPUT);
  digitalWrite(PIN_LONGEST, INACTIVE_STATE);
  pinMode (PIN_LONGEST, OUTPUT); 
}

void loop()
{
  while (digitalRead(BUTTON_PIN) != ACTIVE_BUTTON) {} // wait for button press

  digitalWrite(RED_LED_PIN, HIGH);

  // Turn on all the pumps.
  digitalWrite(PIN_SHORTEST, ACTIVE_STATE);
  digitalWrite(PIN_MIDDLE, ACTIVE_STATE);
  digitalWrite(PIN_LONGEST, ACTIVE_STATE);
  
  // Turn off pumps one at a time.
  delay(ON_TIME_SHORTEST);
  digitalWrite(PIN_SHORTEST, INACTIVE_STATE);
  delay(ON_TIME_MIDDLE - ON_TIME_SHORTEST);
  digitalWrite(PIN_MIDDLE, INACTIVE_STATE);
  delay(ON_TIME_LONGEST - ON_TIME_MIDDLE - ON_TIME_SHORTEST);
  digitalWrite(PIN_LONGEST, INACTIVE_STATE);

  // All the pumps are off.
  digitalWrite(RED_LED_PIN, LOW);

  // Blink the green LED.
  for (int i = 0; i < TIMES_TO_BLINK; i++)
  {
    digitalWrite(GREEN_LED_PIN, HIGH);
    delay(GREEN_ON_TIME);
    digitalWrite(GREEN_LED_PIN, LOW);
    delay(GREEN_OFF_TIME);
  }
}

DuaneDegn:
Here's a simpler version of the code.

const unsigned long MILLISECONDS_PER_SECOND = 1000;

const unsigned long ON_TIME_LONGEST = MILLISECONDS_PER_SECOND * 240;
const unsigned long ON_TIME_MIDDLE = MILLISECONDS_PER_SECOND * 30;
const unsigned long ON_TIME_SHORTEST = MILLISECONDS_PER_SECOND * 5;
const unsigned long PIN_LONGEST = 2;
const unsigned long PIN_MIDDLE = 3;
const unsigned long PIN_SHORTEST = 4;
const int GREEN_ON_TIME = 250;
const int GREEN_OFF_TIME = 250;
const byte SECONDS_TO_BLINK = 25;

// "TIMES_TO_BLINK" is calculated from the above three constants.
const byte TIMES_TO_BLINK = SECONDS_TO_BLINK * MILLISECONDS_PER_SECOND / (GREEN_ON_TIME + GREEN_OFF_TIME);

const boolean ACTIVE_STATE = HIGH;    // Pin state to turn on the pump
const boolean INACTIVE_STATE = LOW;  //!ACTIVE_STATE; // would this work?
const boolean ACTIVE_BUTTON = LOW;    // Assumes the pin will be pulled high.
const byte BUTTON_PIN = A3;
const byte RED_LED_PIN = 5;
const byte GREEN_LED_PIN = 6;

void setup()
{
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);

digitalWrite(PIN_SHORTEST, INACTIVE_STATE);
  pinMode (PIN_SHORTEST, OUTPUT);
  digitalWrite(PIN_MIDDLE, INACTIVE_STATE);
  pinMode (PIN_MIDDLE, OUTPUT);
  digitalWrite(PIN_LONGEST, INACTIVE_STATE);
  pinMode (PIN_LONGEST, OUTPUT);
}

void loop()
{
  while (digitalRead(BUTTON_PIN) != ACTIVE_BUTTON) {} // wait for button press

digitalWrite(RED_LED_PIN, HIGH);

// Turn on all the pumps.
  digitalWrite(PIN_SHORTEST, ACTIVE_STATE);
  digitalWrite(PIN_MIDDLE, ACTIVE_STATE);
  digitalWrite(PIN_LONGEST, ACTIVE_STATE);
 
  // Turn off pumps one at a time.
  delay(ON_TIME_SHORTEST);
  digitalWrite(PIN_SHORTEST, INACTIVE_STATE);
  delay(ON_TIME_MIDDLE - ON_TIME_SHORTEST);
  digitalWrite(PIN_MIDDLE, INACTIVE_STATE);
  delay(ON_TIME_LONGEST - ON_TIME_MIDDLE - ON_TIME_SHORTEST);
  digitalWrite(PIN_LONGEST, INACTIVE_STATE);

// All the pumps are off.
  digitalWrite(RED_LED_PIN, LOW);

// Blink the green LED.
  for (int i = 0; i < TIMES_TO_BLINK; i++)
  {
    digitalWrite(GREEN_LED_PIN, HIGH);
    delay(GREEN_ON_TIME);
    digitalWrite(GREEN_LED_PIN, LOW);
    delay(GREEN_OFF_TIME);
  }
}

I've been trying to get this code going for a few days to no avail any help would be greatly appreciated

Zaksutro:
I've been trying to get this code going for a few days to no avail any help would be greatly appreciated

So did any of the LEDs turn on? Did any pumps turn on?

Can you tell us more about what was unexpected?

It's often helpful to add serial statements to a program so you can monitor which part of the code is running.

I added some serial statements to the simple version of the code.

const unsigned long MILLISECONDS_PER_SECOND = 1000;
const unsigned long ON_TIME_LONGEST = MILLISECONDS_PER_SECOND * 240;
const unsigned long ON_TIME_MIDDLE = MILLISECONDS_PER_SECOND * 30;
const unsigned long ON_TIME_SHORTEST = MILLISECONDS_PER_SECOND * 5;
const unsigned long PIN_LONGEST = 2;
const unsigned long PIN_MIDDLE = 3;
const unsigned long PIN_SHORTEST = 4;
const int GREEN_ON_TIME = 250;
const int GREEN_OFF_TIME = 250;
const byte SECONDS_TO_BLINK = 25;

// "TIMES_TO_BLINK" is calculated from the above three constants.
const byte TIMES_TO_BLINK = SECONDS_TO_BLINK * MILLISECONDS_PER_SECOND / (GREEN_ON_TIME + GREEN_OFF_TIME);

const boolean ACTIVE_STATE = HIGH;    // Pin state to turn on the pump
const boolean INACTIVE_STATE = LOW;   //!ACTIVE_STATE; // would this work?
const boolean ACTIVE_BUTTON = LOW;    // Assumes the pin will be pulled high.
const byte BUTTON_PIN = A3;
const byte RED_LED_PIN = 5;
const byte GREEN_LED_PIN = 6;

void setup()
{
  Serial.begin(9600);
  Serial.println("Pump Timer Program");
  
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);

  digitalWrite(PIN_SHORTEST, INACTIVE_STATE);
  pinMode (PIN_SHORTEST, OUTPUT);
  digitalWrite(PIN_MIDDLE, INACTIVE_STATE);
  pinMode (PIN_MIDDLE, OUTPUT);
  digitalWrite(PIN_LONGEST, INACTIVE_STATE);
  pinMode (PIN_LONGEST, OUTPUT); 
}

void loop()
{
  Serial.println(); 
  Serial.println("All Pumps Off");
  Serial.println(); 
  Serial.println("Waiting for button press.");
  while (digitalRead(BUTTON_PIN) != ACTIVE_BUTTON) {} // wait for button press
  Serial.println("The button has been pressed.");
  
  digitalWrite(RED_LED_PIN, HIGH);

  // Turn on all the pumps.
  digitalWrite(PIN_SHORTEST, ACTIVE_STATE);
  digitalWrite(PIN_MIDDLE, ACTIVE_STATE);
  digitalWrite(PIN_LONGEST, ACTIVE_STATE);
  Serial.println(); 
  Serial.println("All Pumps On");
   
  // Turn off pumps one at a time.
  delay(ON_TIME_SHORTEST);
  digitalWrite(PIN_SHORTEST, INACTIVE_STATE);
  
  Serial.println(); 
  Serial.println("The first pump has been turned off.");
  
  delay(ON_TIME_MIDDLE - ON_TIME_SHORTEST);
  digitalWrite(PIN_MIDDLE, INACTIVE_STATE);
  
  Serial.println(); 
  Serial.println("The second pump has been turned off.");
  
  delay(ON_TIME_LONGEST - ON_TIME_MIDDLE - ON_TIME_SHORTEST);
  digitalWrite(PIN_LONGEST, INACTIVE_STATE);

  Serial.println(); 
  Serial.println("The last pump as been turned off.");
  
  // All the pumps are off.
  digitalWrite(RED_LED_PIN, LOW);

  Serial.println(); 
  Serial.print("The red LED is off and the green LED will now blink ");
  Serial.print(TIMES_TO_BLINK);
  Serial.println(" times.");
  
  // Blink the green LED.
  for (int i = 0; i < TIMES_TO_BLINK; i++)
  {
    digitalWrite(GREEN_LED_PIN, HIGH);
    delay(GREEN_ON_TIME);
    digitalWrite(GREEN_LED_PIN, LOW);
    delay(GREEN_OFF_TIME);
  }
  Serial.println(); 
  Serial.println("End of loop.");
}

This is the output you should expect.

Pump Timer Program

All Pumps Off

Waiting for button press.
The button has been pressed.

All Pumps On

The first pump has been turned off.

The second pump has been turned off.

The last pump as been turned off.

The red LED is off and the green LED will now blink 50 times.

End of loop.

All Pumps Off

Waiting for button press.

I used an active low button. One side of the button was connected to the Arduino's "A3" pin and the other side connected to ground.

The I/O pins controlling the pumps will be set high to turn on the pumps and set low to turn them off.

You should have a current limiting resistor between the Arduino's I/O pin.

Arduino I/O pin -> resistor -> LED anode |LED| LED cathode -> ground

I think a resistor value of 220 ohm to 1k ohm should work okay with most LEDs.

Do you have the transistors and pumps wired correctly?

Let us know which LEDs are on, and which pumps are active at various points of the program. Use the serial output to help identify which part of the program is running.