Add counter and push button reset to arduino uno R3

I am needing to add a counter to my code which goes up one every cycle and then stops after 101 cycles. Then I need to have a push button that resets the count to zero and starts the program up again. Please help. Thanks

Freedom_Assembly.ino (2.6 KB)

Please help.

With what? Wiring up a switch? Reading when it becomes pressed? Performing some action when the switch becomes pressed? Defining what "a cycle" is?

/*
Activates 3 air cylinders. The first cycinder acts as a plunger to assemble our lid together.
The second acts as a gate to have the part stop at the correct spot to be assembled.
The third acts as a gate to hold the unassembled part back until the assembled part is dropped.
This all works off an input signal from a proximity switch when the part is in the correct spot to be assembled.

The circuit:

  • Pin 4 hooked to +5V relay switching +24V to operate air clinder 1
  • Pin 7 hooked to +5V relay switching +24V to operate air cylinder 2
  • Pin 8 hooked to +5V relay switching +24V to operate air cylinder 3
  • Prox Switch attached to pin 2 from +5V
  • 10K resistor attached to pin 2 from ground

*/

// constants won't change. They're used here to
// set pin numbers:
const int proxswitchPin = 2; // the number of the pushbutton pin
const int airval1 = 4; // the number of Plunger valve 1 pin
const int airval2 = 7; // the number of Part release after plunger valve 2 pin
const int airval3 = 8; // the number of Part release into plunger valve 3 pin

// variables will change:
int proxswitchState = 0; // variable for reading the pushbutton status

void setup() {

pinMode(airval1, OUTPUT); // initialize airval1 pin as output
pinMode(airval2, OUTPUT); // initialize airval2 pin as output
pinMode(airval3, OUTPUT); // initialize airval3 pin as output
pinMode(proxswitchPin, INPUT); // initialize the proxswitch pin as an input:
}

void loop() {
// read the state of the proxswitch value:

proxswitchState = digitalRead(proxswitchPin); // check if the prox is made.

// if it is, the proxswitchState is HIGH:
if (proxswitchState == HIGH) {
delay(100); // Delay to make sure part is in write position before assembly
digitalWrite(airval3, HIGH); // Bring cylinder up to hold second part in line in place while first one drops
digitalWrite(airval1, HIGH); // Activate Plunger
delay(50); // Delay to make sure plunger goes all the way down
digitalWrite(airval1, LOW); // Bring plunger back up
digitalWrite(airval2, HIGH); // Open gate for part to fall off chute
delay(500); // Delay to make sure part has time to fall
digitalWrite(airval2, LOW); // Close gate after part falls
delay(00); // Delay after part drop off chute to make sure gate is closed
digitalWrite(airval3, LOW); // release part to get assembled
}
else {
digitalWrite(airval2, LOW); // Leave gate open for parts to fall into plunger area
digitalWrite(airval3, LOW); // Leave gate closed so parts stop in plunger area
}
}

Sorry,

Every time a lid is assembled I need the count to increase by one until it reaches 101. I also need the loop to stop running and wait for an input from a push button that would reset the count to 0 and start the loop again. However this can been done is fine with me as long as it gets done. Obviously the simplest way would be best but any way would be fine.

Thanks

In the code block after detecting that a part is near the proximity switch add 1 to a counter variable. In the loop() function add

if (count >= 101)
  {
    while (digitalRead(resetButton) == HIGH)  //could be LOW depending on wiring
      {
      }
   count = 0
  }

Obviously you will need to declare the count variable and set up the pinMode for the reset button.

There are better ways to do this but you did say you wanted it simple.

lkusy48:
/*

*/

// constants won't change. They're used here to
// set pin numbers:
const int proxswitchPin = 2; // the number of the pushbutton pin
const int airval1 = 4; // the number of Plunger valve 1 pin
const int airval2 = 7; // the number of Part release after plunger valve 2 pin
const int airval3 = 8; // the number of Part release into plunger valve 3 pin
const int reset = 3;

// variables will change:
int proxswitchState = 0; // variable for reading the pushbutton status
int counter = 0;

void setup() {

pinMode(airval1, OUTPUT); // initialize airval1 pin as output
pinMode(airval2, OUTPUT); // initialize airval2 pin as output
pinMode(airval3, OUTPUT); // initialize airval3 pin as output
pinMode(reset, INPUT); // initialize reset pin as output
pinMode(proxswitchPin, INPUT); // initialize the pushbutton pin as an input:

}

void loop()
{
if (counter >= 5)
{
while (digitalRead(reset) == HIGH) //could be LOW depending on wiring
{
}
counter = 0;
}

proxswitchState = digitalRead(proxswitchPin); // Check if prox switch is made

if (proxswitchState == HIGH)
{
delay(100); // Delay to make sure part is in write position before assembly
digitalWrite(airval3, HIGH); // Bring cylinder up to hold second part in line in place while first one drops
digitalWrite(airval1, HIGH); // Activate Plunger
delay(50); // Delay to make sure plunger goes all the way down
digitalWrite(airval1, LOW); // Bring plunger back up
digitalWrite(airval2, HIGH); // Open gate for part to fall off chute
delay(500); // Delay to make sure part has time to fall
digitalWrite(airval2, LOW); // Close gate after part falls
delay(00); // Delay after part drop off chute to make sure gate is closed
digitalWrite(airval3, LOW); // release part to get assembled
counter ++;
Serial.write((byte*)&counter, 2);
}
}

I still can't get it to work. Its like it doesn't even recognize the counter code in the program

What do you see on the serial monitor when you print counter ?
By the way, you can print it simply using

Serial.println(counter);

Also, when posting code please use code tags (</>) rather than quote tags.

By the way, you can print it simply using

Serial.println(counter);

Even better would be:

Serial.print("counter = ");
Serial.println(counter);

so you have some idea what the random stream of numbers means.

i don't see anything on the monitor when i print counter. It's like the counter code isn't even there

i don't see anything on the monitor when i print counter. It's like the counter code isn't even there

Why are you sending binary data to an ASCII terminal? Wise up.