Need help about response time of reed switch input


I have a piston mechanism(pneumatic) controlling by Arduino Mega which i want to limit it’s stroke via magnetic reed sensor switch.

Sensor is working and system is also working( with many relay circuit of course).

Problem is in my code and i need your help if you can.

I can not post the code with code tags because of my company’s very interesting internet blocking software.

#include <EEPROM.h> //EEPROM uses to save last values into the Arduino

const int valfdort = 5; //define pin values for the valves
const int valfuc = 6;
const int valfiki = 7;
const int valfbir = 8;
const int sensorbir= A0;
int i = 0;
int j = 0;
int counter=0;
int besbin=1;
int cycle=1;
void setup()
Serial.begin(9600); //initialize serial window
pinMode(valfbir,OUTPUT); //set valve pins as outputs
pinMode(sensorbir,INPUT); //set reed swtich as input
void loop()
counter= 180i + j;
if (besbin!=0)
Serial.print("Current Cycle is: ");
delay(2500); //needs to be check!!!
j=j+1; //This calculation neccesary because EEPROM memory can store max 4096bytes.
EEPROM.write(0,i); //Save current i value to address “0”
EEPROM.write(1,j); //Save current j value to address “1”
besbin=counter%5000; //Calculate mod value of counter
Serial.print("Current loop: ");
Serial.println(counter); //Prints current loop
//Check if a cycle is finished
Serial.println(“Check the test rig”);
cycle=cycle+1; //Loop processed 5000 times,Go on to next cycle
Serial.println(“Test is Finished!”);

I needed to give some pace to my code beucase my piston working fastly and before arduino read sensor input piston goes end of it’s stroke already. %98 percent i am sure it is about my code.

Maybe i can save the sensor input into the random variable?

Thanks for your advices.

If you're using a reed switch I suspect it will never be fast enough. You'd probably be better off going for a hall effect transistor. The response time would be far better. (no moving parts).

Sorry about your interesting web security issue. I know of a plane that was grounded for over 24 hours because a request for a spare part had been put into quarantine. (apparently the part required included the word "black") :)

Nsitu: I needed to give some pace to my code beucase my piston working fastly

If you want your code to be faster than the piston, you'd have to avoid two things: - delay() - slow function calls

Any "delay()" will block the program execution for the given amount of time.

And slow functions like reading from and writing to EEPROM will take the time they need for themselves.

You cannot write a fast program when blocking your program ("delay") or using slow devices (EEPROM) at the same time.

I'm afraid that a pneumatic cylinder stroke can not be controlled (perhaps a double effect one; I'm not sure)


@KenF I think you are right, it seems not possible to execute as fast as i desire. BTW your story was awesome :))

@jurs These are important informations for me thank you very much.But i already deleted delay effect by adding "//" before on it. And EEPROM variables is not used actually they are just to see current cylcle.

@vffgaston I am using double effect piston.

Thank you very much.

@vffgaston I am using double effect piston.

What is the cylinder moving?. Can you get the same effect by hand?


@Nsitu: I see where you've made over 30 posts, but still don't post your code in the proper manner. Please read Nick Gammon's posting rules at the top of this Forum. It will probably help get your problems resolved quicker.

@econjack I am very happy because you are helping me to get my answer quicker. What a thoughtful behavior.

But did not see my message about why i posted my code without tags? I will edit it when i turn back to my home.

Note: If you really saw my over 30 message you had to see this is my second wrong posted code.First one was because lack of experince and second one is this and please see my reason message for this one.