Hi. (I will update my program it this top post.)
This is my first sketch on this big scale and I am a newbie, so bear with me
Hope to get some help to get this project running
Descriptsion:
Conveyor belt is always running
bottle barrier is closed so bottels stops at the barrier.
when sensor bottle 1 have count 4 bottels the piston will push the filler tubes down in the bottles
add a time delay befor the valve for co2 open close it after a delay. open filler close when filler sensor have a value,
when all 4 sensors have value activate piston to rice the filler tubes.
when piston sensor have signal open bottle barrier.
when bottle sensor 2 have count to 4 close barrier.
bottle sensor1 need to count also at the same time. so this prosess can be repeated!
sketch of the bottle line:
bottleline.jpg
Tings to fix:
FillerValve1-4 is high after startup!
Run filling only when 4 bottles have entered sensor1
//Automatic bottlefiller line,
//---------- Display-------------------
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
// --------CONSTANTS (won't change)---------------
const int BottleBarrier = 2; //pin 2 OUTPUT bottlebarrier = LOW = CLoSED
const int FillerValve1 = 3; //pin 3-6 OUTPUT fillervalve 1-4 = LOW = CLOSED
const int FillerValve2 = 4;
const int FillerValve3 = 5;
const int FillerValve4 = 6;
const int Co2Valve = 7; //pin 7 OUTPUT co2valve 1-4 = LOW = CLOSED
const int SensorFiller1 = A0; //pin A0-A4 INPUT sensor_filler 1-4 = value
const int SensorFiller2 = A1;
const int SensorFiller3 = A2;
const int SensorFiller4 = A3;
const int Piston = 11; //pin 11 OUPUT piston = LOW = piston valve for up posision
const int SensorBottle1 = 12; //pin 12 INPUT sensor_bottle1 = HIGH = bottle at sensor befor filler
const int SensorBottle2 = 13; //pin 13 INPUT sensor_bottle2 = HIGH = bottle at sensor after filler
const int SensorPiston = 8; //pin A4 INPUT sensor_piston_up = HIGH = piston UP
//DISPLAY: SDA - ANALOG Pin 4, SCL - ANALOG pin 5
//--------Delays------------------
const int PistonDelay = 2000; //time between piston start and co2 purge
const int Co2PurgeTime = 2000; //co2 purge time befor filling
const int sensorInterval = 300; // number of millisecs between sensor readings
const int SensorValue = 1000; // Value on filling sensors
//--------VARIABLES (will change)------------------
byte SensorBottle1State = LOW; // used to record whether the sensors are detecting HIGH = not detecting
byte SensorBottle2State = LOW;
byte SensorPistonState = LOW;
int SensorValue1 = 0; // variable to store the value read
int SensorValue2 = 0; // variable to store the value read
int SensorValue3 = 0; // variable to store the value read
int SensorValue4 = 0; // variable to store the value read
int Bottle1Counter = 0; // counter for the number of bottles
int Bottle1State = 0; // current state of the sensor
int LastBottle1State = 0; // previous state of the sensor
int Bottle2Counter = 0; // counter for the number of bottles
int Bottle2State = 0; // current state of the sensor
int PistonState = 0;
int LastBottle2State = 0; // previous state of the sensor
boolean runFilling = false;
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long Filltime = 0;
unsigned long previousSensorBottle1Millis = 0; //
unsigned long previousSensorBottle2Millis = 0; //
void setup()
{
lcd.begin(20,4); //change to (16,2) if using a 16x2 display
// ------- Quick 3 blinks of backlight -------------
for(int i = 0; i< 3; i++)
{
lcd.clear();
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight(); // finish with backlight on
lcd.setCursor(4,0); //Start at character 4 on line 0
lcd.print("Starter opp!");
lcd.setCursor(5,1);
lcd.print("Automatisk");
lcd.setCursor(4,2);
lcd.print("Flaskelinje");
lcd.setCursor(1,3);
lcd.print("AUL Mikrobryggeri!");
delay(3000);
pinMode(BottleBarrier, OUTPUT);
pinMode(FillerValve1, OUTPUT);
pinMode(FillerValve2, OUTPUT);
pinMode(FillerValve3, OUTPUT);
pinMode(FillerValve4, OUTPUT);
pinMode(Co2Valve, OUTPUT);
pinMode(Piston, OUTPUT);
pinMode(SensorBottle1, INPUT_PULLUP);
pinMode(SensorBottle2, INPUT_PULLUP);
pinMode(SensorPiston, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
currentMillis = millis();
updateSensorBottle1();
updateSensorBottle2();
updateFillingSensors();
Filling();
if ((Bottle1Counter == 4) && (Bottle1Counter > 0)){
Bottle1Counter = 0;
runFilling = true;
}
if (Bottle2Counter % 4 == 0){
digitalWrite(BottleBarrier, LOW);
}
else
{
digitalWrite(BottleBarrier, HIGH);
}
}
//=============================================================================
void Filling() {
if(currentMillis >= (Filltime + PistonDelay) && (currentMillis <= (Filltime + PistonDelay + Co2PurgeTime))){
digitalWrite(Co2Valve, HIGH);
lcd.clear();
lcd.setCursor(6,1);
lcd.print("Co2 utrensing!");
lcd.setCursor(7,2);
lcd.print(Co2PurgeTime);
lcd.setCursor(11,2);
lcd.print("ms");
}
else
{
digitalWrite(Co2Valve, LOW);
}
if(currentMillis >= (Filltime + PistonDelay + Co2PurgeTime) && (currentMillis <= (Filltime + PistonDelay + Co2PurgeTime + 20000))){
digitalWrite(FillerValve1, HIGH);
digitalWrite(FillerValve2, HIGH);
digitalWrite(FillerValve3, HIGH);
digitalWrite(FillerValve4, HIGH);
lcd.clear();
lcd.setCursor(6,0);
lcd.print("FYLLER!");
lcd.setCursor(1,1);
lcd.print("Fyllesensorverdi:");
lcd.setCursor(1,2);
lcd.print("1: 2: 3: 4:");
lcd.setCursor(1,3);
lcd.print(SensorValue1);
lcd.setCursor(6,3);
lcd.print(SensorValue1);
lcd.setCursor(11,3);
lcd.print(SensorValue1);
lcd.setCursor(16,3);
lcd.print(SensorValue1);
delay(100);
}
else
{
digitalWrite(FillerValve1, LOW);
digitalWrite(FillerValve2, LOW);
digitalWrite(FillerValve3, LOW);
digitalWrite(FillerValve4, LOW);
}
if (runFilling == true){
Filltime = currentMillis;
lcd.clear();
lcd.setCursor(6,0);
lcd.print("FYLLER!");
lcd.setCursor(2,1);
lcd.print("Fyllestempel ned");
digitalWrite(Piston, HIGH);
}
runFilling = false;
}
//===============================================================================
void updateFillingSensors() {
SensorValue1 = analogRead(SensorFiller1); // read the input pin
SensorValue2 = analogRead(SensorFiller2); // read the input pin
SensorValue3 = analogRead(SensorFiller3); // read the input pin
SensorValue4 = analogRead(SensorFiller4); // read the input pin
if (SensorValue1 > SensorValue)
{
digitalWrite(FillerValve1, LOW); // Filling off
}
if (SensorValue2 > SensorValue)
{
digitalWrite(FillerValve2, LOW); // Filling off
}
if (SensorValue3 > SensorValue)
{
digitalWrite(FillerValve3, LOW); // Filling off
}
if (SensorValue4 > SensorValue)
{
digitalWrite(FillerValve4, LOW); // Filling off
}
if (SensorValue1 > SensorValue) // legg til alle følere
{
digitalWrite(Piston, LOW); }
}
//===============================================================================
void updateSensorBottle1() {
if (millis() - previousSensorBottle1Millis >= sensorInterval) {
if (digitalRead(SensorBottle1) == LOW) {
SensorBottle1State = ! SensorBottle1State; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
previousSensorBottle1Millis += sensorInterval;
}
}
Bottle1State = digitalRead(SensorBottle1);
if (Bottle1State != LastBottle1State){
if (Bottle1State == HIGH){
Bottle1Counter++;
Serial.print("number of bottls sensor1 : ");
Serial.println(Bottle1Counter);
}
}
LastBottle1State = Bottle1State;
}
//===========================================================================
void updateSensorBottle2() {
if (millis() - previousSensorBottle2Millis >= sensorInterval) {
if (digitalRead(SensorBottle2) == LOW) {
SensorBottle2State = ! SensorBottle2State; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
previousSensorBottle2Millis += sensorInterval;
}
}
Bottle2State = digitalRead(SensorBottle2);
if (Bottle2State != LastBottle2State){
if (Bottle2State == HIGH){
Bottle2Counter++;
Serial.print("number of bottls filled : ");
Serial.println(Bottle2Counter);
}
}
LastBottle2State = Bottle2State;
}
I have also made a skech at
Best regards
Pål