Total beginner, change state of outputrs in this code? (Resolved, thanks)

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have some code that allows my Arduino Uno and Ethernet Shield to change the state of 4 relays over my hard wired ethernet. I have this working fine but the four relay module I have does not allow jumpering to change the inputs from high to low when changing whether a relay energises or not. I did not write this code, it's something from A N Other.

 

So when I graphically press a relay button on screen from off to on the relay changes from energised to de-energised.

 

I want to reverse this in the code, so when a button shows OFF the relay is NOT energised, for each of four relays individually, and each relay module LED is OFF when each button shows OFF.

 

I hope I have made this fairly understandable? I am totally new to this and it probably shows :)

 

I also hope zipping the .ino file is acceptable, thanks for any help in modifying the code.

Attachment(s): 

Best regards, Chris Wilson

Last Edited: Thu. Nov 25, 2021 - 11:38 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thee are 252 files there, can you reduce the number, please!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

in the INO file, change this:

// checks if received HTTP request is switching on/off Relays
// also saves the state of the Relays
void SetRelays(void)
{
    // Relay 1 (pin 6)
    if (StrContains(HTTP_req, "Relay1=1")) {
        Relay_state[0] = 1;  // save Relay state
        digitalWrite(6, LOW);
    }
    else if (StrContains(HTTP_req, "Relay1=0")) {
        Relay_state[0] = 0;  // save Relay state
        digitalWrite(6, HIGH);
    }
    // Relay 2 (pin 7)
    if (StrContains(HTTP_req, "Relay2=1")) {
        Relay_state[1] = 1;  // save Relay state
        digitalWrite(7, LOW);
    }
    else if (StrContains(HTTP_req, "Relay2=0")) {
        Relay_state[1] = 0;  // save Relay state
        digitalWrite(7, HIGH);
    }
    // Relay 3 (pin 8)
    if (StrContains(HTTP_req, "Relay3=1")) {
        Relay_state[2] = 1;  // save Relay state
        digitalWrite(8, LOW);
    }
    else if (StrContains(HTTP_req, "Relay3=0")) {
        Relay_state[2] = 0;  // save Relay state
        digitalWrite(8, HIGH);
    }
    // Relay 4 (pin 9)
    if (StrContains(HTTP_req, "Relay4=1")) {
        Relay_state[3] = 1;  // save Relay state
        digitalWrite(9, LOW);
    }
    else if (StrContains(HTTP_req, "Relay4=0")) {
        Relay_state[3] = 0;  // save Relay state
        digitalWrite(9, HIGH);
    }
}

to this:

// checks if received HTTP request is switching on/off Relays
// also saves the state of the Relays
void SetRelays(void)
{
    // Relay 1 (pin 6)
    if (StrContains(HTTP_req, "Relay1=1")) {
        Relay_state[0] = 1;  // save Relay state
        digitalWrite(6, HIGH);
    }
    else if (StrContains(HTTP_req, "Relay1=0")) {
        Relay_state[0] = 0;  // save Relay state
        digitalWrite(6, LOW);
    }
    // Relay 2 (pin 7)
    if (StrContains(HTTP_req, "Relay2=1")) {
        Relay_state[1] = 1;  // save Relay state
        digitalWrite(7, HIGH);
    }
    else if (StrContains(HTTP_req, "Relay2=0")) {
        Relay_state[1] = 0;  // save Relay state
        digitalWrite(7, LOW);
    }
    // Relay 3 (pin 8)
    if (StrContains(HTTP_req, "Relay3=1")) {
        Relay_state[2] = 1;  // save Relay state
        digitalWrite(8, HIGH);
    }
    else if (StrContains(HTTP_req, "Relay3=0")) {
        Relay_state[2] = 0;  // save Relay state
        digitalWrite(8, LOW);
    }
    // Relay 4 (pin 9)
    if (StrContains(HTTP_req, "Relay4=1")) {
        Relay_state[3] = 1;  // save Relay state
        digitalWrite(9, HIGH);
    }
    else if (StrContains(HTTP_req, "Relay4=0")) {
        Relay_state[3] = 0;  // save Relay state
        digitalWrite(9, LOW);
    }
}

 

 

AND:

Change:

 // **********  button Relay states
   // Relay1
    cl.print("<Relay>");
    if (Relay_state[0]) {
        cl.print("on");
    }
    else {
        cl.print("off");
    }
    cl.println("</Relay>");
 
 // Relay2
    cl.print("<Relay>");
    if (Relay_state[1]) {
        cl.print("on");
    }
    else {
        cl.print("off");
    }
    cl.println("</Relay>");
    
	// Relay3
    cl.print("<Relay>");
    if (Relay_state[2]) {
        cl.print("on");
    }
    else {
        cl.print("off");
    }
    cl.println("</Relay>");
    // Relay4
    cl.print("<Relay>");
    if (Relay_state[3]) {
        cl.print("on");
    }
    else {
        cl.print("off");
    }
    cl.println("</Relay>");
    
    cl.print("</inputs>");
}

 

TO:

 // **********  button Relay states
   // Relay1
    cl.print("<Relay>");
    if (Relay_state[0]) {
        cl.print("off");
    }
    else {
        cl.print("on");
    }
    cl.println("</Relay>");
 
 // Relay2
    cl.print("<Relay>");
    if (Relay_state[1]) {
        cl.print("off");
    }
    else {
        cl.print("on");
    }
    cl.println("</Relay>");
    
	// Relay3
    cl.print("<Relay>");
    if (Relay_state[2]) {
        cl.print("off");
    }
    else {
        cl.print("on");
    }
    cl.println("</Relay>");
    // Relay4
    cl.print("<Relay>");
    if (Relay_state[3]) {
        cl.print("off");
    }
    else {
        cl.print("on");
    }
    cl.println("</Relay>");
    
    cl.print("</inputs>");
}

 

 

UNTESTED, but should do the trick

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

grohote wrote:

Thee are 252 files there, can you reduce the number, please!

 

I apologise, but I am not sure what you mean, I just zipped up the whole .ino file, is that not how i should post it?

Best regards, Chris Wilson

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

in the INO file, change this:

<SNIP>

 

 

UNTESTED, but should do the trick

 

JIm

 

Thanks a lot Jim, I loaded it but now all relay buttons show ON and I am unable to toggle their state. I may be able to get a relay module where I can jumper the inputs from ON with HIGH to OFF with HIGH, which might be easiest :) I appreciate your help.

Best regards, Chris Wilson

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MrBasil wrote:
but now all relay buttons show ON

Restore the original code for the second part....

you just needed to change the state of the relays, not how they display...

Flyover Jim

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

MrBasil wrote:

but now all relay buttons show ON

 

Restore the original code for the second part....

you just needed to change the state of the relays, not how they display...

Flyover Jim

 

Thanks, that's done the trick! I did get an error issue, something like stray \357 in program

 

Maybe because I used Windows Notepad to edit the .ino? I tried afresh using Notepad++ to edit the original,

and it loaded OK.

 

Many many thanks, I'll keep both .ino files so I can run the original on relay modules with reversible inputs.

 

For future reference, how should I upload .ino files correctly please?

Best regards, Chris Wilson

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MrBasil wrote:
For future reference, how should I upload .ino files correctly please?

 

I only saw one file in your ZIP upload so I do not know what GHROTE is upset about.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
I only saw one file in your ZIP upload so I do not know what GROHOTE is upset about

 

Sorry, one of "those" days... apologies for "252".
When I am opening .zip it actually pop-up WinRAR, and it resulted in 252 files.

I will change it in Firefox, from now on, the file .zip will be associated with Total Commander.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm back again, please forgive the noise :) If I power off the Arduino Uno (currently powered via its USB port to my PC) by unplugging the USB cable, when I power it on again by plugging the USB cable back in all relays go to ON, whatever their state at power down, which could be a problem.

 

Is this a facet of the code or a quirk of the Arduino itself? With Windows PC's with true serial ports I have had issues with Windows toggling the ports on boot up, a known and apparently insurmountable issue with Windows. Thanks, I'll be going to bed soon, so peace will reign ;)

 

EDIT: Scrub this question, I found the French comment:

 

/ init : je mets toutes les pins à l'état haut car transistor vers relais inverse l'état
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);

 

and changing HIGH to LOW now forces the Arduino to fire up with the relays de-enegised, which is what I want. Thanks.

Best regards, Chris Wilson

Last Edited: Wed. Nov 24, 2021 - 10:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

grohote wrote:

jgmdesign wrote:

I only saw one file in your ZIP upload so I do not know what GROHOTE is upset about

 

 

Sorry, one of "those" days... apologies for "252".
When I am opening .zip it actually pop-up WinRAR, and it resulted in 252 files.

I will change it in Firefox, from now on, the file .zip will be associated with Total Commander.

 

No worries, being utterly new to this and the accepted ways to post code I naturally assumed I'd made a faux pas, Please don't apologise, I too have "those days". Lots of them... :)

 

Best regards, Chris Wilson