Pages: [1]   Go Down
Author Topic: Magnetic Switch or Push button Switch, getting stuck?  (Read 859 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,

I am trying to use a magnetic switch by sending and output from the Arduino though the switch to an input in the Arduino.

The switch is Normally Open until it contacts the magnet then the switch becomes closed.

I am using a digital input/output. When the switch is closed the serial monitor reports a 1, perfect, right?  

Yes! but, when the magnet is pulled from the switch the input continues to read a 1! this is no good.

We have also tried this with a momentary pushbutton switch, same result.


Any suggestions of why this is getting stuck?  A continuity check confirms that the switches are working properly.


I am using pin 12/13 IN/OUT.   Could using the pins right next to each other cause an issue?  or is it somethign with my code?



Code:
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  // put your setup code here, to run once:

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps


//MAG Switch Ports  
pinMode(12, INPUT);
pinMode(8, INPUT);
pinMode(13, OUTPUT);
pinMode(7, OUTPUT);
//Outputs 5v to magnetic switch
digitalWrite(13, HIGH);
digitalWrite(7, HIGH);

//Receiver Inputs
pinMode(5,INPUT);
pinMode(6, INPUT);

//Motor Outputs
myservo.attach(9);

}

void loop() {
  
  
//Checks PWM for ch1-ch2
double ch1 = pulseIn(5, HIGH);
double ch2 = pulseIn(6, HIGH);

int Mag_rHs = digitalRead(8);
int Mag_lHs = digitalRead(12);

//Maps Inputs about 0

ch1=map(ch1,1250,1750,0,180);
ch2=map(ch1,1250,1750,0,180);

//Constrains the Receiver input
if (Mag_rHs == 1){
   ch1 = constrain(ch1,0,90);//Can only turn left and Neutral
  

}

if (Mag_lHs == 1){
   ch1 = constrain(ch1,90, 180);//Can only turn right and Neutral
}

if (ch1<95 && ch1 >85){
  ch1=90;
}

//Output to Victor Speed Controller
myservo.write(ch1);
Serial.println(Mag_lHs);

//Prints PWM to Serial Monitor
//Serial.println("Steering");
//Serial.println(ch1);
//Serial.println("Forward");
//Serial.println(ch2);
//Serial.println(ch3);
//Serial.println(ch4);
}
« Last Edit: May 10, 2013, 04:37:19 pm by dlsloan4 » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8701
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the only thing connected to the input pin is one side of a switch, when the switch is open the input pin is left floating.

Usually you use a pull-down resistor from the pin to Ground and use the switch to connect the pin to +5 (open=LOW, closed=HIGH) OR you use a pull-up resistor from the pin to +5 and use the switch to connect the pin to Ground (open=HIGH, closed=LOW).
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So what your saying is to put a resistor at the input pin to ground? 


Sorry we are a bunch of MEs working on controls.  What value is the pulldown resistor?  Something to drop the 5v to zero to ground?  Or is it a standard value?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8701
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See this tutorial: http://www.arduino.cc/en/Tutorial/Pushbutton
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 82
Posts: 3846
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


This is useful too....
Logged

The Elders of the Internet know who I am.....
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Pages: [1]   Go Up
Jump to: