Just waiting for the parts, but i needed to do something så i tried to make some coding.
Do i think right here, or do i need more studding?
Best regards
Frank
// No loop, i will use the startbutton each time.
// 12V relay (board with 8ch).
// 12V valve to co2, beerline.
// When the moisture meter hits the foam, each beer line stops.
const int button = 2; // the number of the startbutton pin
int buttonState = 0; // variable for reading the startbutton status
// int pin_switch = 2; // Not in use!
int purge_co2 = 10; // relay 1
int beer3 = 11; // relay 2
int beer1 = 13; // relay 3
int beer2 = 12; // relay 4
int rele_4 = 14; // relay 5
int relay_5_lower = 15; // relay 6
int relay_6_rais = 16; // relay 7
int soil_beer1 = A0;
int soil_beer2 = A1;
int soil_beer3 = A2;
int val;
void setup() {
Serial.begin(9600);
pinMode(button, INPUT); // start button
pinMode(relay_5_lower, OUTPUT); // lower the filler relay
pinMode(purge_co2, OUTPUT); // purge_co2 relay
pinMode(beer1, OUTPUT); // Beerline 1 relay
pinMode(beer2, OUTPUT); // Beerline 2 relay
pinMode(beer3, OUTPUT); // Beerline 3 relay
pinMode(soil_beer1, INPUT); // SoilPin to beerline 1 relay
pinMode(soil_beer2, INPUT); // SoilPin to beerline 2 relay
pinMode(soil_beer3, INPUT); // SoilPin to beerline 3 relay
pinMode(relay_6_rais, OUTPUT); // Rais the filler relay
}
void loop() {
buttonState = digitalRead(button);
if (buttonState == HIGH) {
}
digitalWrite(relay_5_lower, HIGH); // sets the digital pin 15 on
Serial.print("Lower the rail! ");
delay(3000); // waits for 2 second
digitalWrite(purge_co2, HIGH);
Serial.print("Purge with co2 ");
delay(3000); // waits for a 3 second
Serial.print("Filling 1 - 2 - 3 ");
val = digitalRead(soil_beer1);
if (val == LOW)
{
digitalWrite(beer1, LOW); //if soil moisture sensor provides LOW value send LOW value to relay
}
else
{
digitalWrite(beer1, HIGH); //if soil moisture sensor provides HIGH value send HIGH value to relay
}
val = digitalRead(soil_beer2);
if (val == LOW)
{
digitalWrite(beer2, LOW); //if soil moisture sensor provides LOW value send LOW value to relay
}
else
{
digitalWrite(beer2, HIGH); //if soil moisture sensor provides HIGH value send HIGH value to relay
}
val = digitalRead(soil_beer3);
if (val == LOW)
{
digitalWrite(beer3, LOW); //if soil moisture sensor provides LOW value send LOW value to relay
}
else
{
digitalWrite(beer3, HIGH); //if soil moisture sensor provides HIGH value send HIGH value to relay
}
digitalWrite(relay_6_rais, HIGH);
Serial.print("Raising! ");
delay(1000);
Serial.print("Finish! ");
}
first karma for posting this as the first posting.Well done code in code tags and code well formatted
Welcome to the forum!
From a first quick looking your code looks pretty good.
Of course different functionalities can make sense depending on what the programmer wants.
As a quick & dirty description your code controls some kind of a beer drawer.
If I should say If your code already does exactly what it should do would require a description in normal words or a program-procedure-plan
The basic examples in the Arduino IDE use this (IMHO) awful command delay().
Delay() can be such a DELAY in writing programs. delay() blocks the microcontroller completely. As long as you don't want to check for something like keypresses, reading sensors etc regularly in "parallel" to the delay you can use the function delay()
But as soon as you want to check for something like keypresses, reading sensors etc regularly you should use another function called millis(). Millis() is a system-variable that starts counting up milli-seconds at powerup and keeps counting up all the time until it reacheds the number 2^32 - 1. Then it will "rollover" to zero again.
2^32 - 1 milliseconds is 49,7 days. Pretty long. There is a programming-technique that will automatically care about this "rollover" so that any timing will always work the right way. Even if the timing happens "across" a rollover.
There are several tutorial about that. I want to post my personal preferred version of this timing-technique.
unsigned long DemoTimerA = 0; // variables that are used to store timeInformation
unsigned long DemoTimerB = 0;
unsigned long DemoTimerC = 0;
unsigned long DoDelayTimer = 0;
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - expireTime >= TimePeriod )
{
expireTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
void setup()
{
Serial.begin(115200);
Serial.println("Program started activate Show timestamp in serial monitor");
}
void loop()
{
if ( TimePeriodIsOver(DemoTimerA,1000) )
{
Serial.println(" TimerA overDue");
}
if ( TimePeriodIsOver(DemoTimerB,2000) )
{
Serial.println(" TimerB overDue");
}
if ( TimePeriodIsOver(DemoTimerC,3000) )
{
Serial.println(" TimerC overDue");
}
if ( TimePeriodIsOver(DoDelayTimer,20000) )
{
Serial.println("every 20 seconds execute delay(3500)... to make all other timers overdue");
delay(3500);
}
}
FNH:
Just waiting for the parts, but i needed to do something så i tried to make some coding.
I reckon it would be better to wait for the parts and then develop the code gradually testing it after every few lines of code are added. That way if you run into a problem you know exactly which lines of code have caused the problem.
Also, learn how each part is used (relay, moisture sensor etc) with separate short programs and don't try to make the composite program until you can get each of the parts to work separately. If you need help it will be much easier if the code is short and focused on a single task.
I did change the already relay with the millis without error.
And yes, i know its better to make small parts of coding because its easier to find out if something is wrong.
I think i come back when i get the parts.
But the purpose of this is to be able to fill 3 cans with beer where I weld a frame that has the ability to fill the cans at the same time. It should lower the frame (relay 6), purge the boxes with co2 (relay 1) for 2 seconds then and fill the boxes with beer (relay 2,3,4) until the beer reaches up to the moisture sensors (A0, A1, A2). For so and raise the frame (relay 7). This was the short version
I have an NANO (never used) but i did order a MEGA
It sounds like a Nano has enough pins for the job. Per Robin2's advice, if you have any of your other parts on hand you can get started. If it turns out later that you really do need the Mega, the code can be directly transferred.
const int button = 2; // the number of the startbutton pin
pinMode(button, INPUT); // start button
if (buttonState == HIGH) {
Do you have pullup or pulldown resistors on input pins to keep the pins from "floating" and causing false HIGHs or LOWs when the button is not pressed? Best way is like S3 in the diagram, no external resistor needed, logic is reversed though (pin is LOW when button is pressed).