Using Relays with an arduino

Hey everyone,

I’m relatively new to arduino coding, and had some problems with coding.

I’m trying to change some existing code to power a small relay instead of an LED.

Instead of outputting 1 second of power to an LED, I want case -1 to output power to pin 3 until case -2 is triggered, regardless of other input. If possible, I would also like case -3 to output power to pin 5 until case -3 is triggered again. The entire thing should run on a loop so that each trigger can be repeated multiple times.

The output to the pins is not directly powering a relay, it will be running through a transistor first to trigger the relay on with another power source.

The code below is the sections I am referring to.

#define LED1 3
#define LED2 4
#define LED3 5
(skips some lines for space)
if((tA <= tB) && tA <= tC)
return -3 ;// FORWRD
if((tB <= tC) && tB <= tA)
return -2 ;// LEFT
if((tC <= tB) && tC <= tA)
return -1 ;//RIGHT
//Etc
return 1;

}

void DoCommand(int command)
{
switch (command)
{
case 0: Serial.println(“Stopped”);delay(500);break;
case -1: Serial.println("Turn RIGHT ");digitalWrite(LED1,HIGH); delay(1000);break;
case -2: Serial.println("Turn LEFT ");digitalWrite(LED2,HIGH); delay(1000);break;
case -3: Serial.println("GO FORWARD “);digitalWrite(LED3,HIGH); delay(1000);break;
case -4: Serial.println(” GO BACK "); delay(1000);break;
default : Serial.println(“NO Command detected”);break;
}
ResetCommand();
}
void ResetCommand()
{
for(int i = 3;i<=5;i++)
{
digitalWrite(i,LOW);//ALL LEDs
}
tA = 0;//Reset time A
tB = 0;//Reset time B
tC = 0;//Reset time C
}

Folks around here would prefer that you place your code inside code tags (refer to the sticky "how to use this forum" at the top of each forum.

Also, you'll be better off sharing your entire code. Sometimes the issue isn't were one might think it should be.

Feel free to edit your post above.

You should also probably mention what the problem is that you want help with. That's like THE single biggest most important piece of information you need in order to give someone help.

A couple tips, make your code readable by using better variable names. learn about enums, don't use numbers when names will do. I'm not sure what you're doing with the tA..tC - but once your goal should be to make your code easy to understand. Putting multiple lines on one line, is going to be tough to see when you miss a break or something else. Usually I would put {} around all case groups, but I followed your lead on this since it's not mandatory. Use functions where repetition occurs or where it makes your code more readable.

enum type_movement  { tmNoCommand, tmStop, tmRight,   tmLeft, tmForward, tmBackward };
int delayAmount[] = { 0,  500, 1000, 1000,  1000,  1000 };   // delays for above movements
// usually I would align the numbers under the types to show they are related, but this editor is adding spaces.

                                                                            // you could create another string array for the text ...

type_movement readInputFromUser()
{
    return tmStop;      // get your input from somewhere - must be in the type_movement array
}

#define LED1 3
#define LED2 4
#define LED3 5

int main()
{
    type_movement direction = tmNoCommand;

    direction = readInputFromUser();
    // I left the switch - but it could be done with a string array
    switch (direction)
    {
    case tmStop:
        printf("Stopped");  // Serial.println
        break;
    case tmRight:
        printf("Turn RIGHT ");
        digitalWrite(LED1, HIGH);
        break;
    case tmLeft:
        printf("Turn LEFT ");
        digitalWrite(LED2, HIGH);
        break;
    case tmForward:
        printf("GO FORWARD ");
        digitalWrite(LED3, HIGH);
        break;
    case tmBackward:
        printf("GO BACK ");
        break;
    case tmNoCommand:
        printf("NO Command detected");
        break;
    default:
        printf("Developer bug, you added a new movement type without adding a switch");
        direction = NoCommand;  // this keeps below delay from failing on unknown value
        break;
    }
    delay(delayAmount[direction]);
    return 0;
}

The output to the pins is not directly powering a relay, it will be running through a transistor first to trigger the relay on with another power source.

I'd be inclined to go for one of those ready made relay module thingies with the transistor built in, and often opto-isolation.

We live and learn: never occurred to me to use a -ve value in a switch/case.

kenwood120s: I'd be inclined to go for one of those ready made relay module thingies with the transistor built in, and often opto-isolation.

We live and learn: never occurred to me to use a -ve value in a switch/case.

I did the transistor route at first. Once I saw the circuit on my breadboard, I ripped it out and ordered a .40 cent digital logic level converter. Which gave me four 5v I/O on one side and 3.3v I/O on the other. The operative word here is I/O - both Input and Output, not just Output.

A Node MCU powered with 5v, can provide both the 5v and 3.3v.

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

  • What did the code do originally?
  • Did the code work originally?
  • What do you want it to do?
  • What is it doing?

Can you tell us your electronics, programming, Arduino, hardware experience?

Thanks.. Tom.. :)