WonderingI'm wondering if someone could have a look at my code and let me know if there's something wrong with it. I I had the code done for me a while back and did a quick test and it seemed to work. I I have now built a pneumatic machine for it to work on, and it's doing some strange things.
The first relay turns on and off a pneumatic solenoid, this is connected to a small pneumatic cylinder, it moves in and out. This This cylinder causes a pneumatic feeder to activate and move back and forth also.
The solenoid turns on for approx half a second and then back off again, this pushes the small cylinder out then back in. This sends the feeder forward, it then turns around and comes back toward its resting position. The arduinoArduino waits for the signal from the proximity sensor to tell it that the feeder is back in its resting position before doing it again.
After 20 sub cycles, the arduinoArduino turns on the second relay for about a second, before turning it back off again.
I just bought a new board and tried again, but now it counts to about five subcycles and then jumps the gun and doesn't wait for the signal from the proximity sensor before doing the next sub cycle. So once again the count ends up wrong. Any
Any ideas on what iI could try?
int Relay_1 = 10; //first Relay
int Relay_2 = 11; //second Relay
int Prox_Input = 12; //input signal from proximity sensor
int Switch_Input = 13; //the switch
int Cycle_Number = 20; //number of repeating number of "complete" cycle
int Sub_Cycle_Number = 20; //number of repeating number of "sub" cycle
int Delay_Cycle=1000;Delay_Cycle = 1000; //delay of the "Complete" Cycle in milleseconds
int Delay_Sub_Cycle=500;Delay_Sub_Cycle = 500; //delay of the "Sub" Cycle in milliseconds
bool Button_Pushed = true ; //the state required for the button
int i,j; j;
void setup() {
/*configurate the output relays to be begin "low"*/
pinMode(Relay_1 , OUTPUT);
digitalWrite(Relay_1, LOW);
pinMode(Relay_2 , OUTPUT);
digitalWrite(Relay_2 , LOW);
/*===========================================================*/
/*configurate the input switch and sensor*/
pinMode(Prox_Input , INPUT);
pinMode(Switch_Input , INPUT_PULLUP);
/*===========================================================*/
/*configurate Serial port if you want it*/
Serial.begin(9600);
/*===========================================================*/
}
void loop() {
if (Check_Button() == Button_Pushed) { //chechcheck the button
{
for (i=1i ;= i<=Cycle_Number1 ; i++) i <= Cycle_Number ; i++) { //the loop of the "complete" Cycle
{
for (j=1j ;= j<=Sub_Cycle_Number1 ; j++) j <= Sub_Cycle_Number ; j++) { //the loop of the "sub" Cycle
{
digitalWrite(Relay_1 , HIGH);
delay(Delay_Sub_Cycle);
digitalWrite(Relay_1 , LOW);
while (digitalRead(Prox_Input) == HIGH) {} // waiting the sensor signal
}
digitalWrite(Relay_2 , HIGH);
delay(Delay_Sub_Cycle);
digitalWrite(Relay_2 , LOW);
delay(Delay_Cycle);
}
}
}
bool Check_Button()
{
int Reading_1;
int Reading_2;
Reading_1 = digitalRead(Switch_Input);
delay(20);
Reading_2 = digitalRead(Switch_Input);
if ((Reading_1 == 1) && (Reading_2 == 1))
return true;
else
return false;
}
