Any arduino fanboys out there?

bigone5500

Well-Known Member
I've been tinkering with these things for a little while. I started off heavy into them but slacked off for a little while. But now I'm trying to get my head back into it and learn the programming language needed to write 'sketches'. The arduino IDE uses C/C++. There is so much you can do with these things and there are so many modules and boards out there that the possibilities are virtually endless. From robotics to home automation to wearables, the projects people have done is mind boggling.

Yesterday, I built an 8 push button module with pin headers to work with my arduino UNO and 4 relay board. I plan to use it in a project sometime and think it will work great. I needed a way to turn on a circuit and have it stay on for a certain time. Now I have it.

This stuff is so cheap to buy. I have bought 99% of mine from banggood. You can buy an original arduino for $30 or get a knockoff that uses the same exact MCU for about $3. I'll post photos of that project later.

Show off your projects! Don't forget to share the code too... :adoration:
 

bigone5500

Well-Known Member
Ok here it is. I call it pushbutton controlled relays...go figure. By the way, these arduinos can be powered by sub, 9v battery, car battery, any power source up to 12v.

20170813_210613.jpg
 

bigone5500

Well-Known Member
Here is the code...

Code:
/*   Pushbutton controlled relays. Modified by Jared Elliott.
 *   Control 4 relays via arduino using pushbuttons.
 *   There is a delay after pressing the pushbutton before it turns off.
 *   The delays can be adjusted to suit your needs.
 *   Additional relays can be added by copying the lines of code and adding the required setup code.
 *  
 *   This code was originally by Samir Tawfik on YouTube.
 */

#define RELAY_ON 0                                // The relays are active-LOW; on when LOW (0)
#define RELAY_OFF 1                               // and off when HIGH (1). These #defines just help keep track.

const int RelayPin1 = 8;                          // Pin numbers for the relays.
const int RelayPin2 = 9;                          // Constant integers keep the pin numbers from changing (read-only).
const int RelayPin3 = 10;
const int RelayPin4 = 11;
//const int RelayPin5 = *;                        // Change the * to correspond with the arduino pin
                                                  // that you wish to use for an additional relay. Uncomment to use.

const int ButtonPin1 = 2;                         // Pin numbers for the push buttons.
const int ButtonPin2 = 3;
const int ButtonPin3 = 4;
const int ButtonPin4 = 5;
//const int ButtonPin5 = *;                       // Change the * to correspond with the arduino pin
                                                  // that the additional button is connected to. Uncomment to use.

const int Duration1 = 500;                        // Number of milliseconds that the relays run after the buttons are pressed.
const int Duration2 = 500;
const int Duration3 = 500;
const int Duration4 = 500;
//const int Duration5 = *;                        // Change the * for Duration to a number of your choice. Uncomment to use.

// variables

byte State1 = RELAY_OFF;                          // Used to record whether the relays are on or off.
byte State2 = RELAY_OFF;                          // - default to HIGH/OFF -
byte State3 = RELAY_OFF;
byte State4 = RELAY_OFF;
//byte Stete5 = RELAY_OFF;                        // Uncomment to use for an additional relay.

unsigned long currentMillis = 0;                  // Stores the value of millis() in each iteration of loop().

unsigned long TimerMillis1 = 0;                   // Stores the times when the buttons were last pressed.
unsigned long TimerMillis2 = 0;
unsigned long TimerMillis3 = 0;
unsigned long TimerMillis4 = 0;
//unsigned long TimerMillis5 = 0;                 // Uncomment to use for an additional relay.

void setup()
{
// --Debug--
  Serial.begin(9600);
  Serial.println("Starting Relay Controller.ino");

  digitalWrite(RelayPin1, RELAY_OFF);             // Relays are active-LOW. Initialize relay pins
  digitalWrite(RelayPin2, RELAY_OFF);             // high so the relays are inactive at startup/reset.
  digitalWrite(RelayPin3, RELAY_OFF);
  digitalWrite(RelayPin4, RELAY_OFF);
//digitalWrite(RelayPin5, RELAY_OFF);             // Uncomment for an additional relay.

  pinMode(RelayPin1, OUTPUT);                     // Then set the pins as outputs.
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
//pinMode(RelayPin5, OUTPUT);                     // Uncomment for an additional relay.

  pinMode(ButtonPin1, INPUT_PULLUP);              // Set the float sensor pins as inputs and use the internal pullup resistors.
  pinMode(ButtonPin2, INPUT_PULLUP);
  pinMode(ButtonPin3, INPUT_PULLUP);
  pinMode(ButtonPin4, INPUT_PULLUP);
//pinMode(ButtonPin5, INPUT_PULLUP);              // Uncomment for an additional relay.

}

void loop()
{
// Get the current clock count

  currentMillis = millis() + Duration1;           // We add the countdown timer duration to
                                                  // the clock to prevent the relay from
                                                  // running at boot time, whele the clock
                                                  // counter is still below the timer value.
// Call the cunctions that do the work

  readButtonPin1();                               // Check each button and decide whether
  readButtonPin2();                               // to start the relay and countdown timer.
  readButtonPin3();
  readButtonPin4();
  triggerRelay();                                 // Actually toggles the relay pins based on
                                                  // the data from the above functions.
}

// ---Worker functions---

void readButtonPin1()                             // Repeat comments for each 'readButtonPin' iteration.
{
  if (digitalRead(ButtonPin1) == LOW)             // If the button is tripped (pulled low)...
  {
    State1 = RELAY_ON;                            // ...then trigger the relay pin
    TimerMillis1 = currentMillis;                 // and set the relay countdown timer to the current clock time.
  }

  if (currentMillis - TimerMillis1 >= Duration1)  //If the countdown timer has expired...
  {
    State1 = RELAY_OFF;                           // ...then turn off the relay pin
    TimerMillis1 = 0;                             // and reset the timer to 0 for the next trigger.
  }
}

void readButtonPin2()
{
  if (digitalRead(ButtonPin2) == LOW)
  {
    State2 = RELAY_ON;
    TimerMillis2 = currentMillis;
  }

  if (currentMillis - TimerMillis2 >= Duration2)
  {
    State2 = RELAY_OFF;
    TimerMillis2 = 0;
  }
}

void readButtonPin3()
{
  if (digitalRead(ButtonPin3) == LOW)
  {
    State3 = RELAY_ON;
    TimerMillis3= currentMillis;
  }

  if (currentMillis - TimerMillis3 >= Duration3)
  {
    State3 = RELAY_OFF;
    TimerMillis3 = 0;
  }
}

void readButtonPin4()
{
  if (digitalRead(ButtonPin4) == LOW)
  {
    State4 = RELAY_ON;
    TimerMillis4 = currentMillis;
  }

  if (currentMillis - TimerMillis4 >= Duration4)
  {
    State4 = RELAY_OFF;
    TimerMillis4 = 0;
  }

 // Uncomment the following block of code for the additional relay
 // and change the number to correspond with your setup.
 // When uncommenting the previous lines to add an additional relay, simply change the * to 5.
 /* void readButtonPin*()
{
  if (digitalRead(ButtonPin*) == LOW)
  {
    State* = RELAY_ON;
    TimerMillis* = currentMillis;
  }

  if (currentMillis - TimerMillis* >= Duration*)
  {
    State* = RELAY_OFF;
    TimerMillis* = 0;
  }
*/  
}

void triggerRelay()
{
  digitalWrite(RelayPin1, State1);                // Toggle the relay pins on and off based on
  digitalWrite(RelayPin2, State2);                // the State from the readButton functions.
  digitalWrite(RelayPin3, State3);
  digitalWrite(RelayPin4, State4);
//digitalWrite(RelayPin5, Stete5);                // Uncomment for an additional relay.
                                                  // Adjust the number as needed to correspond with the setup.
}
 

Tony

Staff member
I was looking for one of these a while back but no one had one to sell. Still looking. Pretty sure I would spend too much time playing with it anyway lol.
 

bigone5500

Well-Known Member
So I decided to try to add a couple more relays to this project. My theory that by adding more lines and connecting the buttons and relay pins to the correct pins on the arduino, that they would operate as intended. This is proving to be wrong. I am in the process of figuring out what is going on. I am getting an error when compiling the code. For some reason, line 96, readButtonPin5();, results in: 'readButtonPin5' was not declared in this scope. However, it is declared. Line 25 sets ButtonPin5 to input pin 6 on the board and line 77 sets pinMode of ButtonPin5 to use internal pullup resistors.

Here is what I'm dealing with:
Code:
/*   Pushbutton controlled relays. Modified by Jared Elliott.
 *   Control 4 relays via arduino using pushbuttons.
 *   There is a delay after pressing the pushbutton before it turns off.
 *   The delays can be adjusted to suit your needs.
 *   Additional relays can be added by copying the
 *   lines of code and adding the required setup code.
 *   
 *   This code was originally by Samir Tawfik on YouTube.
 */

#define RELAY_ON 0                                // The relays are active-LOW; on when LOW (0)
#define RELAY_OFF 1                               // and off when HIGH (1). These #defines just help keep track.

const int RelayPin1 = 8;                          // Pin numbers for the relays.
const int RelayPin2 = 9;                          // Constant integers keep the pin numbers from changing (read-only).
const int RelayPin3 = 10;
const int RelayPin4 = 11;
const int RelayPin5 = A0;
const int RelayPin6 = A1;

const int ButtonPin1 = 2;                         // Pin numbers for the push buttons.
const int ButtonPin2 = 3;
const int ButtonPin3 = 4;
const int ButtonPin4 = 5;
const int ButtonPin5 = 6;
const int ButtonPin6 = 7;

const int Duration1 = 1000;                        // Number of milliseconds that the relays
const int Duration2 = 2000;                        // run after the buttons are pressed.
const int Duration3 = 3000;
const int Duration4 = 4000;
const int Duration5 = 5000;
const int Duration6 = 6000;

// variables

byte State1 = RELAY_OFF;                          // Used to record whether the relays are on or off.
byte State2 = RELAY_OFF;                          // - default to HIGH/OFF -
byte State3 = RELAY_OFF;
byte State4 = RELAY_OFF;
byte Stete5 = RELAY_OFF;                        // Uncomment to use for an additional relay.
byte Stete6 = RELAY_OFF;

unsigned long currentMillis = 0;                  // Stores the value of millis() in each iteration of loop().

unsigned long TimerMillis1 = 0;                   // Stores the times when the buttons were last pressed.
unsigned long TimerMillis2 = 0;
unsigned long TimerMillis3 = 0;
unsigned long TimerMillis4 = 0;
unsigned long TimerMillis5 = 0;                 // Uncomment to use for an additional relay.
unsigned long TimerMillis6 = 0;

void setup()
{
// --Debug--
  Serial.begin(9600);
  Serial.println("Starting Relay Controller.ino");

  digitalWrite(RelayPin1, RELAY_OFF);             // Relays are active-LOW. Initialize relay pins
  digitalWrite(RelayPin2, RELAY_OFF);             // high so the relays are inactive at startup/reset.
  digitalWrite(RelayPin3, RELAY_OFF);
  digitalWrite(RelayPin4, RELAY_OFF);
  digitalWrite(RelayPin5, RELAY_OFF);             // Uncomment for an additional relay.
  digitalWrite(RelayPin6, RELAY_OFF);

  pinMode(RelayPin1, OUTPUT);                     // Then set the pins as outputs.
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(RelayPin5, OUTPUT);                     // Uncomment for an additional relay.
  pinMode(RelayPin6, OUTPUT);

  pinMode(ButtonPin1, INPUT_PULLUP);              // Set the float sensor pins as inputs
  pinMode(ButtonPin2, INPUT_PULLUP);              // and use the internal pullup resistors.
  pinMode(ButtonPin3, INPUT_PULLUP);
  pinMode(ButtonPin4, INPUT_PULLUP);
  pinMode(ButtonPin5, INPUT_PULLUP);
  pinMode(ButtonPin6, INPUT_PULLUP);

}

void loop()
{
// Get the current clock count

  currentMillis = millis() + Duration1;           // We add the countdown timer duration to
                                                  // the clock to prevent the relay from
                                                  // running at boot time, whele the clock
                                                  // counter is still below the timer value.
// Call the functions that do the work

  readButtonPin1();                               // Check each button and decide whether
  readButtonPin2();                               // to start the relay and countdown timer.
  readButtonPin3();
  readButtonPin4();
  readButtonPin5();
  readButtonPin6();
  triggerRelay();                                 // Actually toggles the relay pins based on
                                                  // the data from the above functions.
}

// ---Worker functions---

void readButtonPin1()                             // Repeat comments for each 'readButtonPin' iteration.
{
  if (digitalRead(ButtonPin1) == LOW)             // If the button is tripped (pulled low)...
  {
    State1 = RELAY_ON;                            // ...then trigger the relay pin
    TimerMillis1 = currentMillis;                 // and set the relay countdown timer to the current clock time.
  }

  if (currentMillis - TimerMillis1 >= Duration1)  //If the countdown timer has expired...
  {
    State1 = RELAY_OFF;                           // ...then turn off the relay pin
    TimerMillis1 = 0;                             // and reset the timer to 0 for the next trigger.
  }
}

void readButtonPin2()
{
  if (digitalRead(ButtonPin2) == LOW)
  {
    State2 = RELAY_ON;
    TimerMillis2 = currentMillis;
  }

  if (currentMillis - TimerMillis2 >= Duration2)
  {
    State2 = RELAY_OFF;
    TimerMillis2 = 0;
  }
}

void readButtonPin3()
{
  if (digitalRead(ButtonPin3) == LOW)
  {
    State3 = RELAY_ON;
    TimerMillis3= currentMillis;
  }

  if (currentMillis - TimerMillis3 >= Duration3)
  {
    State3 = RELAY_OFF;
    TimerMillis3 = 0;
  }
}

void readButtonPin4()
{
  if (digitalRead(ButtonPin4) == LOW)
  {
    State4 = RELAY_ON;
    TimerMillis4 = currentMillis;
  }

  if (currentMillis - TimerMillis4 >= Duration4)
  {
    State4 = RELAY_OFF;
    TimerMillis4 = 0;
  }

void readButtonPin5()
{
  if (digitalRead(ButtonPin5) == LOW)
  {
    State5 = RELAY_ON;
    TimerMillis5 = currentMillis;
  }

  if (currentMillis - TimerMillis5 >= Duration5)
  {
    State5 = RELAY_OFF;
    TimerMillis5 = 0;
  }

void readButtonPin6()
{
  if (digitalRead(ButtonPin6) == LOW)
  {
    State6 = RELAY_ON;
    TimerMillis6 = currentMillis;
  }

  if (currentMillis - TimerMillis6 >= Duration6)
  {
    State6 = RELAY_OFF;
    TimerMillis6 = 0;
  }
}

void triggerRelay()
{
  digitalWrite(RelayPin1, State1);                // Toggle the relay pins on and off based on
  digitalWrite(RelayPin2, State2);                // the State from the readButton functions.
  digitalWrite(RelayPin3, State3);
  digitalWrite(RelayPin4, State4);
  digitalWrite(RelayPin5, Stete5);
  digitalWrite(RelayPin5, Stete6);
}
 

EyeStation

Well-Known Member
byte Stete5 = RELAY_OFF; // Uncomment to use for an additional relay.
byte Stete6 = RELAY_OFF;

Should this not be
byte State5 = RELAY_OFF:
you have the word "Stete"
 

bigone5500

Well-Known Member
byte Stete5 = RELAY_OFF; // Uncomment to use for an additional relay.
byte Stete6 = RELAY_OFF;

Should this not be
byte State5 = RELAY_OFF:
you have the word "Stete"
Thank you sir. I have not wanted to slap myself so hard before. I need to get a new prescription...really...

I'm just ignorant...
 

EyeStation

Well-Known Member
It looks to have originated as just a comment line in your original code posted....
byte State4 = RELAY_OFF;
//byte Stete5 = RELAY_OFF; // Uncomment to use for an additional relay.
 

bigone5500

Well-Known Member
Yes it was originally just a commented line in the original code with a slight typo. However, after correcting the spelling, I am still getting the same error whilst compiling. I will continue to investigate this and try to get some constructive input from someone on the electronics forum I am on. Hopefully not some smart*$$ like the last guy who replied.
 

EyeStation

Well-Known Member
i see it again in the final lines of code:
digitalWrite(RelayPin5, Stete5);
digitalWrite(RelayPin5, Stete6);
 

bigone5500

Well-Known Member
Alright. The sketch is now working. I got help from someone on the arduino forums and it was due to a bracketing issue...and soemone who can't sepll...

But here is the final code and I have verified it is working.

Code:
/*   Pushbutton controlled relays. Modified by Jared Elliott.
 *   Control 4 relays via arduino using pushbuttons.
 *   There is a delay after pressing the pushbutton before it turns off.
 *   The delays can be adjusted to suit your needs.
 *   Additional relays can be added by copying the
 *   lines of code and adding the required setup code.
 *  
 *   This code was originally by Samir Tawfik on YouTube.
 */

#define RELAY_ON 0                                // The relays are active-LOW; on when LOW (0)
#define RELAY_OFF 1                               // and off when HIGH (1). These #defines just help keep track.

const int RelayPin1 = 8;                          // Pin numbers for the relays.
const int RelayPin2 = 9;                          // Constant integers keep the pin numbers from changing (read-only).
const int RelayPin3 = 10;
const int RelayPin4 = 11;
const int RelayPin5 = A0;
const int RelayPin6 = A1;

const int ButtonPin1 = 2;                         // Pin numbers for the push buttons.
const int ButtonPin2 = 3;
const int ButtonPin3 = 4;
const int ButtonPin4 = 5;
const int ButtonPin5 = 6;
const int ButtonPin6 = 7;

const int Duration1 = 1000;                        // Number of milliseconds that the relays
const int Duration2 = 2000;                        // run after the buttons are pressed.
const int Duration3 = 3000;
const int Duration4 = 4000;
const int Duration5 = 5000;
const int Duration6 = 6000;

// variables

byte State1 = RELAY_OFF;                          // Used to record whether the relays are on or off.
byte State2 = RELAY_OFF;                          // - default to HIGH/OFF -
byte State3 = RELAY_OFF;
byte State4 = RELAY_OFF;
byte State5 = RELAY_OFF;
byte State6 = RELAY_OFF;

unsigned long currentMillis = 0;                  // Stores the value of millis() in each iteration of loop().

unsigned long TimerMillis1 = 0;                   // Stores the times when the buttons were last pressed.
unsigned long TimerMillis2 = 0;
unsigned long TimerMillis3 = 0;
unsigned long TimerMillis4 = 0;
unsigned long TimerMillis5 = 0;
unsigned long TimerMillis6 = 0;

void setup()
{
// --Debug--
  Serial.begin(9600);
  Serial.println("Starting Relay Controller.ino");

  digitalWrite(RelayPin1, RELAY_OFF);             // Relays are active-LOW. Initialize relay pins
  digitalWrite(RelayPin2, RELAY_OFF);             // high so the relays are inactive at startup/reset.
  digitalWrite(RelayPin3, RELAY_OFF);
  digitalWrite(RelayPin4, RELAY_OFF);
  digitalWrite(RelayPin5, RELAY_OFF);
  digitalWrite(RelayPin6, RELAY_OFF);

  pinMode(RelayPin1, OUTPUT);                     // Then set the pins as outputs.
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(RelayPin5, OUTPUT);
  pinMode(RelayPin6, OUTPUT);

  pinMode(ButtonPin1, INPUT_PULLUP);              // Set the float sensor pins as inputs
  pinMode(ButtonPin2, INPUT_PULLUP);              // and use the internal pullup resistors.
  pinMode(ButtonPin3, INPUT_PULLUP);
  pinMode(ButtonPin4, INPUT_PULLUP);
  pinMode(ButtonPin5, INPUT_PULLUP);
  pinMode(ButtonPin6, INPUT_PULLUP);
}

void loop()
{
// Get the current clock count

  currentMillis = millis() + Duration1;           // We add the countdown timer duration to
                                                  // the clock to prevent the relay from
                                                  // running at boot time, whele the clock
                                                  // counter is still below the timer value.
// Call the functions that do the work

  readButtonPin1();                               // Check each button and decide whether
  readButtonPin2();                               // to start the relay and countdown timer.
  readButtonPin3();
  readButtonPin4();
  readButtonPin5();
  readButtonPin6();
  triggerRelay();                                 // Actually toggles the relay pins based on
                                                  // the data from the above functions.
}

// ---Worker functions---

void readButtonPin1()                             // Repeat comments for each 'readButtonPin' iteration.
{
  if (digitalRead(ButtonPin1) == LOW)             // If the button is tripped (pulled low)...
  {
    State1 = RELAY_ON;                            // ...then trigger the relay pin
    TimerMillis1 = currentMillis;                 // and set the relay countdown timer to the current clock time.
  }

  if (currentMillis - TimerMillis1 >= Duration1)  //If the countdown timer has expired...
  {
    State1 = RELAY_OFF;                           // ...then turn off the relay pin
    TimerMillis1 = 0;                             // and reset the timer to 0 for the next trigger.
  }
}

void readButtonPin2()
{
  if (digitalRead(ButtonPin2) == LOW)
  {
    State2 = RELAY_ON;
    TimerMillis2 = currentMillis;
  }

  if (currentMillis - TimerMillis2 >= Duration2)
  {
    State2 = RELAY_OFF;
    TimerMillis2 = 0;
  }
}

void readButtonPin3()
{
  if (digitalRead(ButtonPin3) == LOW)
  {
    State3 = RELAY_ON;
    TimerMillis3= currentMillis;
  }

  if (currentMillis - TimerMillis3 >= Duration3)
  {
    State3 = RELAY_OFF;
    TimerMillis3 = 0;
  }
}

void readButtonPin4()
{
  if (digitalRead(ButtonPin4) == LOW)
  {
    State4 = RELAY_ON;
    TimerMillis4 = currentMillis;
  }

  if (currentMillis - TimerMillis4 >= Duration4)
  {
    State4 = RELAY_OFF;
    TimerMillis4 = 0;
  }
}

void readButtonPin5()
{
  if (digitalRead(ButtonPin5) == LOW)
  {
    State5 = RELAY_ON;
    TimerMillis5 = currentMillis;
  }

  if (currentMillis - TimerMillis5 >= Duration5)
  {
    State5 = RELAY_OFF;
    TimerMillis5 = 0;
  }
}

void readButtonPin6()
{
  if (digitalRead(ButtonPin6) == LOW)
  {
    State6 = RELAY_ON;
    TimerMillis6 = currentMillis;
  }

  if (currentMillis - TimerMillis6 >= Duration6)
  {
    State6 = RELAY_OFF;
    TimerMillis6 = 0;
  }
}

void triggerRelay()
{
  digitalWrite(RelayPin1, State1);                // Toggle the relay pins on and off based on
  digitalWrite(RelayPin2, State2);                // the State from the readButton functions.
  digitalWrite(RelayPin3, State3);
  digitalWrite(RelayPin4, State4);
  digitalWrite(RelayPin5, State5);
  digitalWrite(RelayPin6, State6);
}
 

bigone5500

Well-Known Member
I finally was able to get a clock to work. I had been to the point of throwing this thing out the window. But I'm happy its working.

20170830_210711.jpg
 
Top Bottom