First Project - Guidance

I’m working on my first project using the arduino… I’m trying to control 6 selenoid valves and 2 leds using just a switch.

The scope is this: I’d like to blow air through six different ports for a determined amount of time. I plan to use 6 12V selenoid valves ( up through relays ( During the cycle, a red LED will be lit. Once the cycle ends, the red LED will go out and a green LED will light up. There will a manual switch that will cut power to the arduino after the cycle ends and the cycle should rerun as soon as power is reconnected.

I’m in the early stages on this, My arduino uno R3 is in the mail. I’ve attached some code i’ve spent 15 minutes on- it has an error though. Having little experience with this I’d appreciate any feedback I can get.

sketch_dec29a.ino (1.46 KB)

For a first, not bad.

Basic things:

  1. ctrl+t will “auto format” your code to make it a bit clearer.
  2. You have void setup()…but you seem to have deleted void loop().
void setup(){

// Any "setup" code goes here//

void loop(){

// Any code to be run goes here

  1. You can use loops to cut the number of lines of code in this. Look for a FOR loop.


for (byte i=2; i<8; i++){



I’ve attached some code i’ve spent 15 minutes on- it has an error though. Having little experience with this I’d appreciate any feedback I can get.

If you want help figuring out an error message you need to post the text of the message.

If you use delay() there is no way to interrupt the Arduino during that period. The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking.


A better thing would be to not use delay() and use millis() so the program could be stopped by pressing a button etc.

Just written this. Only a 5 minute job…so not the “best” or most efficient code…but it works.

Look up these terms for your learning!

  • FOR loops.
  • ints
  • floats
  • arrays
//Red LED on pin2.
//Green on pin 3.
// 1st Solenoid on pin 4.
//6th solenoid on pin 9.

// An array holding the "times" for each pump to be on (1-6).
int array_of_times[6] = {2, 2, 3, 1, 2, 3};

// Program has not run yet, so make a boolean value called program_state "false".
boolean program_state = false;

void setup() {
  Serial.begin(19200); // Start a serial line so you can debug/ print things to your PC.

  // Make pins 2-9 all OUTPUTs

  for (int i = 1; i < 10; i++)     // For every iteration of i from i being 1 to i being 10, do the stuff in {}. Add 1 to i each time (i++).
    pinMode(i, OUTPUT);


void loop() {
  // put your main code here, to run repeatedly:
  if (program_state == false) {
    //Turn on RED LED:
    digitalWrite(2, 1);

    // Now run through each pump using the times in the array_of_times as the "delay".

    for (int i = 4; i < 10; i++) {

      Serial.print("Turning on pump ");
      Serial.print(i - 3);
      Serial.print(" for ");
      Serial.print((1000 * array_of_times[i - 4]));
      Serial.println(" milliseconds");

      digitalWrite(i, 1);
      delay((1000 * array_of_times[i - 4]));
      digitalWrite(i, 0);


    // Now turn off the red LED and on the green.

    digitalWrite(2, 0);
    digitalWrite(3, 1);

    //Set the boolean value to TRUE;

    program_state = true;


else {
  Serial.println("Program has already run. Restart when ready.");


Thanks johnny, I gave you some karma for that. I plan to run this without an attached PC. Do you have any idea for a power supply. This is going to essentially be running a dryer for a manifold in a manufacturing plant. The valves will be blowing air through each of the different passages in the part. Once I figure out the amount of time for each valve, there shouldn't be any need for error logs and that sort of thing.

The little USB wall warts sold for use for charging phones. work well for powering the arduino. I usually use one for the arduino and another for the relay board. That way you can take advantage of the opto-isolators that are on the relay board to protect the arduino from voltage spikes that may occur.

This Thread shows the how to wire it to provide proper isolation. 8 Channel Relay