Scale tare button problem

Hi i have a problem , i want to code a push button to reset my scale to zero but , when i click on it the scale start calibrating ,any solution!??
But without scale.tare(); function.
This is the code i'm using and the button is named (sw).

#include <LiquidCrystal.h>
LiquidCrystal lcd(11,10,9,8,7,6,5,4,3,2);
#define DT A0
#define SCK A1

#define sw 12

long sample=0;

float val=0;

long count=0;

unsigned long readCount(void)

{

unsigned long Count;

unsigned char i;

pinMode(DT, OUTPUT);

digitalWrite(DT,HIGH);

digitalWrite(SCK,LOW);

Count=0;

pinMode(DT, INPUT);

while(digitalRead(DT));

for (i=0;i<24;i++)

{

digitalWrite(SCK,HIGH);

Count=Count<<1;

digitalWrite(SCK,LOW);

if(digitalRead(DT))

Count++;

}

digitalWrite(SCK,HIGH);

Count=Count^0x800000;

digitalWrite(SCK,LOW);

return(Count);

}

void setup()

{

pinMode(SCK, OUTPUT);

pinMode(sw, INPUT_PULLUP);

lcd.begin(16,2);

lcd.print(" Weight ");

lcd.setCursor(0,1);

lcd.print(" Measurement ");

delay(300);

lcd.clear();

calibrate();

}

void loop()

{

count= readCount();

int w=(((count-sample)/val)-2*((count-sample)/val));

lcd.setCursor(0,0);

lcd.print("Measured Weight");

lcd.setCursor(0,1);

lcd.print(w);

lcd.print("g ");

if(digitalRead(sw)==0) return 0;

{

val=0;

sample=0;

w=0;

count=0;

}

}

void calibrate()

{

lcd.clear();

lcd.print("Calibrating...");

lcd.setCursor(0,1);

lcd.print("Please Wait...");

for(int i=0;i<100;i++)

{

count=readCount();

sample+=count;

}

sample/=100;

lcd.clear();

lcd.print("Put 100g & wait");

count=0;

while(count<1000)

{

count=readCount();

count=sample-count;

}

lcd.clear();

lcd.print("Please Wait....");

delay(200);

for(int i=0;i<100;i++)

{

count=readCount();

val+=sample-count;

}

val=val/100.0;

val=val/100.0; // put here your calibrating weight

lcd.clear();

}

if not tare, what else the button should do

 if (digitalRead(sw) == 0) return 0;  {
    val = 0;
    sample = 0;
    w = 0;
    count = 0;
  }

Ooops. Only the "return 0;" is part of the if clause. What you have there is equivalent to:

  if (digitalRead(sw) == 0)
    return 0;
  {
    val = 0;
    sample = 0;
    w = 0;
    count = 0;
  }

if return would be a part of if clause, it doesn't change much :slight_smile:

The bigger issue is, he's clearing the four values unconditionally, instead of when the button's pressed...
And, IIRC, that return may actually exit loop(), but not sure on that one as I rarely use return() to escape out of code structures like this.
Either way, he's somehow popping off into a function that's only executed via setup(), so something smells...

Welcome to the forum.

The return makes the statements the equivalent of

	if (digitalRead(sw) != 0) {
		val = 0;
		sample = 0;
		w = 0;
		count = 0;
	}

so the reset is under control of the pushbutton and may not be oppsite to how you want to control that.

How is your switch wired? What does digitaklRead() return when the button is pressed?


Yes. If the OP sees

"Calibrating..."

more than just at power up or reset, something is def wrong.

Imma toss it into the simulator.

a7

OK, I had to short out a few while loops becuase I do not have the hardware that would otherwise change and let them exit.

Having done that, the code seems to function, that is to say I see no indication that it is a software problem causing the reset.

The only thing the software might be doing just now is to get caught in one of those while loops. But that would mean no further changes.

@doudouprs you should add some serial printing as I did, to see the values of key variables, check that they are plausible and are properly informing the flow through your program.

In seetup(), add

  Serial.begin(115200);
  Serial.println("bob is your uncle\n");

then use Serial.print() very liberally all over the place.

HTH

a7

if (digitalRead(sw) == 0) return 0; {...}

@alto777
So in this case, the if structure will continue with the {} content, after executing the "return 0;" ? That's interesting. It was my impression that
if(something) statement; was a completed control structure.

I have no hardware connected, so I can't test this right now.
That would also mean the following needs revision:

specifically,

The brackets may be omitted after an if statement. If this is done, the next line (defined by the semicolon) becomes the only conditional statement.

No
Note that @alto777 has reverted the condition of the if structure.

It is not an if structure will execute {} content. In the OP code the only contents of if structure is a return statement. The {} content is a normal code AFTER if structure. But because fairing the return will cause the code jump to the start of loop(), the {} content can be reached only if condition doesn't matched.

@b707 Please note, that's what I said earlier, and my comment you responded to was directed to @alto777 as he seems to indicate otherwise.

Note that @alto777 has reverted the condition of the if structure.

Nope. My point was, the return is the only statement executed conditionally, the rest execute unconditionally, leaving the OP thinking it's recalibrating.
But that's okay, you guys sort it out. I'm done.

OK, CU, but first...

This, directly copied from the original sketch


if(digitalRead(sw)==0) return 0;

{

val=0;

sample=0;

w=0;

count=0;

}

Is identical in function, in the context from which it was snipped, to


if (digitalRead(sw) != 0) {
  val=0;
  sample=0;
  w=0;
  count=0;
}

The first one returns if the switch reads LOW, therefor does not reset the variables in the {} block, as that is never executed. The block is executed if the switch reads HIGH.

My rewrite executes the block if the switch reads HIGH.

Same same. Both versions conditionally execute those assignemnts.

Or I need to go to sleep or wake up. Always a possibility. :expressionless:

There will in any case be no need to change the documentation of the if statement.

a7

1 Like

I guess I'll try that when I get to the lab. Because as far as I know, the scope of the conditional ends after one statement, irregardless of anything beyond that, unless the 'door has been opened' for more than one statement, using {. Yes, I know, those are words, not the canonical phrasing from some authority, but there it is.
Maybe, if you have the option, try it in one of the simulators? Because I don't do sim, I cannot, until I have my hands on a Nano.

if(digitalRead(sw)==0) return 0;  //<<<<<At this point, the if control statement has no more relevance.  It has no more effect on subsequent statements, whether they're enclosed in {} or not.

I am discovering things in the simulator.

See if this helps:

Code copied from the original sketch

if(digitalRead(sw)==0) return 0;

{

val=0;

sample=0;

w=0;

count=0;

}

Is identical in function, in the context from which it was snipped, to

  if (digitalRead(sw) == 0) {
    return 0;
  }
  else {  // digitalRead was not == 0
    val=0;
    sample=0;
    w=0;
    count=0;
  }

Again, completely equivalent logically.

Without being sure the OP wired the button the way we all might (pulled high, presst reads LOW) it looks like the button logic is backwards.

@doudouprs when you comb through all this, could you please post the code that just reads the scale and reports? I can't get it to function, and am too lazy to debug code when you probably have a working version with one or two fewer features. I hope.

Or point to any examples code that you are basing your code upon, or which you have actually stolen borrowed.

TIA

a7

1 Like

Is identical in function, in the context from which it was snipped, to

  if (digitalRead(sw) == 0) {
    return 0;
  }
  else {  // digitalRead was not == 0
    val=0;
    sample=0;
    w=0;
    count=0;
  }

Again, completely equivalent logically.

Perhaps, but I'm going to have to actually see it, in front of me, to believe it. So, back later.

it's stuck now when i added the " else "it tares but the scale no longer measure.

Given the hot mess above(sorry for that!), please provide your current code.

i can send you a pic of the sumulation

No thanks, I'll pass. One of these days, I'll climb on that horse.