Pages: [1]   Go Down
Author Topic: Issues with De-bounce counting  (Read 793 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I am trying to make a controller that will operate multiple winches on an off road vehicle, initially i am using two arduino boards one for the handset and the other for the controller that will power the winches.  On the board that will be used for the handset i have got two switches, one Winch in, and one Winch out. these switches are sending the Letters A and C if the switches are made and the letters B and D if they are not made, the programming for this board is;

Code:
const int WinchIn = 2; // Number of Pin for Winch In
const int WinchOut = 3; // Number of pin for Winch Out
int WIN = 0; //Variable input from Winch In
int WOUT = 0; //Variable input from Winch Out

void setup() {
  Serial.begin(9600);
  pinMode(WinchIn, INPUT); // Setting Winch In as an input
  digitalWrite(WinchIn, HIGH); //Sets Winch In as High
  pinMode(WinchOut, INPUT); // Setting Winch Out as an input
  digitalWrite(WinchOut, HIGH); // Sets Winch Out as High
}

void loop(){
  WIN = digitalRead(WinchIn); // Reads whether Winch In is high or low
  WOUT= digitalRead(WinchOut); // Reads whether Winch Out is high or low
     
  if (WIN == LOW) {
    Serial.println("A"); // if Winch In is pressed outputs A
  }
  else {
    Serial.println("B"); // if Winch In is not pressed outputs B
  }
  if (WOUT == LOW) {
    Serial.println("C"); // if Winch Out is pressed outputs C
  }
  else{
    Serial.println("D"); // if Winch Out is not pressed 0utputs D
}
}

On the second board ive got two LEDs that are being used as a visual output acting as the winches for testing purposes. As well as this i have got a reed switch that will pulse each time the drum of the winch completes a revolution, if the 'Winch in' switch is made and the drum makes a revolution the counter should count up, and if the 'Winch out' switch is made and the drum makes a revolution the counter should count down.

If the number of wraps of rope on the winch drum is between a certain number and the 'winch in', or 'winch out' switch are made then the output should light up indicating that the winch will be switched on.

The programming for the other board is;
Code:
const int ReedSwitch = 4; //Reed Switch Input Pin 4
const int In = 5;         //In Operation of the winch
const int Out = 6;        //Out Operation of the winch
int WIn = 0;              //Variable of Winch In
int WOut = 0;             //Variable of Winch Out
int ReedSw = 0;           //Variable of Reed Switch
int Turncount = 20;       //Number of Turns of Rope
int PrevReedSw = LOW;     //State change of reed switch
int transInt = 50;        // transition time between bounces
int variable = 0;         //Variable is 0 unless data recieved
unsigned long LastReedSw = 0; //Duration Reed sw has been made

void setup() {
  Serial.begin(9600);
  pinMode(ReedSwitch, INPUT);     //Sets pin as an input
  digitalWrite(ReedSwitch, HIGH); //Pin 4 High unless Switched
  pinMode(In, OUTPUT);            //Sets pin as an ouput
  digitalWrite(In, LOW);          //Pin 5 LOW
  pinMode(Out, OUTPUT);           //Sets pin as an output
  digitalWrite(Out, LOW);         //Pin 6 LOW
  char variable;                  //Variable is a character
}

void loop(){
  ReedSw = digitalRead(ReedSwitch);
 
  if( Serial.available() >0 ){
    variable = Serial.read(); //Read available Serial
 
  if (variable == 'A' && ReedSw == LOW && PrevReedSw == LOW){
    if(millis() - LastReedSw > transInt)
    {   
      Turncount ++; //If Reed Sw and Winch in are made for longer
    }               //than the transition time, Count Up
    LastReedSw = millis(); //duration of Reed Sw in Milliseconds
  }

  if (variable == 'C' && ReedSw == LOW && PrevReedSw == LOW){
    if(millis() - LastReedSw > transInt)
    {
      Turncount --; //If Reed Sw and Winch out are made for longer
    }               //than the transition time, Count Down
    LastReedSw =  millis(); //duration of Reed Sw in Milliseconds
  }
  if(variable == 'A' && Turncount <=30){
    digitalWrite(5, HIGH); //If A received and Turns of rope is less
  }                        //than or equal to 30 turns, Winch In
  else{
    digitalWrite(5, LOW); 
  }
  if(variable == 'C' && Turncount >=10){
    digitalWrite(6, HIGH); //If C received and Turns of rops is more
  }                        //than or equal to 10 turns, Winch Out
  else{
    digitalWrite(6, LOW);
  }

  Serial.println(Turncount); //displays the number of Turns of rope
}
}

The two boards are linked together with a common ground, and a common 5v link, to transfer data between the two boards i am using two wires linked between the Tx and Rx terminals.

The issue that i have got is that when the 'winch in' or 'winch out' switch is made and the leds are switched on they are flashing on and off every second rather than being on constantly. there is also a slight delay in the leds switching on.

Any thoughts as to what could be causing this?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why don't you just have one Arduino and a couple of remote normally-open buttons on long wires?

I haven't followed that serial receiver/reed switch processing code but I don't trust it as far as I can throw it. The logic looks fundamentally flawed. You're only looking at the reed switch state when you receive something on the serial port? The drum will be moving continually regardless of whether the serial port is active. I suggest you get rid of the idea of a 'continuous' character stream specifying which way each winch is supposed to be running, and just put a set of plain old normally-open circuits that your Arduino can monitor to decide whether to turn the winch. The logic to detect the drum rotation should be separate from that and should just be told what the expected direction of rotation of the drum is.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 235
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've only glanced at the code so I don't have much to say.  This won't fix the problem but you probably want to do the de-bouncing on the Arduino that's directly connected to the switches.  Also, how long are the RX and TX wires?  You may need to use some transceivers in the final solution.
Logged

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

by the sounds of it, i havnt explained the use of the controller fully.

For my final year project at uni i am making a wireless controller that will operate multiple winches on a land rover (although for the programming i am only using one winch until the programme works). The reason for using two arduino boards is that i am hoping to have one board mounted to the vehicle and another board will be for the wireless handset. Unfortunately the chips i need for the wireless link have not arrived so i am using a physical link between the RX and TX pins to make progress with the project.

PeterH - Why does the logic look flawed, The drum only turns if the switch is pressed, and then for each turn of the drum the arduino board counts either up or down.

How would i seperate the logic to monitor the drum rotation from the winch operation?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

After I wrote the previous post I wondered whether you were thinking ahead to a wireless solution. I agree, that's a good reason for adopting the dual Arduino architecture from the outset.

There are two separate event sources: the character inputs commanding winch movement, and the signal telling you that the drum has completed a revolution.

Either of these can happen independently of the other. So, you should be polling for them independently.

Have one piece of code that tests whether there is any serial input, recognises what command each input character represents, see whether that constitutes a change to the command already running for the relevant winch, and if so configures that winch to do what was commanded.

Meanwhile, completely separately, poll the 'drum rotation' sensor. When you get an event on here which tells you one of the winches has completed a turn, increment or decrement the relevant counter. (For robustness, you will need to deal with the case where the drum moves when the winch is stopped.)

If it was me, I'd encapsulate the logic dealing with an individual winch in a class that dealt with the drum sensor input, position counter and winch motor outputs, with an interface that allowed your sketch to tell it to winch in, winch out or stop. Then you serial-input-parsing code can be a simple switch that sends the relevent command to the relevent winch.

« Last Edit: January 19, 2012, 07:54:02 pm by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

I am assuming that when you say polling them independently you mean that the two bits of code (one for recieving the switching signals from the other board, and one for sensing the drum rotation) should be on seperate loops?

This is the first bit of programming that i have undertaken so i am a bit green when it comes to understanding programming. Would i need to have two two void loop() to programme this or am i barking up the wrong tree?
Logged

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

Hi,

if you are getting a noisy line then you have to tie it to ground when it is not pressed and 5v when it is pressed, that way you will eliminate noise, also the thresh hold for high and low is 3.3v i think, so make sure you have a good 5v on the switch. Use a switch with NO NC, NC to ground and NO to 5v
Logged

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

Hi,

if you are getting a noisy line then you have to tie it to ground when it is not pressed and 5v when it is pressed, that way you will eliminate noise, also the thresh hold for high and low is 3.3v i think, so make sure you have a good 5v on the switch. Use a switch with NO NC, NC to ground and NO to 5v

I have tied all of the inputs from the switch to High with the internal pull up resistors and when switching the switches i am pulling them to ground.
Logged

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

Oh OK then, but you still have to make a hard wire connection to stop noise!
Logged

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

OK, look at the winch in statement, its a conditional one, usually when i come across something like this its because something is becoming not true, so perhaps you have a connection problem with the com ports or the fact that you are reading each time may for some reason be make 'A'  not true. Try putting some timing code, so check the current millis on each loop, then log the time of each 'A', that way you can add an additional && to make sure that even if 'A' is not received (missed) it will still wheel in for a little time, but that time will be so small that it will not go out of control, also have a look at the serial.find() command
Logged

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

Well ive been racking my brains and editing the programme to try and remove the delay and flashing outputs, and ive eventually found out that the issues were being caused by the serial.print;

Code:
Serial.println(Turncount); //displays the number of Turns of rope

if i take the above line out of the code the delay goes away and the outputs are no longer flashing!

Thanks for all the help.
Logged

Pages: [1]   Go Up
Jump to: