Resistance measurement + State change detection

Hi all! I'm programming a code that measuring resistance of a relay contact, the contact will switch 50 times, if the resistance of the contact is greater than 3 ohm, lcd will display :" High ohm", I have sketch already but I dont know how to program correctly, pls any help, thank you all and regard.

Post what you have so far. Tell us what it does. Tell us how that compares with what you want. Please be very specific about what you want to happen.

It’s like the combination between resistance measurement and state change detection, I wanna measure resistance of a relay contact when switching, lcd display counter and resistance, if R>2 ohm, lcd display: “high ohm”, and if the counter is 50 and the contact resistance<2 ohm, lcd displays:" none high ohm.

Yeah, you said that.

Delta_G:
Post what you have so far. Tell us what it does. Tell us how that compares with what you want.

Or if you’re looking for someone to just write it for you, grab your wallet and head over to the “Gigs and Collaborations” section of the forum.

Now I just change a little bit, no limit 50 times counting, when 1 relay contact is high ohm, it will
display immediately: high ohm, this sketch is probably not realistic, so Ineed your support.
This is my sketch:
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int analogPin= 0;
int raw= 0;
int Vin= 5;
float Vout= 0;
float R1= 1000;
float R2= 0;
float buffer= 0;
const int buttonPin = 2;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop()
{
raw= analogRead(analogPin);
if(raw)
{
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
while(R2<3)
{
buttonState = digitalRead(buttonPin);
if(lastButtonState != buttonState)
{
if (buttonState == 1)
{ buttonPushCounter++;
lcd.print("R2: ");
lcd.println(R2);
lcd.setCursor(0,1);
lcd.print(“push time:”);
lcd.println(buttonPushCounter);
};
lastButtonState = buttonState;
};
};

};
lcd.clear();
lcd.print("high ohm: ");
lcd.println(R2);}

Please read this: How to Use This Forum and try to respect the rules here. Pay particular attention to point 7.

while(R2<3)
 {
buttonState = digitalRead(buttonPin);
if(lastButtonState != buttonState)
      {
          if (buttonState == 1) 
               {  buttonPushCounter++;
                  lcd.print("R2: ");
                  lcd.println(R2);
                  lcd.setCursor(0,1);
                  lcd.print("push time:");
                  lcd.println(buttonPushCounter);
                };
                  lastButtonState = buttonState;
               };
      };

This will turn into an infinite loop. Since R2 doesn’t change in the while loop, if it is less than three to get into the while loop then it will never ever exit that loop.

Your picture isn't showing up.

This will turn into an infinite loop. Since R2 doesn't change in the while loop, if it is less than three to get into the while loop then it will never ever exit that loop. [/quote] Yeah, but for 1 case, when the relay contact doesn't switch perfectly, its resistance>3 ohm, will it get out of infinite loop and display: 'high ohm' or not? https://drive.google.com/file/d/0BzHrWTfUZYO0QlN0OU8yMzkxTlE/view

No, It won't get out of the while loop because you don't measure any resistance or assign any new value to R2 inside the while loop. If R2 is greater than 3 on the very first pass through the code then it would print it. But if R2 is ever less than 3 then the code is locked up at that point.

I see, so should I continue using while command, or change to use another command, and it will check counter +resistance without stuck in infinite loop!?

https://scontent.fsgn2-2.fna.fbcdn.net/v/t35.0-12/16880084_1134444596678607_697841264_o.png?oh=319781dc487fd0d98bfb45c80ab3aab6&oe=58B04D82

nichky12345: I see, so should I continue using while command, or change to use another command, and it will check counter +resistance without stuck in infinite loop!?

That depends on what you want it to do. You could check the resistance and put a new value in R2 in the while loop. Or you could get rid of the while loop altogether and take advantage of the fact that the loop function already loops. From your description of what you want to do, I don't see any room for any while loops.

From what you describe you need something like this pseudo:

int numberOfSamples = 0;
boolean highResistance = false;


void loop(){

read the resistance

if it is greater than 3, set highResistance to true and print "High Ohms"

increment numberOfSamples

if numberOfSamples is greater than 50 AND highResistance is false then print "none high ohm"

Thank, I'' try this

I try to get rid of while loop, is it ok?

#include <LiquidCrystal.h>
 
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
int analogPin= 0;
int raw= 0;
int Vin= 5;
float Vout= 0;
float R1= 1000;
float R2= 0;
float buffer= 0;
const int  buttonPin = 2;    
int buttonPushCounter = 0;   
int buttonState = 0;        
int lastButtonState = 0; 
void setup()
{
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop()
{
raw= analogRead(analogPin);
if(raw) 
{
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
 if(R2<3)
 {
buttonState = digitalRead(buttonPin);
if(lastButtonState != buttonState)
      {
          if (buttonState == 1) 
               {  buttonPushCounter++;
                  lcd.print("R2: ");
                  lcd.println(R2);
                  lcd.setCursor(0,1);
                  lcd.print("push time:");
                  lcd.println(buttonPushCounter);
                  lcd.setCursor(0,1);
                };
                  lastButtonState = buttonState;
       };
 }
 else
 {     
lcd.clear();
lcd.print("high ohm: ");
lcd.println(R2);
  };};}

Please do us all a favour and Auto Format your code in the IDE and post it here in code tags.

Why all the semicolons on closing braces ?

Does it work?

The button represents for a relay contact and R2 (a relay contact resistance is approximately 1-1.5 ohm), the counter increments when pushing, but R2 is 0.98 all the time, and I’m confused about is it realistic to set a real board like its simulation? for example, is case of R2>3 ohm for the 1st time, lcd display: high ohm, but the relay contact doesn’t stop, it continue switch incessantly, so after that maybe R2<3 ohm, will it save lcd display: high ohm or not?

Please autoformat your code and put it in code tags. I'm not going to try to parse through it like this anymore. If you want to just ignore the forum rules then fine, but don't expect much help if you just thumb your nose at us.

Obviously the high ohm would not remain on the screen because you told it that if R2 is less than 3 to print some other things. But you could have just tested it and figured that out.