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 1341 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
  • ntewinkel
    ntewinkel over 13 years ago

    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), and 3 LEDs that go on when the pin goes HIGH.

     

    If anyone else wants to double check my work and/or give advice or tweaks, I welcome the input.

     

    /*

    Customized Vehicle with options, controlled by Arduino

    Written by Nico teWinkel

    www.mulBerryLand.com

    */

     

    // these are the pins the relays are connected to

    int RELAY1 = 2;

    int RELAY2 = 3;

    int RELAY3 = 4;

    //int RELAY4 = 5;

     

     

    // this is where the momentary switch is connected

    int STARTBUTTON = 8;

     

     

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

    // relay1 starts immediately

    long RELAY2_START_TIME = 10000;   // 0:10

    long RELAY3_START_TIME = 22000;   // 0:22

    long ACTION_STOP_TIME = 112000;  // 1:52    - stops relays 1 and 2

    long 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);

     

      // 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, LOW); // turn off relay1

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

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

          active = false;

        }

        else {

          active = true;

          startTime = millis();

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

          digitalWrite(RELAY1, HIGH); // 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, HIGH); // 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, HIGH); // turn on relay3

          relay3_active = true;

        }

     

     

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

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

          digitalWrite(RELAY3, LOW); // 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, LOW); // turn off relay1

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

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

          active = false;

          relay2_active = false;

          relay3_active = false;

        }

     

     

      }

     

    }

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • billabott
    billabott over 13 years ago in reply to ntewinkel

    That is awesome.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • billabott
    billabott over 13 years ago in reply to ntewinkel

    That is awesome.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
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