Timing of different events independently

Hi forum,

I intend to build a small production machine where I need certain relays controlled at specific times.
I put this into the LED section since programming leds or relays should be the same.
The mechanical part is pretty much finished of the project but now I need to programm the arduino nano.
The problem is that I am quite experienced in electronics but know close to nothing about programming.

What I need:
timing of 4 or 5 relays independently (to move an object through a small production chain)
I need to tweak the timings for every relay just right so it will work.
For example:
Relay 1 starts at 340ms and stays on for 250ms and so on.

triggered by a pushbutton.

Any help would be much appreciated.

Thanks,
oger

Are these relays on / off separately? That is, is there ever a time when two relays are on at the same time?

Relay 1 starts at 340ms and stays on for 250ms and so on.

These are very short times for a mechanical relay and although you can energise them at this speed with the pull in and pull out time this might not be the times the relays are actually on for.

oger:
The problem is that I am quite experienced in electronics but know close to nothing about programming.

Then start with the basic examples that come with the IDE.
You soon will get to the "BlinkWithoutDelay" example, that could do what you're asking.
Leo..

Thanks for the replies!
I did some digging and found out that it was easiest to just use a code for controlling a multi colored led.
I was able to enhance this to do exactly what I want.
Also I can use the pwm pins so I can regulate power to the coils.

Now I just need to figure out how to change the code the looping to a single push button running
the sequence...

You could use a while statement to 'freeze' the loop until you press a button.

while(digitalRead(buttonPin); // wait here until buttonPin becomes LOW

There are several other ways.
Post your code if you want help with it.
Leo..

Thanks,

here's the code.
Works great so far.
But I just need it to start at a pushbutton and loop as long as the pushbutton is pressed.
But It always has to end at the last step (after releasing the pushbutton) otherwise my object gets stuck in the process.

int valve = 3; // valve
int motorcw = 5; // Motor CW
int motorccw = 6; // Motor CCW
int feed = 9; // feed solenoid
void setup() {
pinMode(valve, OUTPUT);
pinMode(motorcw, OUTPUT);
pinMode(motorccw, OUTPUT);
pinMode(feed, OUTPUT);
}
void loop() {
setColor(255, 0, 0, 0); // activate valve
delay(250); // valve opening time
setColor(0, 255, 0, 255); // motor CW
delay(200); // motor travel time
setColor(0, 0, 0, 0); // loading
delay(100); // loading time
setColor(0, 0, 255, 255); // motor CCW
delay(200); // motor travel time
setColor(0, 0, 0, 0); // done
delay(2000);
}
void setColor(int redValue, int greenValue, int blueValue, int orangeValue) {
analogWrite(valve, redValue);
analogWrite(motorcw, greenValue);
analogWrite(motorccw, blueValue);
analogWrite(feed, orangeValue);
}

@larryd's tutorial implements most of what it seems you need. Naturally, there'll be tweaks and you'll have to modify the state machine control but, a lot of the grunt work is already done.

Thanks for the info.
I wish I saw that before.
But as my code is doing pretty much everything I need, I'll stick to that.
Just need this switch implemented and I'm done...

Hey, I figured it out myself!

Here's the code if anyone cares.
Thanks for all the help.

int Button = 1; // button
int valve = 3; // valve
int motorcw = 5; // Motor CW
int motorccw = 6; // Motor CCW
int feed = 9; // feed solenoid
void setup() {
pinMode(Button, INPUT_PULLUP);
pinMode(valve, OUTPUT);
pinMode(motorcw, OUTPUT);
pinMode(motorccw, OUTPUT);
pinMode(feed, OUTPUT);
pinMode(Button, INPUT_PULLUP);
}
void loop() {
while(digitalRead(Button) == 0){ // wait until button is pushed
}
setColor(255, 0, 0, 0); // activate valve
delay(250); // valve opening time
setColor(0, 255, 0, 255); // motor CW
delay(160); // motor travel time
setColor(0, 0, 0, 0); // loading
delay(100); // loading time
setColor(0, 0, 255, 255); // motor CCW
delay(160); // motor travel time
setColor(0, 0, 0, 0); // done
delay(0); // rate of production
}
void setColor(int redValue, int greenValue, int blueValue, int orangeValue) {
analogWrite(valve, redValue);
analogWrite(motorcw, greenValue);
analogWrite(motorccw, blueValue);
analogWrite(feed, orangeValue);
}

int Button = 1; // button
Pin 0 and 1 are already used by the USB<>Serial chip, so don't use these.

const byte buttonPin = 2; // button pin 2
const byte = 3; // valve pin 3
etc.
Best to use constant byte for values < 255 that do not change, like pin numbers. It saves memory.

pinMode(Button, INPUT_PULLUP); // internal pull up resistor assumes you are going to connect the button between pin and ground.
The pin is HIGH (from the pull up) when the button is not pressed (open circuit).

while(digitalRead(Button) == 0); // wait until button is pushed released
This assumes to wait here while the button is held down. Did you want that?

while(digitalRead(buttonPin); // this is the same...
while(digitalRead(buttonPin == HIGH); //as this
It means: wait here while the buttonPin is HIGH (not pressed).

Remove the two brackets there.

Next time use code tags. Read the "how to post" sticky on top of every main page.
Leo..

Thanks for the input.

I cleaned up the code accordingly (a bit)
See below.
I removed the PULLUP arguement,
Changed from pin 1

The confusion with the Pullup/pulldown is because it was written for pulldown,
but I want to use a pullup button.

Didn't care about the const byte cause I have enough memory.

When I remove the brackets as you mentioned the code starts at the first line of action already which is not desired.
Here it is:

    int Button = 2;   // button
    int valve = 3;    // valve
    int motorcw = 5;  // Motor CW
    int motorccw = 6; // Motor CCW
    int feed = 9;     // feed solenoid
    void setup() {
      pinMode(Button, INPUT);
      pinMode(valve, OUTPUT);
      pinMode(motorcw, OUTPUT);
      pinMode(motorccw, OUTPUT);
      pinMode(feed, OUTPUT);
      pinMode(Button, INPUT);
    }
    void loop() {
      while(digitalRead(Button) == 0){  // wait until button is pushed
    }
      setColor(255, 0, 0, 0);   // activate valve
      delay(250);               // valve opening time
      setColor(0, 255, 0, 255); // motor CW
      delay(160);               // motor travel time
      setColor(0, 0, 0, 0);     // loading
      delay(100);               // loading time
      setColor(0, 0, 255, 255); // motor CCW
      delay(160);               // motor travel time
      setColor(0, 0, 0, 0);     // done
      delay(0);              // rate of production
    }
    void setColor(int redValue, int greenValue, int blueValue, int orangeValue) {
      analogWrite(valve, redValue);
      analogWrite(motorcw, greenValue);
      analogWrite(motorccw, blueValue);
      analogWrite(feed, orangeValue);
    }