Tapping Machine sketch

Hi guys..
I'm projecting an automatic tapping machine for beer bottle; for my home production.
This is the code I have developed.
The functioning is based on a pneumatic cylinder drived by an electrovalve.
The bottles are brought under the cylinder by a conveyor belt that is moved by a 24v motor.

Here you are the code.
It's the first time I try to implement the debouncing of the button..can you control it?

Thanks!!

const int MotAvantiPin = 11; // Motor of the conveyor belt 
const int Tappata = 12; // Pneumatic tapping cylinder
const int buttonPin = 3; // start/stop button
const int tx = 2; // infrared emitter
const int rx = 4; // infrared receiver

int sensorValue;  
int buttonState; 
int lastButtonState = LOW;

long lastDebounceTime = 0;
long debounceDelay = 50;


void setup() 
{ 
  pinMode(buttonPin, INPUT);
  pinMode(Tappata, OUTPUT);
  pinMode(MotAvantiPin, OUTPUT);
  pinMode (tx,OUTPUT);
  pinMode (rx,INPUT); 

  Serial.begin(9600);  
  digitalWrite(tx,HIGH);
  digitalWrite(Tappata,LOW);
  digitalWrite(MotAvantiPin, HIGH); // The belt start running 
  delay(1000);
  digitalWrite(MotAvantiPin, LOW); // The belt stop running
  Serial.println("Put bottles in the belt"); 
} 


void loop() { 

  int currButtonState = digitalRead(buttonPin);

  if (currButtonState != lastButtonState)  
  {
    lastDebounceTime = millis(); 
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (currButtonState != buttonState);
    {
      buttonState = currButtonState;


      if (buttonState == HIGH); 

      { 
        digitalWrite(MotAvantiPin, HIGH); // if the button is pressed the conveyor belt start
        sensorValue = analogRead(rx); // read the infrared receiver
        if (sensorValue >500)
        { // infrared barrier interrupted (there is the bottle)  

          digitalWrite(MotAvantiPin, LOW); // belt stop 
          Serial.println("Tapping Bottle"); 
          delay(5000); 
          digitalWrite(Tappata, HIGH); // the cylinder goes down
          delay(1000);
          digitalWrite(Tappata, LOW); // the cilynder return at the starting place
          delay(3000);
          Serial.println("Nastro Avanti-nuova bottiglia"); 
          digitalWrite(MotAvantiPin, HIGH); // Belt re-start running
        } 
      } 
    }
  }
  else {
    // If button not pressed Motor is off:
    digitalWrite(MotAvantiPin, LOW); 
  }
}

It's the first time I try to implement the debouncing of the button.

That code looks like the crappy debounce example.

  if (reading != lastButtonState)

There is nothing in these names that implies that they are related. Now, currButtonState and lastButtonState DO look like they are related.

Beyond that,
I stopped reading.
The code bouncing all
over the place
was giving
me
a headache.

If you can't indent properly as you write the code, use the Tools + Auto Format option before you post code.

So..completely wrong debouncing!
Sorry for the headache!!!
Can you give me some suggestions?

So..completely wrong debouncing!

I didn't say that. I said I couldn't tell because you appear to be comparing unrelated things and because your code is too hard to read.

Can you give me some suggestions?

I might be mistaken, but I thought I had made some.

I have used the auto format option..now it should be more easy to read!!
Sorry for the inconvenience!!

  if (reading != lastButtonState)

There is nothing in these names that implies that they are related. Now, currButtonState and lastButtonState DO look like they are related.

  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState)

They are related..lastButtonState is set to low.

but nothing has changed!

It most certainly has. The functionality of the program has not changed, but the piss-poor indenting has been fixed. (Thank you for that!)

They are related..

As a 10 year old, introducing my 11 year old sister, we had the same last name. As a 57 year old, introducing my sister, we no longer have the same last names. But, we are still related.

Looking at our names on a list of names, you, without knowing either of us, would not know that. Looking at reading and lastButtonState, you know that they are related. I do not.

Looking at currButtonState and lastButtonState, I can see that they are related. If you don't want to use names that makes the relationship APPARENT, that's fine. Just don't expect me to read the code.

Ok! I have understood what you meant!! :slight_smile:
Now I think it's fixed!
Thank you!!

const int MotAvantiPin = 11; // Motor of the conveyor belt 
const int Tappata = 12; // Pneumatic tapping cylinder
const int buttonPin = 3; // start/stop button
const int tx = 2; // infrared emitter
const int rx = 4; // infrared receiver

int sensorValue;  
int buttonState; 
int lastButtonState = LOW;

long lastDebounceTime = 0;
long debounceDelay = 50;


void setup() 
{ 
  pinMode(buttonPin, INPUT);
  pinMode(Tappata, OUTPUT);
  pinMode(MotAvantiPin, OUTPUT);
  pinMode (tx,OUTPUT);
  pinMode (rx,INPUT); 

  Serial.begin(9600);  
  digitalWrite(tx,HIGH);
  digitalWrite(Tappata,LOW);
  digitalWrite(MotAvantiPin, HIGH); // The belt start running 
  delay(1000);
  digitalWrite(MotAvantiPin, LOW); // The belt stop running
  Serial.println("Put bottles in the belt"); 
} 


void loop() { 

  int currButtonState = digitalRead(buttonPin);

  if (currButtonState != lastButtonState)  
  {
    lastDebounceTime = millis(); 
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (currButtonState != buttonState);
    {
      buttonState = currButtonState;


      if (buttonState == HIGH); 

      { 
        digitalWrite(MotAvantiPin, HIGH); // if the button is pressed the conveyor belt start
        sensorValue = analogRead(rx); // read the infrared receiver
        if (sensorValue >500)
        { // infrared barrier interrupted (there is the bottle)  

          digitalWrite(MotAvantiPin, LOW); // belt stop 
          Serial.println("Tapping Bottle"); 
          delay(5000); 
          digitalWrite(Tappata, HIGH); // the cylinder goes down
          delay(1000);
          digitalWrite(Tappata, LOW); // the cilynder return at the starting place
          delay(3000);
          Serial.println("Nastro Avanti-nuova bottiglia"); 
          digitalWrite(MotAvantiPin, HIGH); // Belt re-start running
        } 
      } 
    }
  }
  else {
    // If button not pressed Motor is off:
    digitalWrite(MotAvantiPin, LOW); 
  }
}

You now have three variables that are used to test whether this reading is the same as the reading last time. Why do you need three? This reading is either the same as the last one, or it isn't.

PaulS:
You now have three variables that are used to test whether this reading is the same as the reading last time. Why do you need three? This reading is either the same as the last one, or it isn't.

I try to insert in my code this debouncing http://arduino.cc/en/Tutorial/Debounce

I try to insert in my code this debouncing...

See reply #1. That tutorial is just about the worst example provided.

PaulS:

I try to insert in my code this debouncing...

See reply #1. That tutorial is just about the worst example provided.

What about this way?
Do you think it could work?

const int buttonPin = 14;    // the number of the pushbutton pin
const int MotorPin = 9;      // the number of the LED pin

// Variables will change:
int MotorState = LOW;         // the current state of the output pin
int buttonState = LOW;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 250;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(MotorPin, OUTPUT);

  // set initial LED state
  digitalWrite(MotorPin, MotorState);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if( (millis() - lastDebounceTime) > debounceDelay) {

    if ( (buttonState == HIGH) && (MotorState = LOW) ){
      digitalWrite(MotorPin,HIGH);
      MotorState = HIGH;
      lastDebounceTime = millis();
    }
    if ( (buttonState == HIGH) && (MotorState = HIGH) ){
      digitalWrite (MotorPin,MotorState);
      MotorState = HIGH;
      lastDebounceTime = millis();
    } 
    if ( (buttonState ==LOW) && (MotorState = LOW) ){
      digitalWrite(MotorPin,MotorState);
      MotorState = LOW;
      lastDebounceTime = millis(); 
    } 
    if ( (buttonState ==LOW) && (MotorState = HIGH) ){
      digitalWrite(MotorPin,LOW);
      MotorState = LOW;
      lastDebounceTime = millis();    
    }   
  }
}