Hello,
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(valfiki,OUTPUT);
pinMode(valfuc,OUTPUT);
pinMode(valfdort,OUTPUT);
pinMode(sensorbir,INPUT); //set reed swtich as input
}
//---------------------------------------------------------------------//
void loop()
{
//Counter
i=EEPROM.read(0);
j=EEPROM.read(1);
counter= 180i + j;
//Cycle
if (besbin!=0)
{
Serial.print("Current Cycle is: ");
Serial.println(cycle);
digitalWrite(valfiki,HIGH);
digitalWrite(valfbir,LOW);
delay(2500); //needs to be check!!!
digitalWrite(valfdort,HIGH);
digitalWrite(valfuc,LOW);
//delay(2500);
if(analogRead(sensorbir))
{
digitalWrite(valfiki,LOW);
digitalWrite(valfbir,HIGH);
//delay(2500);
digitalWrite(valfdort,LOW);
digitalWrite(valfuc,HIGH);
delay(2500);
}
}
//Counter
j=j+1; //This calculation neccesary because EEPROM memory can store max 4096bytes.
if(j==180)
{
i=i+1;
j=0;
}
EEPROM.write(0,i); //Save current i value to address "0"
EEPROM.write(1,j); //Save current j value to address "1"
delay(30);
counter=180i+j;
besbin=counter%5000; //Calculate mod value of counter
Serial.print("Current loop: ");
Serial.println(counter); //Prints current loop
//Check if a cycle is finished
if(besbin==0)
{
Serial.println("Paused");
Serial.println("Check the test rig");
cycle=cycle+1; //Loop processed 5000 times,Go on to next cycle
}
if(counter==36000)
{
Serial.println("Test is Finished!");
besbin=0;
}
}
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.