Need help with Realy code

#1

I am using a Arduino nano clone and it is using the old bootloader. I am making a relay center to control various things on my new laser cutter. I have a 4 bank relay module and it works fine. I wired it up as it shows and I can open the serial monitor and run through the tests for the buttons and the relay and it all works. But when I hit reset and close the SM window it refuses to work. Now I am planing on using this in the protected mode . So I am going to run a separate power and ground to the relay to isolate the nano. But as of right now I have it wire as it has showed me. I am going to test it to see if there is any output to the relay when the button is pushed . Here is the code that was generated.
// Include Libraries
#include “Arduino.h”
#include “Button.h”

// Pin Definitions
#define PUSHBUTTON_1_PIN_2 2
#define PUSHBUTTON_2_PIN_2 3
#define PUSHBUTTON_3_PIN_2 4
#define PUSHBUTTON_4_PIN_2 5
#define RELAYMODULE4CH_PIN_IN1 6
#define RELAYMODULE4CH_PIN_IN2 7
#define RELAYMODULE4CH_PIN_IN4 9
#define RELAYMODULE4CH_PIN_IN3 8

// Global variables and defines
//define an array for the 4ch relay module pins
int RelayModule4chPins[] = { RELAYMODULE4CH_PIN_IN1, RELAYMODULE4CH_PIN_IN2, RELAYMODULE4CH_PIN_IN3, RELAYMODULE4CH_PIN_IN4 };
// object initialization
Button pushButton_1(PUSHBUTTON_1_PIN_2);
Button pushButton_2(PUSHBUTTON_2_PIN_2);
Button pushButton_3(PUSHBUTTON_3_PIN_2);
Button pushButton_4(PUSHBUTTON_4_PIN_2);

// define vars for testing menu
const int timeout = 10000; //define timeout of 10 sec
char menuOption = 0;
long time0;

// Setup the essentials for your circuit to work. It runs first every time your circuit is powered with electricity.
void setup()
{
// Setup Serial which is useful for debugging
// Use the Serial Monitor to view printed messages
Serial.begin(9600);
while (!Serial) ; // wait for serial port to connect. Needed for native USB
Serial.println(“start”);

pushButton_1.init();
pushButton_2.init();
pushButton_3.init();
pushButton_4.init();
pinMode(RELAYMODULE4CH_PIN_IN1, OUTPUT);
pinMode(RELAYMODULE4CH_PIN_IN2, OUTPUT);
pinMode(RELAYMODULE4CH_PIN_IN3, OUTPUT);
pinMode(RELAYMODULE4CH_PIN_IN4, OUTPUT);
menuOption = menu();

}

// Main logic of your circuit. It defines the interaction between the components you selected. After setup, it runs over and over again, in an eternal loop.
void loop()
{

if(menuOption == '1') {
// Mini Pushbutton Switch #1 - Test Code
//Read pushbutton state. 
//if button is pressed function will return HIGH (1). if not function will return LOW (0). 
//for debounce funtionality try also pushButton_1.onPress(), .onRelease() and .onChange().
//if debounce is not working properly try changing 'debounceDelay' variable in Button.h
bool pushButton_1Val = pushButton_1.read();
Serial.print(F("Val: ")); Serial.println(pushButton_1Val);

}
else if(menuOption == '2') {
// Mini Pushbutton Switch #2 - Test Code
//Read pushbutton state. 
//if button is pressed function will return HIGH (1). if not function will return LOW (0). 
//for debounce funtionality try also pushButton_2.onPress(), .onRelease() and .onChange().
//if debounce is not working properly try changing 'debounceDelay' variable in Button.h
bool pushButton_2Val = pushButton_2.read();
Serial.print(F("Val: ")); Serial.println(pushButton_2Val);

}
else if(menuOption == '3') {
// Mini Pushbutton Switch #3 - Test Code
//Read pushbutton state. 
//if button is pressed function will return HIGH (1). if not function will return LOW (0). 
//for debounce funtionality try also pushButton_3.onPress(), .onRelease() and .onChange().
//if debounce is not working properly try changing 'debounceDelay' variable in Button.h
bool pushButton_3Val = pushButton_3.read();
Serial.print(F("Val: ")); Serial.println(pushButton_3Val);

}
else if(menuOption == '4') {
// Mini Pushbutton Switch #4 - Test Code
//Read pushbutton state. 
//if button is pressed function will return HIGH (1). if not function will return LOW (0). 
//for debounce funtionality try also pushButton_4.onPress(), .onRelease() and .onChange().
//if debounce is not working properly try changing 'debounceDelay' variable in Button.h
bool pushButton_4Val = pushButton_4.read();
Serial.print(F("Val: ")); Serial.println(pushButton_4Val);

}
else if(menuOption == '5') {
// Relay Module 4-Ch - Test Code
//This loop will turn on and off each relay in the array for 0.5 sec
for (int i = 0; i < 4; i++) { 
digitalWrite(RelayModule4chPins[i],HIGH);
delay(500);
digitalWrite(RelayModule4chPins[i],LOW);
delay(500);
}
}

if (millis() - time0 > timeout)
{
    menuOption = menu();
}

}

// Menu function for selecting the components to be tested
// Follow serial monitor for instrcutions
char menu()
{

Serial.println(F("\nWhich component would you like to test?"));
Serial.println(F("(1) Mini Pushbutton Switch #1"));
Serial.println(F("(2) Mini Pushbutton Switch #2"));
Serial.println(F("(3) Mini Pushbutton Switch #3"));
Serial.println(F("(4) Mini Pushbutton Switch #4"));
Serial.println(F("(5) Relay Module 4-Ch"));
Serial.println(F("(menu) send anything else or press on board reset button\n"));
while (!Serial.available());

// Read data from serial monitor if received
while (Serial.available()) 
{
    char c = Serial.read();
    if (isAlphaNumeric(c)) 
    {   
        
        if(c == '1') 
			Serial.println(F("Now Testing Mini Pushbutton Switch #1"));
		else if(c == '2') 
			Serial.println(F("Now Testing Mini Pushbutton Switch #2"));
		else if(c == '3') 
			Serial.println(F("Now Testing Mini Pushbutton Switch #3"));
		else if(c == '4') 
			Serial.println(F("Now Testing Mini Pushbutton Switch #4"));
		else if(c == '5') 
			Serial.println(F("Now Testing Relay Module 4-Ch"));
        else
        {
            Serial.println(F("illegal input!"));
            return 0;
        }
        time0 = millis();
        return c;
    }
}

}

0 Likes

#2

Could you include the URL of the circuit Richard? That way we can all see which components are on the circuit and we can get the full source code to build locally as well.

0 Likes

#3

https://www.circuito.io/app?components=97,97,97,97,514,11022,442979
This is what I had used. It functions fine in “testing mode” . But out of testing mode it will not function. I had tested inputs and they work . Just not sure why it will not work outside testing.

1 Like

#4

Could you post the code you have now (out of testing). Or are you using the code from your first post?

0 Likes