Hi gang!
here is the schematic of my limit curcuit:
or the link
The switch is a Cherry magnetic reed switch with contact ratings of 0.5 amps. I am working with one meter sheilded leads to the switch and am in a noisy environment hence the strong pull up resistor R12 of 330 ohms and the RC low pass filter, R11 and C9.
Here is the code supporting it:
#define Lim_pin 9 // for the limit switch input
#define PWM_pin 10 // for the speed control
void Limit_check()
{
if(digitalRead(Lim_pin) == LOW) // found a limit switch
{ // debounce
delay(100); // pause 100ms (1/10) second
if(digitalRead(Lim_pin) == HIGH) {return;} // false alarm, get out
}
.
.
.
handle my limit encounter....
}
void setup()
{
pinMode (Lim_pin, INPUT); // limit switch
pinMode (PWM_pin, OUTPUT); // output for motor PWM
digitalWrite (Lim_pin, HIGH); // turn on pullup resistors
TCCR2A = B10000001; // set Atmega PWM params
TCCR2B = B00000001; // set Atmega PWM params - bout 31250 hz
OCR2A = 0; // init pulse width to min (max = 255) 0 = off
}
void loop()
{
delay(10); // without it, loop is less that one ms
Limit_check();
.
.
.
.
other stuff
}
THE PROBLEM!
Sometimes! <- (emphasis!) The Arduino will think the limit switch is close when it is not. It may go through several hundred limit cycles or happen on the first one. I have place my multi meter across the limit contacts and normal readings are 5v or zero volts depending on if the magnet is in range or not. When I am getting a false reading, the meter says 0.5 volts or zero volts. SOMETHING is clamping it down! Since the only non-discrete component in the system is the Arduino, I am wondering what the hey is going on. I am wondering if the PWM signal I am manipulating on pin ten is affecting pin nine ( they both share the same oscillator, Timer 2). I manipulate it with the code TCCR2A = B10000001 and TCCR2B = B00000001 in setup then inc or dec OCR2A in a subroutine from 0 to 255. I use this output to control my motor speed.