Can anyone help me with while loop [solved]

Hello everyone I am doing a school project which is an automatic powder dispenser. I want to get input using potentiometer and button but it only works for first digit I can't move to second digit, at the same time I think I can't get values out of the while loop. Can anyone help me with code? I don't know how to code I was just putting together the code I found in forums .

Edit: I found the mistakes incase anyone else has same problem
first I couldn't move to second digit because I wasn't updating value of button_state when I change
while (buttonState == HIGH) {
to
while (digitalRead(buttonPin == HIGH) { it worked. Second problem was because I put int in the while loop arduino was creating new variable for that loop so when I am out the loop the value was gone I just removed int part in the while loops
It works without problem now
The problems mostly was mostly because I didn't know coding but at the end it worked it is the important part. Thanks everyone for help

#include <Q2HX711.h>
#include <TM1637.h>
#include <Servo.h>

#define POTENTIOMETER_PIN A5 // potentiometer port
Servo servo;
int CLK = 11;
int DIO = 10;
float weight = 0.0;

TM1637 tm(CLK, DIO);

const byte hx711_data_pin = 4;
const byte hx711_clock_pin = 5;
const int buttonPin = 2;     // the number of the pushbutton pin

float y1 = 145.28; // calibrated mass to be added
long x1 = 0L;
long x0 = 0L;
float avg_size = 10.0; // amount of averages for each mass measurement
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


Q2HX711 hx711(hx711_data_pin, hx711_clock_pin); // prep hx711

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  servo.attach(8);
  tm.init();

  //set brightness; 0-7
  tm.set(2);
  Serial.begin(9600); // prepare serial port
  delay(1000); // allow load cell and hx711 to settle
  x0 = 8366859; // rate for scale
  x1 = 8519917; // rate for scale
}
int firstdigit = 0;
int seconddigit = 0;
int thirddigit = 0;
int fourthdigit = 0;
void loop() {


  buttonState = digitalRead(buttonPin);
  while (buttonState == HIGH) {
    buttonState = digitalRead(buttonPin);
    Serial.println(buttonState);
    int data = analogRead(POTENTIOMETER_PIN); // read pot
    int firstdigit = map(data, 0, 1023, 0, 9); // convert to 1 digit
    tm.display(3, firstdigit); // print digit in lcd
    if (buttonState == LOW) {
      break;
    }
    delay(100); // little bit time for board to rest


  }
  delay(1000);
  while (buttonState == HIGH) {
    buttonState = digitalRead(buttonPin);
    int data = analogRead(POTENTIOMETER_PIN);  // read pot
    int seconddigit = map(data, 0, 1023, 0, 9); // convert to 1 digit
    Serial.println(seconddigit);
    tm.display(2, seconddigit); // print digit in lcd
    if (buttonState == LOW) {
      break;
    }
    delay(100); // little bit time for board to rest
  }
  delay(500);
  while (buttonState == HIGH) {
    buttonState = digitalRead(buttonPin);
    int data = analogRead(POTENTIOMETER_PIN);  // read pot
    int thirddigit = map(data, 0, 1023, 0, 9); // convert to 1 digit
    Serial.println(thirddigit);
    tm.display(1, thirddigit); // print digit in lcd
    if (buttonState == LOW) {
      break;
    }
    delay(100); // little bit time for board to rest
  }
  delay(500);
  while (buttonState == HIGH) {
    buttonState = digitalRead(buttonPin);
    int data = analogRead(POTENTIOMETER_PIN); // read pot
    int fourthdigit = map(data, 0, 1023, 0, 9); // convert to 1 digit
    Serial.println(fourthdigit);
    tm.display(0, fourthdigit); // print digit in lcd
    if (buttonState == LOW) {
      break;
    }
    delay(100); // little bit time for board to rest
  }
  float weight  = firstdigit / 10 + seconddigit + thirddigit * 10 + fourthdigit * 100 ; // the data is in the while loop so program cant read it out of the loop
  Serial.println(weight);


  // averaging reading
  long reading = 0;
  for (int jj = 0; jj < int(avg_size); jj++) {
    reading += hx711.read();
  }
  reading /= long(avg_size);
  // calculating mass based on calibration and linear fit
  float ratio_1 = (float) (reading - x0);
  float ratio_2 = (float) (x1 - x0);
  float ratio = ratio_1 / ratio_2;
  float mass = y1 * ratio;

  float r = round(mass * 10);

  int d1 = (int) r % 10;
  int d2 = (int) r / 10 % 10;
  int d3 = (int) r / 100 % 10;
  int d4 = (int) r / 1000 % 10;
  Serial.println(mass);
  while ( mass < weight) {
    long reading = 0;
    for (int jj = 0; jj < int(avg_size); jj++) {
      reading += hx711.read();
    }
    reading /= long(avg_size);
    // calculating mass based on calibration and linear fit
    float ratio_1 = (float) (reading - x0);
    float ratio_2 = (float) (x1 - x0);
    float ratio = ratio_1 / ratio_2;
    float mass = y1 * ratio;


    float r = round(mass * 10);
    Serial.println(mass);
    Serial.println(weight);
// converting to one digit and displaying on display
    int d1 = (int) r % 10;
    int d2 = (int) r / 10 % 10;
    int d3 = (int) r / 100 % 10;
    int d4 = (int) r / 1000 % 10;
    tm.display(3, d1);
    tm.display(2, d2);
    tm.display(1, d3);
    tm.display(0, d4);
// motor
    servo.write(0);
    delay(500);
    servo.write(90);


  }

  tm.display(3, d1);
  tm.display(2, d2);
  tm.display(1, d3);
  tm.display(0, d4);

  delay(1000);
}

In detailed step by step instructions, what is your program supposed to do?

For somebody who doesn't know how to code, this looks like an involved project. What part is working? What part isn't?

So, IF I understand, the pot sets a digit 0-9 and the button locks that digit and moves to the next digit position? Low (right) to high (left)? How to ENTER... long button press or two short pressss? How to backspace?

Thanks for reply

  • First It should get a input from user How many powder they want to dispense. I want to go digit by digit because I think my display only can show 0-9 and some letters. I used a potentiometer to get input.

  • Then it should put together this digits it is gonna be weight that user wants to dispense.

  • Scale should show the current weight in display

  • Then scale should read the weight and as long as it is under the weight of powder user want motor should work
    Motor is gonna be connected to dispenser so it is gonna dispense until the desired weight reached.

It needs to be not connected to pc

It is university project for one of the elective courses but I am studying pharmacy so it is weird

the motor is working the scale is working motor stops when the desired weight reached but I can't get input using potentiometer and button

I also can give values to first digit with pot but when I print value out of while loop it shows as 0 in the while loop it show what ever value I set with pot

Thanks for reply,

Yes the idea was like that but I don't know how to move or enter my idea was when button not pressed it should update digit but when pressed 1 time move to second digit but clearly it is not working it is my first time using Arduino so I have no idea what to do

I would rather use a rotary encoder for that. Do a brand new circuit with just the encoder and the display, and see if you can use the encoder to set an arbitrary value, one digit at a time. When the value is set, have the program just echo it back on serial monitor. This isn't exactly easy, though, so, depending upon your range of values and the step you want, you can use the encoder to just increment the value by the step with each click..

One step at a time is the best approach.
Use the analog read example and work with the POT and the MAP function...
https://www.arduino.cc/reference/en/language/functions/math/map/

Then add in test code for push-button (remember debounce!)

As a "school" project, I hesitate to take a newbee into the deep-dark forest and rework the concept s/he conceived...

Most of us old timers would go to an encoder, but a potentiometer is kind-of original, IMO.

I tried with only them pot and map function is working I can get first digit without problem but I didn't know the debounce thing. Thanks , I will look into it.

The problem is I only have access to arduino board one time every week so trying things is really hard.

Well you will either hate or love wokwi.com then.

Hate it because no excuse not to work on your code.

Love it because you don't need access to the Arduino y'all are sharing.

Take a look:

HTH

a7

Goodness!
May I inquire your general location? Many cities have "Maker Spaces" with Arduino.

Ray

Leuven, Belgium

Thanks
I didn't know there is thing like that I will try it

Fablab-Leuven | CoP CC on Participatory and Deliberative Democracy (europa.eu)

Give them a telephone call. If they do not have an electronics space, maybe there are alternatives for hands-on.

Ray

Thank you,
I am gonna check it.

Next step....

You stated you wanted to depress the button and when released, the digit would move to the next location? So,
1 - read button, debounce (library for that or roll your own code)
2 - button down and while down read and MAP the analog from Pot
3 - button up, store that digit value
4 - all digits populated?
a - No, go to 1
b - yes, drop through to 5
5 - depending on the order input:

D1 + D2 * 10 + D3 * 100 + D4 * 1000 = Quantity desired

The above assumes D1 was first digit populated, that all 4 digits must populate even if 0. No provision is made to backspace.

Start reading about debounce here:
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Debounce

If anyone wants to see project I uploaded it