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
/*
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
}
}
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.
// 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