element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Relay sketch required for a charity project.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 15 replies
  • Subscribers 402 subscribers
  • Views 1344 views
  • Users 0 members are here
Related

Relay sketch required for a charity project.

Former Member
Former Member over 13 years ago

A big hello from the UK!

 

I have a charity car project that is ongoing to raise funds for Help for Heroes. Our vehicle will be customised with lights, motors and sounds.

 

I have a standard arduino and a 4-relay board which is working well using various sketches I have found on the net.

 

I was wondering if anyone could help me with a sketch that will turn on 3, or 4 of these relays at a time interval that I can set from a single N/O momentery switch. I also need it to reset after a certain time so that when the button is pressed again the sequence will start again.

 

I have an HD44780 LCD that I'd like to work into the design that could display text just before the relevent relay becomes active. (This is not essential as the relays are my priority just now)

 

As it stands my relays, 1-4 are connected to pins 2-5 although I don't need the 4th relay at this time.

 

Basically the car will loosely resemble a helicopter and will have a set up of lights, motors and a cheap MP3 player that has a physical switch that when made instantly starts the helicopter sound file.

 

If I'm totally honest i think I need a 'pro' to write me a sketch that is really basic as I lack the skills to do it myself.

 

The lights, motors and mp3 will be powered externally and will be initiated with the arduino like this:

 

1. User presses a N/O momentery switch.

 

2. Relay 1 makes & MP3 plays, engine start up - and remains on for duration of file (1:52).

 

3. After 10 secs - Relay 2 makes and Navigation lights iluminate.

 

4. After engine warm-up 22secs relay 3 makes and 'rotors' start.

 

5. After 1:40 relay 3 breaks, rotors stop.

 

6. After 1:52 relays 1 & 2 break.

 

7. Next press of button, steps 1-6 start again.

 

If it is easier I can use a locking switch, would that would solve the re-set?

 

I know this is a big ask but i severely lack the programming knowledge to do this.

 

I hope someone can help, i'm sure it's not too difficult for you pros!

 

I really appreciate people reading this and taking the time to reply.

 

Many thanks in advance.

 

VB

  • Sign in to reply
  • Cancel

Top Replies

  • ntewinkel
    ntewinkel over 13 years ago +2
    Here we go. I had forgotten the 10k resistor to ground, so it took me a while to figure out why the button press was behaving so oddly! I tested using a momentary-on switch (gives 5 volts (high) when pressed…
  • ntewinkel
    ntewinkel over 13 years ago +1
    Challenge... accepted!
  • ntewinkel
    ntewinkel over 13 years ago in reply to Former Member +1
    Hi Kris, I wrote it so that a line going HIGH means "ON", which I guess happens to be backwards To reverse that we just need to switch my HIGHs and LOWs wherever it says "digitalWrite(RELAY...". I just…
Parents
  • Former Member
    Former Member over 13 years ago

    Nico,

     

    Firstly thankyou for your help - fantasticly quick response!

     

    I have just got in from work and tested the sketch. I think it's pretty much there however it's kinda working backwards I think.

     

    When the arduino is powered all 3 relays are 'active'

     

    I have timed the relays changing state, here is what happens:

     

    Arduino powered - R1,2 & 3 'On'

    Button Pressed - R1 'Off'

    10 seconds - R2 'Off'

    22 seconds - R3 'Off' (R1,2 & 3 'Off)

    1 min 40 seconds - R3 'On'

    1 min 52 seconds - R1,2 & 3 'On'

     

    Basically what's on should be off & what's off should be on.

     

    The pushbuton does start the sequence when pushed and when a second press is made it re-sets which is perfect.

     

    Hope it's not too difficult to modify.

     

    Thanks again.

     

    VB (Kris)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • Former Member
    Former Member over 13 years ago

    Nico,

     

    Firstly thankyou for your help - fantasticly quick response!

     

    I have just got in from work and tested the sketch. I think it's pretty much there however it's kinda working backwards I think.

     

    When the arduino is powered all 3 relays are 'active'

     

    I have timed the relays changing state, here is what happens:

     

    Arduino powered - R1,2 & 3 'On'

    Button Pressed - R1 'Off'

    10 seconds - R2 'Off'

    22 seconds - R3 'Off' (R1,2 & 3 'Off)

    1 min 40 seconds - R3 'On'

    1 min 52 seconds - R1,2 & 3 'On'

     

    Basically what's on should be off & what's off should be on.

     

    The pushbuton does start the sequence when pushed and when a second press is made it re-sets which is perfect.

     

    Hope it's not too difficult to modify.

     

    Thanks again.

     

    VB (Kris)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • ntewinkel
    ntewinkel over 13 years ago in reply to Former Member

    Hi Kris,

     

    I wrote it so that a line going HIGH means "ON", which I guess happens to be backwards image

    To reverse that we just need to switch my HIGHs and LOWs wherever it says "digitalWrite(RELAY...".

     

    I just checked my own relay board, and noticed that my relays behave the same as yours, where LOW = ON, and HIGH = OFF.

    I added #define's for easy switching of HIGH/LOW, in case some other relay boards behave differently in the future.

     

    I also noticed that I was wasting a bit of memory for constants, so I made them into #define's, which is more elegant.

     

    ps, you can use Serial monitor to see the debug messages I included.

     

    So here is the updated code:

     

    /*

    Customized Vehicle, with options controlled by Arduino

    Written by Nico teWinkel

    www.mulBerryLand.com

    */

     

     

    #define RELAY_ON  LOW

    #define RELAY_OFF HIGH

     

     

    // these are the pins the relays are connected to

    #define RELAY1 2

    #define RELAY2 3

    #define RELAY3 4

    //#define RELAY4 5

     

     

    // this is where the momentary switch is connected

    #define STARTBUTTON 8

     

     

    // set up the start and stop times, in milliseconds

    // relay1 starts immediately

    #define RELAY2_START_TIME 10000   // 0:10

    #define RELAY3_START_TIME 22000   // 0:22

    #define ACTION_STOP_TIME 112000   // 1:52    - stops relays 1 and 2

    #define RELAY3_STOP_TIME 100000   // 1:40

     

     

    // this keeps track of the time since the button press

    long startTime;

    long lastTimeButtonPressed;

    long lastOutputTime;

     

     

    // active is TRUE when the actions are happening

    boolean active;

    boolean relay2_active;

    boolean relay3_active;

     

    void setup() {

      // start the serial for debug

      Serial.begin(9600);

      Serial.println("Starting application.");

     

      // set the relay pins as outputs

      pinMode(RELAY1, OUTPUT);

      pinMode(RELAY2, OUTPUT);

      pinMode(RELAY3, OUTPUT);

      //pinMode(RELAY4, OUTPUT);

     

     

      // make sure the relays are all off to start with

      digitalWrite(RELAY1, RELAY_OFF); // turn off relay1

      digitalWrite(RELAY2, RELAY_OFF); // turn off relay2

      digitalWrite(RELAY3, RELAY_OFF); // turn off relay3

     

      // set the pushbutton pin as an input

      pinMode(STARTBUTTON, INPUT);

     

      startTime = 0;

      active = false;

      relay2_active = false;

      relay3_active = false;

      lastTimeButtonPressed = -1000; // to avoid the initial 1 second button ignore

      lastOutputTime = 0;

    }

     

     

    void loop() {

     

     

      // check for button push

      // read the state of the switch into a local variable:

      int reading = digitalRead(STARTBUTTON);

     

      // ignore if button is pressed more than once per second

      // this also simplifies debounce

      if (millis() - lastTimeButtonPressed > 1000 && reading == HIGH) {

        lastTimeButtonPressed = millis();

        Serial.println("Button was pressed.");

       

        if (active) {

          // pushing the button when things are going on stops everything

          digitalWrite(RELAY1, RELAY_OFF); // turn off relay1

          digitalWrite(RELAY2, RELAY_OFF); // turn off relay2

          digitalWrite(RELAY3, RELAY_OFF); // turn off relay3

          active = false;

        }

        else {

          active = true;

          startTime = millis();

          Serial.println("Turning ON relay 1");

          digitalWrite(RELAY1, RELAY_ON); // turn on relay1

        }

      }

     

      if (active) {

        // show time once per second

        if((millis() - lastOutputTime) > 1000) {

          Serial.print("Time: ");

          Serial.println((millis() - startTime) / 1000);

          lastOutputTime = millis();

        }

       

        if (!relay2_active && millis() - startTime > RELAY2_START_TIME && millis() - startTime < ACTION_STOP_TIME) {

          Serial.println("Turning ON relay 2");

          digitalWrite(RELAY2, RELAY_ON); // turn on relay2

          relay2_active = true;

        }

     

     

        if (!relay3_active && millis() - startTime > RELAY3_START_TIME && millis() - startTime < RELAY3_STOP_TIME) {

          Serial.println("Turning ON relay 3");

          digitalWrite(RELAY3, RELAY_ON); // turn on relay3

          relay3_active = true;

        }

     

     

        if (relay3_active && millis() - startTime > RELAY3_STOP_TIME) {

          Serial.println("Turning OFF relay 3");

          digitalWrite(RELAY3, RELAY_OFF); // turn off relay3

          relay3_active = false;

        }

     

     

        if (active && millis() - startTime > ACTION_STOP_TIME) {

          Serial.println("Turning OFF all relays");

          // stop everything

          // relay3 should already be off,

          //   but this makes sure it's all off in case there was a delay

          digitalWrite(RELAY1, RELAY_OFF); // turn off relay1

          digitalWrite(RELAY2, RELAY_OFF); // turn off relay2

          digitalWrite(RELAY3, RELAY_OFF); // turn off relay3

          active = false;

          relay2_active = false;

          relay3_active = false;

        }

     

     

      }

     

    }

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube