Coffee machine coding

hello

I have this code , and i went to change from using button to 3 Capacitive Touch
sensor with these options ;
1st Switch:

  • Gives an option of 1tsp scale (5ml)

2nd Switch:

  • Gives an option of 1tbsp scale (15ml)

3rd Switch: (Cup Scale)
This have 3 options inside of it,

  • 1/4 Cup (62.5 ml)
  • 1/2 Cup (125 ml)
  • 1 Cup (250 ml)

how can I build it ?


#include <LiquidCrystal_I2C.h> 
#include <Servo.h>
Servo servo;
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);


int z = 1;
int led1 = 2;
int led2 = 3;
int led3 = 4;
int filled_led = 5;
int sensor = 6;
// servo = 7
int pump = 8;
int mode = 9;
int buzzer = 10;
int start = 11;


 
void setup() 
{

  // initialize the LCD
  lcd.begin();
  // Turn on the blacklight and print a message.
 lcd.backlight();
 tone(buzzer, 500);
  delay(100);
  noTone(buzzer);
  
   lcd.setCursor(0,0); // column, row
  lcd.print("     Welcome    ");
  lcd.setCursor(0,1); // column, row 
  lcd.print(" Experiment UAE ");
  delay(3500);
  
  lcd.setCursor(0,0); // column, row
  lcd.print(" Pow_Fill ver.1 ");
  lcd.setCursor(0,1); // column, row
  lcd.print("      2020      ");
  delay(2500);
   if(digitalRead(sensor) == HIGH)
 tone(buzzer, 500);
  delay(100);
  noTone(buzzer);
                                                                                                                                                                       
   
 lcd.clear();
 pinMode(start, INPUT_PULLUP);
 pinMode (led1,OUTPUT);
 pinMode (led2,OUTPUT);
 pinMode (led3,OUTPUT);
  pinMode(pump, OUTPUT);
 pinMode (filled_led,OUTPUT);
 pinMode (buzzer,OUTPUT);
 pinMode (sensor, INPUT);
 servo.attach(7);
 servo.write(180);

}
void loop()
{

   int button = digitalRead(mode);
  
  if (button == HIGH)
  {
 tone(buzzer, 500);
  delay(150);
  noTone(buzzer);
  {
    if(z<3)
    z ++;
    else
    z=1;
  }
  }
////////////////////////////////////////////////MODE 1
  
  if 
  (z==1)
  
  {
  digitalWrite (led2,LOW);
  digitalWrite (led3,LOW);
  digitalWrite (led1,HIGH);
   lcd.setCursor(0,0); // column, row
  lcd.print(" PACK 1 #  25Gr"); 
  }
  while(digitalRead(start) == LOW)
{
  if(digitalRead(sensor) == LOW)
  {
      lcd.setCursor(0,1); // column, row
  lcd.print("  Press Start   ");
  }
 if(digitalRead(sensor) == HIGH)
  {
   lcd.setCursor(0,1); // column, row
  lcd.print("   Place Bag    ");
  
  }

  
  if (digitalRead (button) == HIGH)
  break;
}
  ///////////////////////////////////////////////MODE 2
  
  if 
  (z==2)
  {
  digitalWrite (led2,HIGH);
  digitalWrite (led3,LOW);
  digitalWrite (led1,LOW);
   lcd.setCursor(0,0); // column, row
  lcd.print(" PACK 2 #  50Gr"); 
  }
  while(digitalRead(start) == LOW)
{
  if(digitalRead(sensor) == LOW)
  {
       lcd.setCursor(0,1); // column, row
  lcd.print("  Press Start   ");
  }
 if(digitalRead(sensor) == HIGH)
  {
   lcd.setCursor(0,1); // column, row
  lcd.print("   Place Bag    ");
  }

  
  if (digitalRead (button) == HIGH)
  break;
}

//////////////////////////////////////////////////MODE 3
  
   if 
  (z==3
    
  )
  {
  digitalWrite (led2,LOW);
  digitalWrite (led3,HIGH);
  digitalWrite (led1,LOW);
   lcd.setCursor(0,0); // column, row
  lcd.print(" PACK 3 #  75Gr"); 
  }
  while(digitalRead(start) == LOW)
{
  if(digitalRead(sensor) == LOW)
  {
     lcd.setCursor(0,1); // column, row
  lcd.print("  Press Start   ");
  }
 if(digitalRead(sensor) == HIGH)
  {
   lcd.setCursor(0,1); // column, row
  lcd.print("   Place Bag    ");
  
  }

  
  if (digitalRead (button) == HIGH)
  break;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
    delay(200);


     if(digitalRead(start) == HIGH && z==1 && digitalRead(sensor) == LOW ) 
{
 tone(buzzer,500);
  delay(100);
  noTone(buzzer);
   lcd.setCursor(0,1); // column, row
  lcd.print("   Filling... ");
  digitalWrite (pump,HIGH);
   //<<<=========================================<<< To Calibrate MODE 1, Change this Value
 servo.write(30);
 delay(1000);
  digitalWrite (pump,LOW);
  servo.write(180);

 
  
  lcd.setCursor(0,1); // column, row
  lcd.print("Packing Complete");
  
  
  digitalWrite (filled_led,HIGH);
  delay(400);
  tone(buzzer, 500);
  delay(300);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(150);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(300);
  noTone(buzzer);
  delay(1000);
  digitalWrite (filled_led,LOW);
  
  while(digitalRead(sensor) == LOW) 
  {
    if(digitalRead(start) == HIGH)
    {
  lcd.setCursor(0,1); // column, row
  lcd.print(" Place New Bag  ");
   tone(buzzer, 500);
  delay(100);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(100);
  noTone(buzzer);
  
    }
if(digitalRead(mode) == HIGH)
    {
  lcd.setCursor(0,1); // column, row
  lcd.print("   Remove BAG   ");
   tone(buzzer, 500);
  delay(200);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(200);
  noTone(buzzer);
  
    }
    
 if(digitalRead(sensor) == HIGH)
 break;
}
 delay(200);
}
if(digitalRead(start) == HIGH && z==1 && digitalRead(sensor) == HIGH ) 

{
   lcd.setCursor(0,1); // column, row
  lcd.print(" Error! No BAG ");
   tone(buzzer, 500);
  delay(1000);
  noTone(buzzer);

}

 
if(digitalRead(start) == HIGH && z==1 && digitalRead(sensor) == HIGH ) 

{
   lcd.setCursor(0,1); // column, row
  lcd.print("   Place Bag    ");
   tone(buzzer, 500);
  delay(1000);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(1000);
  noTone(buzzer);

}



     if(digitalRead(start) == HIGH && z==2 && digitalRead(sensor) == LOW ) 
{
 tone(buzzer,500);
  delay(100);
  noTone(buzzer);
   lcd.setCursor(0,1); // column, row
  lcd.print("   Filling... ");
  digitalWrite (pump,HIGH);
   //<<<=========================================<<< To Calibrate MODE 1, Change this Value
 servo.write(30);
 delay(2000);
  digitalWrite (pump,LOW);
  servo.write(180);


  
  lcd.setCursor(0,1); // column, row
  lcd.print("Packing Complete");
  
  
  digitalWrite (filled_led,HIGH);
  delay(400);
  tone(buzzer, 500);
  delay(300);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(150);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(300);
  noTone(buzzer);
  delay(1000);
  digitalWrite (filled_led,LOW);
  
  while(digitalRead(sensor) == LOW) 
  {
    if(digitalRead(start) == HIGH)
    {
  lcd.setCursor(0,1); // column, row
  lcd.print(" Place New Bag  ");
   tone(buzzer, 500);
  delay(100);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(100);
  noTone(buzzer);
  
    }
if(digitalRead(mode) == HIGH)
    {
  lcd.setCursor(0,1); // column, row
  lcd.print("   Remove Bag   ");
   tone(buzzer, 500);
  delay(200);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(200);
  noTone(buzzer);
  
    }
    
 if(digitalRead(sensor) == HIGH)
 break;
}
 delay(200);
}
if(digitalRead(start) == HIGH && z==2 && digitalRead(sensor) == HIGH ) 

{
   lcd.setCursor(0,1); // column, row
  lcd.print(" Error! No Bag ");
   tone(buzzer, 500);
  delay(1000);
  noTone(buzzer);

}

 
if(digitalRead(start) == HIGH && z==2 && digitalRead(sensor) == HIGH ) 

{
   lcd.setCursor(0,1); // column, row
  lcd.print("   Place Bag    ");
   tone(buzzer, 500);
  delay(1000);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(1000);
  noTone(buzzer);

}



     


     if(digitalRead(start) == HIGH && z==3 && digitalRead(sensor) == LOW ) 
{
tone(buzzer,500);
  delay(100);
  noTone(buzzer);
   lcd.setCursor(0,1); // column, row
  lcd.print("   Filling... ");
  digitalWrite (pump,HIGH);
   //<<<=========================================<<< To Calibrate MODE 1, Change this Value
 servo.write(30);
 delay(3000);
  digitalWrite (pump,LOW);
  servo.write(180);


  
  lcd.setCursor(0,1); // column, row
  lcd.print("Packing Complete");
 
  
  digitalWrite (filled_led,HIGH);
  delay(400);
  tone(buzzer, 500);
  delay(300);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(150);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(300);
  noTone(buzzer);
  delay(1000);
  digitalWrite (filled_led,LOW);
  
  while(digitalRead(sensor) == LOW) 
  {
    if(digitalRead(start) == HIGH)
    {
  lcd.setCursor(0,1); // column, row
  lcd.print(" Place New Bag  ");
   tone(buzzer, 500);
  delay(100);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(100);
  noTone(buzzer);
  
    }
if(digitalRead(mode) == HIGH)
    {
  lcd.setCursor(0,1); // column, row
  lcd.print("   Remove Bag   ");
   tone(buzzer, 500);
  delay(200);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500);
  delay(200);
  noTone(buzzer);
  
    }
    
 if(digitalRead(sensor) == HIGH)
 break;
}
 delay(200);
}
if(digitalRead(start) == HIGH && z==3 && digitalRead(sensor) == HIGH ) 

{
   lcd.setCursor(0,1); // column, row
  lcd.print(" Error! No Bag ");
   tone(buzzer, 500);
  delay(1000);
  noTone(buzzer);

}

 
if(digitalRead(start) == HIGH && z==3 && digitalRead(sensor) == HIGH ) 

{
   lcd.setCursor(0,1); // column, row
  lcd.print("   Place Bag    ");
   tone(buzzer, 500);
  delay(1000);
  noTone(buzzer);
  delay(30);
   tone(buzzer, 500 );
  delay(1000);
  noTone(buzzer);

}
}

What exactly are you needing help with.


BTW
In the Arduino IDE, use Ctrl T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

You should never ever use delay() in your sketches; well not until you understand why you shouldn’t use it.


What kind of sensors are we talking about.


Let’s say your code is using a potentiometer just for prototyping and testing.
Let’s say in the final hardware you will then use a photo sensor, pressure sensor or temperature sensor.
You can simple exchange the potentiometer directly when the photo sensor etc.

Deleting your posts is considered poor form.

what do u mean ?

Im using Capacitive Touch sensor

You appear to be posting your questions about the same project multiple times.

sorry . i’m trying to delete ii

Adafruit has good reference about capacitive touch switches/sensors.

As @LarryD pointed out, delay() should be avoided…

In this case, most of the delays are used in making simple tone effects. Spend a few moments, and rip that apart into a separate function, which you call to start and maintain the tones using millis()… then a simple call will do everything you want without blocking.

Forum Etiquette is another thing, well leave that to you.

Capacitive Sensors is a large field… it could be a piece of tin foil with a wire, or a standalone module that rejects noise, and outputs serial strings for each button.
My own tinfoil hat is blocking me from seeing what you have.

Finally it might be an idea to break your program up into functions generally, and document those pieces so we can follow what’s going on. Then Ctrl-T and post in code tags

My tutorials on How to write Timers and Delays in Arduino and
Simple Multi-tasking in Arduino may help you re-organize your code in to individual tasks
multitaskingDiagramSmall

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.