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);
}
}
}
}