Hello! I'm making honey filling machine with scale (as a feedback). For this project I'm using ACS606 servo drive, BLM57180-1000 servo motor (which is using PWM control),arduino scale, ultrasonic sensor and button. Working principle is : when you press button and ultrasonic sensor sees tare on the scale and program starts. At this moment servo motor is starting and will rotate till tare will be full (reach 600 grams).
But there is a problem: in while loop(marked red) my servo motor is rotating very, very slow. In a clean arduino sketch servo control program piece is working very well and reaches almost max speed. But when I write code here in main program, it almost rotating. When i delete all those Serial.print and etc. only leave servo control program piece , servo is working good. And at that moment my servo never stops, becouse my while loop cant read scale variable (x = scale.get_units(1));
Serial.print(x) using to see if my while loop variable is reading scale value.
Wiring (using arduino MEGA2560):
PUL+ and DIR+ ---- +5V;
PUL- and DIR- ---- 13 and 4 pin.
Scale pins are: DOUT - 40; SCK - 41;
Maybe there is a way how to do it in other way or I dont know something?
#include <Wire.h>
#include "HX711.h"
#define PULS_Pin 13;
#define PULSminus_Pin 12;
#define DIR_Pin 4;
#define DIRminus_Pin 10;
#define DOUT 40
#define SCK 41
// SDA SCL(SCK)
HX711 scale(DOUT, SCK);
float seed = 206.41; // scale calibration
float x = scale.get_units(1);
int const echoPin = 2; //ultrasonic sensor
int const trigPin = 3;
int duration, distance;
const int buttonPin = 8; //button
int buttonState = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(13, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
digitalWrite(13, LOW);
digitalWrite(8, LOW);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
scale.set_scale(seed);
scale.tare();
}
void loop() {
x = scale.get_units(1);
buttonState = digitalRead(buttonPin);
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (buttonState == HIGH && distance <= 10 && distance >= 0) {
[color=red]while (x <= 600 )
{
Serial.print(x);
Serial.print(" ");
x = scale.get_units(1); //I think this place is stopping motor speed
Serial.print(x);
Serial.print(" ");
digitalWrite(4, HIGH); //servo motor rotation direction and start
delayMicroseconds(51);
digitalWrite(13, HIGH);
delayMicroseconds(0.85);
digitalWrite(13, LOW);
delayMicroseconds(0.85);
Serial.println(" rotating");
}[/color]
while (x > 600) {
Serial.print(x && " ");
Serial.println("Done");
delay(5000);
break;
}
}
else {
Serial.println("default");
delay(500);
}
}