Go Down

Topic: Arduino Uno Inconsistent Reading in One System, ok In another. (Read 402 times) previous topic - next topic

reaperwolverine

Hello,

I am new to Arduino and trying out a simple stroke counter connected to a hydraulic press using an Arduino Uno board. Here's My Code:-

Code: [Select]

#include<stdio.h>

void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);
}

void loop() {
  int buttonState = digitalRead(2);
  if (buttonState == LOW){
    Serial.println("BBS");
    delay(3000);
  }
   else {
    Serial.println(buttonState);
   }
}


2 wires are connected from the machine relay to the board's Digital Pin 2 (Input_Pullup) and the Ground.
The machine relay connects the wires for a brief second or so during the end of operation which the system counts as a stroke. Stroke Time is about 15-20 seconds.


The issue is that this all works fine on my Laptop's 2.0 USB Port.

When I connect the card to a desktop machine's USB 2.0 port, the reading is triggered even if the machine is switched off and I get a reading every 3 seconds due to the delay in coding.
If I decrease the delay to 1 second, It gives the reading every second. After about 8-9 readings, it goes silent for about 9-10 seconds and starts reading again every 3 seconds(1 if I decrease the delay).


I've tried out the following things:-

-Driver versions is same in both systems.
-OS is same in both systems (Win 10).
-Remove the hydraulic machine from the equation and connect the wires manually and it still gives
wrong data but less frequently.
-Used other pins (3, 4, 5) instead of pin 2 and there's no difference.
-Tried to Google the problem but I didn't come across a case where one card behaves differently in 2 systems.

Any help/ideas would be greatly appreciated.
Please tell me if any additional info is needed.

PerryBebbington

#1
Jul 28, 2020, 06:00 pm Last Edit: Jul 28, 2020, 06:05 pm by PerryBebbington
You are detecting when the input is active, you need to detect when it becomes active.

This comes up 500 times a day on here, please do the tutorials, which explain how to detect buttons and other inputs becoming pressed / active, and search for other answers to the same questions.

In the IDE at least do the examples under 02 Digital.

++Karma; // For posting your code correctly on your first post.

reaperwolverine

Thanks :D . I'll definitely look it up and post it here if i find a solution.

reaperwolverine

Quote
You are detecting when the input is active, you need to detect when it becomes active.
Well I tried the tutorial(thanks for the tip) and modified the code as follows:-

Code: [Select]
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to

// Variables will change:
int buttonPushCounter = 0;
int buttonState = 0;         // current state of the button
int lastButtonState = 1;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("BBS");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

}

This is basically the code in the tutorial with slight modifications.


This is the correct output as seen in my Laptop (Timestamps are about 40 seconds apart consistent with button presses.).

Code: [Select]
17:00:55.947 -> BBS
17:00:55.947 -> number of button pushes: 1
17:00:57.056 -> off
17:01:37.919 -> BBS
17:01:37.919 -> number of button pushes: 2
17:01:39.061 -> off
17:02:24.866 -> BBS
17:02:24.866 -> number of button pushes: 3
17:02:25.976 -> off
17:02:59.967 -> BBS
17:02:59.967 -> number of button pushes: 4
17:03:01.073 -> off
17:03:34.245 -> BBS
17:03:34.245 -> number of button pushes: 5
17:03:35.392 -> off
17:04:11.984 -> BBS
17:04:11.984 -> number of button pushes: 6
17:04:13.088 -> off
17:04:50.997 -> BBS
17:04:50.997 -> number of button pushes: 7
17:04:52.137 -> off
17:05:28.635 -> BBS
17:05:28.635 -> number of button pushes: 8
17:05:29.745 -> off
17:05:54.602 -> BBS
17:05:54.602 -> number of button pushes: 9
17:05:55.715 -> off
17:06:20.136 -> BBS
17:06:20.136 -> number of button pushes: 10
17:06:21.245 -> off


This is the incorrect output as seen in the desktop machine (Timestamps are almost constant. Added limited data due to character limit)

Code: [Select]

16:43:23.933 -> number of button pushes: 1
16:43:23.933 -> off
16:43:24.029 -> BBS
16:43:24.029 -> number of button pushes: 2
16:43:24.029 -> off
16:43:24.122 -> BBS
16:43:24.122 -> number of button pushes: 3
16:43:24.122 -> off
16:43:24.216 -> BBS
16:43:24.216 -> number of button pushes: 4
16:43:24.216 -> off
16:43:24.310 -> BBS
16:43:24.310 -> number of button pushes: 5
16:43:24.357 -> off
16:43:24.404 -> BBS
16:43:24.404 -> number of button pushes: 6
16:43:24.450 -> off
16:43:24.497 -> BBS
16:43:24.497 -> number of button pushes: 7
16:43:24.544 -> off
16:43:24.638 -> BBS
16:43:24.638 -> number of button pushes: 8
16:43:24.638 -> off
16:43:24.732 -> BBS
16:43:24.732 -> number of button pushes: 9
16:43:24.732 -> off
16:43:24.825 -> BBS
16:43:24.825 -> number of button pushes: 10
16:43:24.872 -> off
16:43:24.919 -> BBS
16:43:24.919 -> number of button pushes: 11
16:43:24.966 -> off
16:43:25.013 -> BBS
16:43:25.013 -> number of button pushes: 12
16:43:25.060 -> off
16:43:25.153 -> BBS
16:43:25.153 -> number of button pushes: 13
16:43:25.153 -> off
16:43:25.247 -> BBS
16:43:25.247 -> number of button pushes: 14
16:43:25.247 -> off
16:43:25.341 -> BBS
16:43:25.341 -> number of button pushes: 15
16:43:25.341 -> off
16:43:25.434 -> BBS
16:43:25.434 -> number of button pushes: 16
16:43:25.481 -> off
16:43:25.528 -> BBS
16:43:25.528 -> number of button pushes: 17
16:43:25.575 -> off
16:43:25.669 -> BBS
16:43:25.669 -> number of button pushes: 18
16:43:25.669 -> off
16:43:25.762 -> BBS
16:43:25.762 -> number of button pushes: 19
16:43:25.762 -> off
16:43:25.856 -> BBS
16:43:25.856 -> number of button pushes: 20
16:43:25.856 -> off
16:43:25.950 -> BBS
16:43:25.950 -> number of button pushes: 21
16:43:25.997 -> off
16:43:26.044 -> BBS
16:43:26.044 -> number of button pushes: 22
16:43:26.090 -> off
16:43:26.138 -> BBS
16:43:26.138 -> number of button pushes: 23
16:43:26.185 -> off
16:43:26.279 -> BBS
16:43:26.279 -> number of button pushes: 24
16:43:26.279 -> off
16:43:26.372 -> BBS
16:43:26.372 -> number of button pushes: 25
16:43:26.372 -> off
16:43:26.466 -> BBS
16:43:26.466 -> number of button pushes: 26
16:43:26.513 -> off
16:43:26.560 -> BBS
16:43:26.560 -> number of button pushes: 27
16:43:26.607 -> off
16:43:26.654 -> BBS
16:43:26.654 -> number of button pushes: 28
16:43:26.700 -> off
16:43:26.794 -> BBS
16:43:26.794 -> number of button pushes: 29
16:43:26.794 -> off
16:43:26.888 -> BBS
16:43:26.888 -> number of button pushes: 30
16:43:26.888 -> off
16:43:26.982 -> BBS
16:43:26.982 -> number of button pushes: 31
16:43:26.982 -> off
16:43:27.075 -> BBS
16:43:27.075 -> number of button pushes: 32
16:43:27.123 -> off
16:43:27.169 -> BBS
16:43:27.169 -> number of button pushes: 33
16:43:27.216 -> off
16:43:27.310 -> BBS
16:43:27.310 -> number of button pushes: 34
16:43:27.310 -> off
16:43:27.404 -> BBS
16:43:27.404 -> number of button pushes: 35
16:43:27.404 -> off
16:43:27.497 -> BBS
16:43:27.497 -> number of button pushes: 36
16:43:27.497 -> off
16:43:27.591 -> BBS
16:43:27.591 -> number of button pushes: 37
16:43:27.638 -> off
16:43:27.685 -> BBS
16:43:27.685 -> number of button pushes: 38
16:43:27.732 -> off
16:43:27.778 -> BBS
16:43:27.778 -> number of button pushes: 39
16:43:27.826 -> off
16:43:27.921 -> BBS
16:43:27.921 -> number of button pushes: 40



Unfortunately the issue seems to be something other than the program. I was hoping someone had faced a similar problem in the past.

Any Ideas?

PaulRB

Do you take the laptop to the machine and use short wires but use long wires when connecting to the desktop because it's further away?

Maybe try adding an external pull-up resistor, e.g. 4K7 or even 1K. I'm wondering if, with long wires, the internal pull-up is too weak and interference from mains wiring or something is causing the unwanted triggering.

DrAzzy

I'm with PaulRB on this one - it ain't the software. An INPUT_PULLUP pin toggling in the absence of actually connecting that wire to anything is the hallmark of electrical noise/EMI.

The wires I would be concerned about the length of would be the ones going to the switch. Shielded cable may help.

Stronger pull-up definitely would. If I found a value that worked, I'd then try with something of about double the resistance. If approx. double the original resistance worked too, I'd go with the original resistance. If it did not work, I'd try for something around half of the original resistance.

That said, one can also code around it, particularly if a stronger pullup greatly reduces - but does not eliminate - the problem.

One diagnostic I would suggest is to try this:
Code: [Select]

#define BUTTON_PIN 2
// change this if your board has an LED on a different pin, or you want to use an LED on a different pin
#define LED_PIN LED_BUILTIN

void setup() {
pinMode(BUTTON_PIN,INPUT_PULLUP);
pinMode(LED_PIN ,OUTPUT);
}
void loop() {
digitalWrite(LED_PIN ,!digitalRead(BUTTON_PIN));
}


That will turn on the LED on when it thinks the button is pressed and off when it doesn't.
It's helpful for debugging because of a few advantages over serial: 1) it's instant, 2) you get very clear visual feedback about how long these anomalous readings are. As you add pullups, even if the first resistance you try doesn't fix it, if you see than the LED is lit less of the time, you know you're going in the right direction.

There are some further hardware mitigations possible:
A capacitor can be placed between the BUTTON_PIN and ground. 0.1uF would be a reasonable value to try. This will slightly slow the response time (but you're measuring something very slow, so this shouldn't be a concern).
Software mitigation, if hardware mitigation is impractical or contraindicated by project constraints not mentioned in your post is possible. Implementation would be similar to "software debounce" of switches - except with the aim of eliminating totally bogus spikes, rather than transitions caused by bounce. The bar for this is far lower if you've already mostly solved it in hardware.
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Go Up