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
  • Former Member
    Former Member over 13 years ago

    Nico,

     

    I can't thank you enough, It works perfectly!  image

     

    How difficult would it be to include an HD44780 LCD in the sketch? It is not essential, I just thought kids would love it when they see a message coming up on the display telling them what was happening in the 'Start-up sequence'.

     

    I will credit you on our website for your help and also put your business web address on the vehicle - free advertising throughout Europe from UK to Monaco!

     

    How would you like me to describe you? Nico teWinkel from Canada / Company name / University?

     

    Thanks again

     

    Kris

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

    Hi Kris,

     

    That's great that it's working for you now!

    For the LCD, I have a little 16x2 display here I can test with, like this one: http://arduino.cc/en/Tutorial/LiquidCrystal - if that's the kind of display you have, then it's not difficult at all to make that happen and then it should be just a matter of wiring it up the same way on your side.

    I can work on that later tonight.

     

    And thanks for the publicity! mulBerryLand is a guest house, so international coverage is always welcome image

    And whatever you feel is approriate, maybe something like Nico teWinkel (is a software engineer) from BC, Canada. www.mulBerryLand.com ?

    mulBerryLand is a guest house and has a little art gallery (my wife is an artist). I develop iPhone/Android apps and some firmware.

    Would you be able to send pictures when it's done, then we'll post it on our website too image

    Thanks again for that!

     

    Cheers,

    -Nico

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

    Nico,

     

    Yeah that would be fantastic. I have a 16x2 on order and have a 20x4 here just now but think that may be too big. I'll await your additions!

     

    I have put a new post up on our site and have added you to the Sponsors & Supporters page. If you want anything changing let me know!

     

    Thanks again,

     

    Kris - http://www.forukeo.co.uk

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

    Nico,

     

    Yeah that would be fantastic. I have a 16x2 on order and have a 20x4 here just now but think that may be too big. I'll await your additions!

     

    I have put a new post up on our site and have added you to the Sponsors & Supporters page. If you want anything changing let me know!

     

    Thanks again,

     

    Kris - http://www.forukeo.co.uk

    • 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,

     

    Thanks, what you put there is very nice!

     

    Here is the update that includes an LCD display.

     

    First note is that I moved the button to digital pin 6 - so you need to move the button connection as well as the resistor to ground.

     

    For the display, I have 16x2, but to use 20x4 you just need to change the lcd.begin line to read lcd.begin(20, 4) and it should work as is, except that it will only use the top two lines of the display.

    You can change any of the messages, but keep in mind that the message has to fit across the display (in my case 16 characters).

     

    for the 20x4, I would change the updateDisplay method to look like this:

     

    void updateDisplay(char *line1, char *line2, char *line3, char *line4) {

        lcd.setCursor(0,0);

        lcd.print(line1);

        lcd.setCursor(0,1);

        lcd.print(line2);

        lcd.setCursor(0,2);

        lcd.print(line3);

        lcd.setCursor(0,3);

        lcd.print(line4);

    }

     

    And then the message lines would look like this:

      updateDisplay("Fasten your seatbelt", "Press the button... ", "Be nice to others   ", "line 4 message      ");

     

    Pad the messages out to the right number of characters to erase anything that was there before.

     

    Good luck!

     

    Cheers,

    -Nico

     

    ps, I wonder if there's a better way to attach files to these posts!

     

     

    /*

    Customized Vehicle, with options controlled by Arduino

    Written by Nico teWinkel

    www.mulBerryLand.com

    */

    /*

    Connections:

    For Button

    * button one side to +5v

    * button other side to digital pin 6

    * 10k resistor from digital pin 6 to ground

     

    For Relay

    * Relay 1 = digital pin 2

    * Relay 2 = digital pin 3

    * Relay 3 = digital pin 4

    * digital pin 5 is reserved for Relay 4

     

    For LCD

    * LCD RS pin to digital pin 12

    * LCD Enable pin to digital pin 11

    * LCD D4 pin to digital pin 7

    * LCD D5 pin to digital pin 8

    * LCD D6 pin to digital pin 9

    * LCD D7 pin to digital pin 10

    * LCD R/W pin to ground

    * LCD VSS to ground

    * LCD VDD to +5v

    * Variable resistor:

    *    ends to +5V and ground

    *    wiper to LCD VO pin (pin 3)

    * backlight:

    * LCD A (pin 15) to +5v

    * LCD K (pin 16) to 360k resistor to GND

    */

    // include the LCD library

    #include <LiquidCrystal.h>

     

     

    #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 6

     

     

    // 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;

     

     

    // initialize the library with the numbers of the interface pins

    LiquidCrystal lcd(12, 11, 7, 8, 9, 10);

     

     

    void updateDisplay(char *line1, char *line2) {

        lcd.setCursor(0,0);

        lcd.print(line1);

        lcd.setCursor(0,1);

        lcd.print(line2);

    }

     

     

    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;

     

      // set up the LCD's number of columns and rows:

      lcd.begin(16, 2);

      // Print a message to the LCD.

      updateDisplay("Fasten seatbelts", "Press button...");

    }

     

     

    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;

          updateDisplay("Fasten seatbelts", "Press button...");

        }

        else {

          active = true;

          startTime = millis();

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

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

          updateDisplay("Engine is on!   ", "                ");

        }

      }

     

      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;

          updateDisplay("Engine is on!   ", "Lights are on!  ");

        }

     

     

        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;

          updateDisplay("Rotors are on!  ", "Hang on tight!  ");

        }

     

     

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

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

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

          relay3_active = false;

          updateDisplay("Rotors shutdown,", "time to land    ");

        }

     

     

        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;

          updateDisplay("Fasten seatbelts", "Press button...");

        }

     

     

      }

     

    }

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

    This project is looking so cool that I almost want to buy myself a 4-relay board and a tricked out helicopter-car image

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

    Oops - I noticed I still have that bug in there that may cause actions to not start if the button was used to cancel them in the previous run.

     

    In the "pushing the button stops everything" block, add these 2 lines after "active=false;"...

          relay2_active = false;

          relay3_active = false;

    • Cancel
    • Vote Up 0 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