Skip to content
Snippets Groups Projects
Commit 2ed11266 authored by Frederic Aust's avatar Frederic Aust
Browse files

Added Project WaagenTest und cocktailmaschine - cocktailmaschine kann ms pro...

Added Project WaagenTest und cocktailmaschine - cocktailmaschine kann ms pro Pumpe annehmen und das aktuelle Gewicht ausgeben
parent 401ba858
No related branches found
No related tags found
No related merge requests found
//The library used for arduino https://github.com/bogde/HX711<br>// LCD can also be used instead of serial
#include "HX711.h"
// HX711.DOUT - pin 10
// HX711.PD_SCK - pin 11
HX711 scale;
void setup()
{
scale.begin(10, 11); // parameter "gain" is ommited; the default value 128 is used by the library library*/
Serial.begin(38400);
Serial.println("HX711 Demo");
Serial.println("Before setting up the scale:");
Serial.print("read: \t\t ");
//Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average:\t\t ");
//Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t ");
//Serial.println(scale.get_value(5));
// print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t ");
// Serial.println(scale.get_units(5), 1);
// print the average of 5 readings from the ADC minus tare weight (not set) divided by the SCALE parameter (not set yet) </p><p> scale.set_scale(2280.f);
// this value is obtained by calibrating the scale with known weights; see the README for details
// scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
//Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average:\t\t ");
//Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t ");
// print the average of 5 readings from the ADC minus the tare weight, set with tare()
//Serial.println(scale.get_value(5));
Serial.print("get units: ");
//Serial.println(scale.get_units(5), 1);
// print the average of 5 readings from the ADC minus tare weight, divided by the SCALE parameter set with set_scale
Serial.println("Readings:");
}
void loop()
{
Serial.print("Weight :");
Serial.println(scale.get_units(10), 2);
/* ---------Weight in terms of KG-------------*/
Serial.println(String(scale.get_units()*0.1,1 ));
Serial.print("KG");
scale.power_down(); // put the ADC in sleep mode
delay(1000);
Serial.println("Done");
scale.power_up();
}
//The library used for arduino https://github.com/bogde/HX711<br>// LCD can also be used instead of serial
#include "HX711.h"
// HX711.DOUT - pin 10
// HX711.PD_SCK - pin 11
HX711 scale(10, 11); // parameter "gain" is ommited; the default value 128 is used by the library library*/void setup()
{
Serial.begin(38400);
Serial.println("HX711 Demo"); Serial.println("Before setting up the scale:");
Serial.print("read: \t\t ");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average:\t\t ");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t ");
Serial.println(scale.get_value(5));
// print the average of 5 readings from the ADC minus the tare weight (not set yet)
Serial.print("get units: \t\t ");
Serial.println(scale.get_units(5), 1);
// print the average of 5 readings from the ADC minus tare weight (not set) divided by the SCALE parameter (not set yet) scale.set_scale(2280.f);
// this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
Serial.println("After setting up the scale:");
Serial.print("read: \t\t");
Serial.println(scale.read()); // print a raw reading from the ADC
Serial.print("read average:\t\t ");
Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
Serial.print("get value: \t\t ");
// print the average of 5 readings from the ADC minus the tare weight, set with tare()
Serial.println(scale.get_value(5));Serial.print("get units: ");
Serial.println(scale.get_units(5), 1);
// print the average of 5 readings from the ADC minus tare weight, divided by the SCALE parameter set with set_scale
Serial.println("Readings:");
}void loop()
{
Serial.print("Weight :");
/* ---------Weight in terms of KG-------------*/
Serial.print(scale.get_units()*0.1 , 1);
Serial.print("KG"); scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();
}
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include "HX711.h"
// HX711 circuit wiring
const int LOADCELL_DOUT_PIN = A1;
const int LOADCELL_SCK_PIN = A0;
HX711 scale;
// called this way, it uses the address specified with the soldered id (in this case just A0 is soldered)
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// Depending on your servo make, the pulse width min and max may vary, you
// want these to be as small/large as possible without hitting the hard stop
// for max range. You'll have to tweak them as necessary to match the servos you
// have!
#define SERVOMIN 150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096)
#define USMIN 600 // This is the rounded 'minimum' microsecond length based on the minimum pulse of 150
#define USMAX 2400 // This is the rounded 'maximum' microsecond length based on the maximum pulse of 600
#define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates but in this case it's 60 Hz
#define NUM_PUMPS 3
#define PIN_AIR_PUMP 13
#define PIN_PSU 12
bool boot = true;
bool waage_einstellen = false;
bool serving = false;
String input = "";
uint8_t servonum = 0;
unsigned long pumpTimer[NUM_PUMPS]; //pumps
unsigned long empty = 0;
float magic = 461.3;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("######### Started #########");
// Init SCALE ##########
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
initScale();
//######################
// init Servos
pwm.begin();
pwm.setPWMFreq(SERVO_FREQ); // Analog servos run at ~60 Hz updates
delay(10);
//turn pumps in start position closed
for (int i = 0; i < NUM_PUMPS; i++)
{
openPump(i, 1000);
//closePump(i);
}
// power on PSU
pinMode(PIN_PSU, OUTPUT);
digitalWrite(PIN_PSU, HIGH);
//init air pump
pinMode(PIN_AIR_PUMP, OUTPUT);
digitalWrite(PIN_AIR_PUMP, LOW);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop()
{
// Check if any pump reached it's limit
unsigned long now = millis(); //get current timestamp
bool keepPumping = false;
for (uint8_t i = 0; i < NUM_PUMPS; i++)
{
if (pumpTimer[i] > 0 and pumpTimer[i] <= now)
{
pumpTimer[i] = 0;
closePump(i);
}
if (pumpTimer[i] > 0)
{
keepPumping = true;
}
}
// if weightlimit reached stop pummping
if (scale.is_ready()) {
long reading = scale.read();
//Serial.print("HX711 reading: ");
//Serial.println(reading);
Serial.println(String(getWeight(-reading)));
}
if (keepPumping == false)
{
deactivateAirPump();
}
if (Serial.available())
{
String input = Serial.readString();
//for Debugging
Serial.println("Got input: " + input);
// TODO get ';' separated line and returns array
Serial.println(" Convert from String Object to String.");
// https://forum.arduino.cc/index.php?topic=387175.0
int laenge = input.length();
char sz[laenge];
char buf[sizeof(sz)];
input.toCharArray(buf, sizeof(buf));
char *p = buf;
char *str;
int i = 0;
unsigned long pumpMS[NUM_PUMPS];
while ((str = strtok_r(p, ";", &p)) != NULL) // delimiter is the semicolon
{
Serial.println(str);
pumpMS[i] = strtoul(str, NULL, 10); // := milliseconds
i++;
}
Serial.println("done");
for (uint8_t i = 0; i < NUM_PUMPS; i++)
{
if (pumpMS[i] > 0)
{
openPump(i, pumpMS[i]);
}
else
{
closePump(i);
}
}
}
delay(10); // just chilling a bit to reduce power consumtion (perhaps)
}
void demoMode(){
bool demo = true;
String input = "";
Serial.println("Servonummer;ml");
while(demo){
if(Serial.available()){
input = Serial.readString();
//split(input,';');
setServoToDegree(0,90);
}
}
mainMenue();
}
void mainMenue()
{
Serial.println("Modus auswählen:");
Serial.println(" - Waage einstellen: 1");
Serial.println(" - Cocktails: 2");
Serial.println(" - Demo: 3");
boot = true;
loop();
}
void closePump(uint8_t servo)
{
setServoToDegree(servo, 20); // tubes are pressed
Serial.println("Servo: "+String(servo)+" closed");
}
void openPump(uint8_t servo, unsigned long milliseconds)
{
activateAirPump();
setServoToDegree(servo, 100); //tubes are free
pumpTimer[servo] = millis() + milliseconds; // here save current timestamp + seconds the tube should be open
Serial.println("Servo: "+String(servo)+" opened for "+String(milliseconds));
}
void activateAirPump()
{
// TODO to be implemented
Serial.println("Air Pump activated");
digitalWrite(PIN_AIR_PUMP, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
}
void deactivateAirPump()
{
// TODO to be implemented
//Serial.println("Air Pump deactivated");
digitalWrite(PIN_AIR_PUMP, LOW);
digitalWrite(LED_BUILTIN, LOW);
}
void setServoToDegree(uint8_t servo,int degree)
{
String s1 = "servo: "+String(servo); // it is importend to parse the int to string or else the output is trash
String s2 ="degree: "+String(degree);
Serial.println(s1);
Serial.println(s2);
long pulselength = map(degree, 0, 180, SERVOMIN, SERVOMAX);
pwm.setPWM(servo, 0, pulselength);
}
void initScale() {
unsigned long initMedian = 0;
int i = 0;
for (; i < 10; i++) {
long reading = scale.read();
initMedian += abs(reading);
delay(100);
}
empty = initMedian / i;
}
float getWeight(long scaleValue){
long diff = scaleValue- empty;
return diff/magic;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment