Go Down

Topic: How to program a simple switch? (Read 2283 times) previous topic - next topic

lastchancename

I think the OP wants to pass some 'voltage' through the Arduino - using the pins as simple 'contacts' to his hoped-for Arduino 'relay'.

If that's the case - he's completely on the wrong track - and either needs an external relay & driver, or to approach the problem in a different way.

His swx (switch?) pins are assigned as OUTPUTS - so that's not a good start.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

ChrisTenone

This is the second time this weekend someone wanted to use a switch to connect one pin to the other. The other instance was on a steering wheel.
What, I need to say something else too?

larryd

Maybe we are missing something, if we could market the solution, we could make a million.
No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

JimboZA

#18
Apr 11, 2016, 07:23 am Last Edit: Apr 11, 2016, 07:23 am by JimboZA
If you want pin 5 to be in the same condition as pin 6, and you already have the wherewithal to control pin 6, why not just control pin 5 the same way.

But I'm probably missing something here, and as someone suggested already I suspect an XY problem here.

This is the second time this weekend someone wanted to use a switch to connect one pin to the other. The other instance was on a steering wheel.
..... and that didn't end well ;)


Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

DARCCLLC

As aarg stated "You do realize that the Arduino has nothing like internal switches or analog multiplexers that you can assign to pins, right?"

I have worked with PLC's and did not realize that Arduino does not have internal switches.

Thanks for your time and patience.

I'll be looking for a STSP relay.

DARCCLLC


MalharD

This is the second time this weekend someone wanted to use a switch to connect one pin to the other. The other instance was on a steering wheel.
And that guy posted 5 times...

-Malhar
Murphy always gets me ...

outsider

#22
Apr 11, 2016, 04:47 pm Last Edit: Apr 11, 2016, 04:48 pm by outsider
If the load that you're switching is small (less than 500mA) and non reactive, you may want to consider a reed relay. The coil draws only about 15mA and can be driven directly from an Arduino output pin, larger relays need an external power supply and driver like a transistor.
Check back in when you get your relay.
http://www.amazon.com/Opto-22-G4ODC5R-Normally-Isolation/dp/B00597VDBY/ref=sr_1_4?s=industrial&ie=UTF8&qid=1460384117&sr=1-4&keywords=reed+relay+module

http://www.opto22.com/site/pr_details.aspx?cid=4&item=ODC5R


DARCCLLC

Code: [Select]
                         int LED = 13;
const int rs12 = 12; //Reed Switch
const int sw9 = 9; //Tilt Switch
const int rb6 = 6; //Reset Button


int swV = 0; //Tilt Switch On or Off
int rbV = 0; //Reset Button On or Off
int rs12on = 0; //Reed Switch On or Off

void setup()
{                           pinMode(LED, OUTPUT);
 pinMode(rs12, OUTPUT); //Pin 12 5vdc to Reed Switch
 pinMode(sw9, OUTPUT);  //Pin 9 Tilt Switch Output
 pinMode(swV, OUTPUT);  //Variable Switch Value
 pinMode(rb6, OUTPUT);  //Pin 6 Reset Button Output
 pinMode(rbV, OUTPUT);  //Variable Button Value

}
 
void loop()
{                         digitalWrite(LED, LOW); //Keep LED Off

  rbV = digitalRead(rb6); //Makes rbV = Pin 6 value
  swV = digitalRead(sw9);  //Makes swV = Pin 9 value
  while (swV == HIGH)      //#1 Loop until this value becomes false
 {
  if (rbV == HIGH)         //If Reset Button is pressed opens Reed Switch
   {
  rbV = digitalRead(rb6); //Makes rbV = Pin 6 value
  swV = digitalRead(sw9);  //Makes swV = Pin 9 value
  digitalWrite(rs12, LOW);  //Opens Reed Switch (rs12) Which dis-connects Solar LED to Solar Cell/Battery 1.2VDC
   } //If End

  swV = digitalRead(sw9);  //Makes swV = Pin 9 value
  rbV = digitalRead(rb6); //Makes rbV = Pin 6 value 
  while (rbV == LOW)       //#2 Loop until this value becomes false
    {
  rbV = digitalRead(rb6); //Makes rbV = Pin 6 value
  swV = digitalRead(sw9);  //Makes swV = Pin 9 value
  rs12on = digitalRead(rs12); //Makes led12on = Pin 12 value
  digitalWrite(rs12, HIGH);    //Closes Reed Switch (rs12) Which connects Solar LED to Solar Cell/Battery 1.2VDC
    }  //While #2 End
 } //While #1 End
  digitalWrite(rs12, LOW);  //Opens Reed Switch (rs12) Which dis-connects Solar LED to Solar Cell/Battery 1.2VDC
} //Void End






Components:
5VDC Reed Switch, Tilt switch, Push button, 9V battery, 1.2V Solar Light (revised wiring) & Mini Prov 5V

Thanks for your help, I guess I eventually would have figured out the Arduino does not have internal switch capability.

Razz

outsider

Could you please post a drawing of your circuit and explain in detail what your project is supposed to do?
I'm still in the dark. :smiley-confuse:

JimboZA

I eventually would have figured out the Arduino does not have internal switch capability.
But what on any of the product pages (like this one for Uno) would have given you the impression it did?

Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

ChrisTenone


...
 I eventually would have figured out the Arduino does not have internal switch capability.

Razz
Of course it does! Transistors in a microprocessor are switches, and it's lousy with transistors in there. But just leave them alone - they know what to do. You want to use an external switch to send a signal to the Arduino that a button has been pressed (or a reed switch, tilt switch, etc.)

When you hook two wires (or pins, or whatever) together, you are creating a switch, even if you are using a paperclip to connect two pins. The simplest (and surprisingly effective) switch is wired so that one side of the switch goes to a pin, and the other to ground. In the sketch's setup code put in the line:

pinMode(pin, INPUT_PULLUP);

Then in loop, when you want to test the switch use:

buttonState = digitalRead(pin);

If the button is pressed, buttonState will be LOW. When it is released buttonState becomes HIGH. You can get fancier than this, but this is the basic method of using a switch with Arduino.
What, I need to say something else too?

Go Up