Switch case statement with 3 condition

Hi, i want to declare the servo motor control by load cell in switch case statement . Then the case statement will received the data from unity in order to determine the game level using the buzzer as the indicator. But the servo motor doesn't move by condition of weight. Can i know what is the problem with my code?

#include "HX711.h"
#include <Servo.h>

#define DOUT  3
#define CLK  2

const int buzzer = 11;

Servo myservo;

float calibration_factor = 242;
float weight = 763;
float ounces;

double output;

int pos = 0;

HX711 scale;

void setup() {
  Serial.begin(9600);
  scale.begin(DOUT, CLK);
  scale.set_scale(calibration_factor);  // this value is obtained by calibrating the scale with known weights; see the README for details
  scale.tare();                        // reset the scale to 0
  myservo.attach(9);
  myservo.write(pos);
  pinMode (11, OUTPUT);
}

void loop() {

  weight = scale.get_units(), 10;
  ounces = weight * 0.035274;
  //Serial.print(weight);
  //Serial.print(" grams");
  Serial.println((String)pos);
  level();
}

void level() {
  if (Serial.available() > 0)
  {
    int game = Serial.read();

    switch (game)
    {
      case 'E':                         // easy level
        if (weight < -100)
        {
          pos -= 1;
          if (pos <= 0)pos = 0;
          myservo.write(pos);
        }
        if (weight >= 100) {
          pos += 1;
          if (pos >= 90)pos = 90;
          myservo.write(pos);
        }
          digitalWrite (11, HIGH);
          delay (2000);
          digitalWrite (11, LOW);

          /*for (pos = 0; pos <= 30; pos += 10) {
            myservo.write(pos);
            }
            for (pos = 30; pos >= 0; pos -= 10) {
            myservo.write(pos);*/
          break;

        case 'N':                        // normal level
          //(strcmp(myCol, "N") == 0);
          //pos = 60;
          //condition_2();
         // int levelvalue_2 = 200;
          //output = weight;
          if (weight < -200)
          {
            pos -= 1;
            if (pos <= 0)pos = 0;
            myservo.write(pos);
          }
          if (weight >= 200) {
            pos += 1;
            if (pos >= 90)pos = 90;
            myservo.write(pos);
          }
          digitalWrite (11, HIGH);
          delay (2000);
          digitalWrite (11, LOW);
            break;

          case 'H':                      // hard level
            if (weight < -300)
            {
              pos -= 1;
              if (pos <= 0)pos = 0;
              myservo.write(pos);
            }
            if (weight >= 300) {
              pos += 1;
              if (pos >= 90)pos = 90;
              myservo.write(pos);
            
            }
          }
        }
    }

Pretend you know NOTHING about what you are doing, like ALL of us here. Now read your description of your problem. Does it make ANY sense at all to you? Do you see enough information for anyone to help you?

You are calling the .get_units() function. This does not return the measurement of the scale. Please look at the examples that come with the library

That ", 10" doesn't do anything so remove it.

You don't have to cast a number as a String to print the value. Just use 'pos' in place of '(String)pos'.

Your 'level' function doesn't do anything unless a character is waiting in the input buffer. How often do you send 'E', 'N', or 'H' to the Arduino? If it is not many times per second you are not going to see much motion from your servo.

Looks like the only difference between levels is the weight limit. Make a global named WeightLimit and set that when a character arrives. Then you can use the same code for each time the weight is read.

void level() 
{
  if (Serial.available() > 0)
  {
    int game = Serial.read();
    switch (game)
    {
    case 'E' : WeightLimit = 100; break;
    case 'N' : WeightLimit = 200; break;
    case 'H' : WeightLimit = 300; break;
    }
  }
  
  if (weight < -WeightLimit)
  {
    pos -= 1;
    if (pos <= 0) pos = 0;
  }

  if (weight >= WeightLimit) 
  {
    pos += 1;
    if (pos >= 90) pos = 90;
  }
  
  myservo.write(pos);

  digitalWrite (11, HIGH);
  delay (2000);
  digitalWrite (11, LOW);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.