Hall effect sensor by arduino

HI,

Could anyone help me, please?

I just tried my codes with the sensors but it did not work as I wanted. It runs for 10 times with one sensor only. I want both output ones. As if, 3000 time it will ON after contact with first sensor and then wait until it contact with second sensor then turn off .

flow chart will be like,
(Start > Magnetic Sensor 1 ON -> Solenoid Valve ON --> Magnetic Sensor 2 ON ---> Solenoid Valve OFF ) 3000 times.

First of all I declared the global variables

 /*Declaring Global variables*/
int sensor_1_pin = 2;        //hall effect sensor output connected to Ard
int sensor_2_pin = 3;        //hall effect sensor output connected to Ard 

int led = 7;                      //Declared LED 


int sensor_1_value = 0;     
int sensor_2_value = 0;

int cycle = 3000;
int v = 0;

void setup (), has been described as it is simply just declared the pinModes.

/*Declaring Setup  
void setup()
{
  Serial.begin (9600);
  pinMode(sensor_1_pin, INPUT);
  pinMode(sensor_2_pin, INPUT);
  
  pinMode(led, OUTPUT);
}

My main issue is the loop. Last two weeks, I tired a lot to sort it out. there are 3 issues and those stuck me for ages.

Issue 1: when I brings the sensor1 closure to the Magnet, it LED turn on until I remove it but I want to remain it on until the (second sensor) sensor2 come close to the Magnet.

Issue 2: When I brought the sensor1 close to the magnet, it runs for 3000 times until i remove it. But I want the led will turn on and then sensor 2 will be closure and led will turn off. AND OVERALL, it WILL COUNT as 1 CYCLE.

Issue 3: the following code give me value like "Sensor 1 Value = 1" and "Sensor 2 value =1".... No idea why??

void loop()
{
//read and store sensor value (1 or 0)
  sensor_1_value = digitalRead(sensor_1_pin);     //Collect Sensor 1 Value from the sensor 1 pin
  sensor_2_value = digitalRead(sensor_2_pin);     //Collect Sensor 2 Value from the sensor 1 pin
  
  Serial.print ("Sensor 1 value = ");                                     
  Serial.println (sensor_1_value);
  Serial.print ("Sensor 2 value = ");
  Serial.println (sensor_2_value);
 
 //if sensor 1 value doesn't match sensor 2 value
  if (sensor_1_value != sensor_2_value)
  { if (v<cycle)
    {//if sensor 1 value is HIGH
      if(sensor_1_value == HIGH)
         {digitalWrite(led, HIGH);
          Serial.print ("Sensor 1 value = ");
          Serial.println (sensor_1_value); }
//if sensor 2 value is HIGH
      else if(sensor_2_value == HIGH)
         {   digitalWrite(led, LOW);
             Serial.print ("Sensor 1 value = ");
             Serial.println (sensor_1_value);}
    } 
v++; }
 sensor_1_value = sensor_2_value;
}

Please help anybody. I am hopeless. :frowning:

Please use code tags </> to present code.

Your loop executes the body of if (i<cycle) regardless of sensor values. I.e. it counts cycles even if the sensor values have not changed.

What you want is a state machine (automaton). With two digital inputs you have 4 possible states (LL, LH, HL, HH), Let's say you start in state LL (both sensors LOW), you do nothing until a change in the sensor values occurs. When the first sensor value changes to HIGH, you detect HL as the next state, perform desired actions, and remember HL as the new current state. From there on the current and new state remains the same, and nothing shall happen until either sensor value changes. Then again you do whatever required for a change from the old to the new state. The same for the other possible states.

Declare a global currentState variable, and in loop() a newState variable. Calculate newState from both inputs, and check for each meaningful transition like

if (currentState==LL && newState==HL)

and finally remember currentState=newState;
Then fill in code for turning the LED on and off, and for counting an completed cycle, in the body of the related state transition detection.

sir,
I am not great with Arduino and as I understand, I wrote my codes but still not getting the output I wanted and you explained. Please help.

/******************************
* global variables                    *
******************************/

int sensor_1_pin = 2;
int sensor_2_pin = 3; //hall effect sensor output connected to Ard UNO
int currentState = 0;
int led = 7;   //Declaring Variables

int sensor_1_value = 0;  
int sensor_2_value = 0;

int cycle = 3000;   //Cycle to 3000 Times
int i = 0;          //for if loop
/******************************
* setup                                    *
******************************/
void setup()
{
  Serial.begin (9600);
  //Pin declaration 
  
  pinMode(sensor_1_pin, INPUT);
  pinMode(sensor_2_pin, INPUT);
  
  pinMode(led, OUTPUT);
}
void loop()
{
  int newState = 0;
  //Read and store sensor state (1 or 0)
  sensor_1_value = digitalRead(sensor_1_pin);
  sensor_2_value = digitalRead(sensor_2_pin);
  
  Serial.print ("Sensor 1 value = ");
  Serial.println (sensor_1_value);
  Serial.print ("Sensor 2 value = ");
  Serial.println (sensor_2_value);
  
  //if sensor 1 value doesn't match sensor 2 value
  if (currentState==(sensor_1_value==1 && sensor_1_value==1) && (sensor_1_value==0 && sensor_1_value==1))
  if (sensor_1_value != sensor_2_value)
  {
    if (i<cycle)
    {
      if(sensor_1_value == HIGH)             //sensor 1 value is a 1, turn off led
      {
              digitalWrite(led, LOW);
              Serial.print ("Sensor 1 value = ");
              Serial.println (sensor_1_value);
      }
      else if(sensor_2_value == HIGH)         //sensor 2 value is a 1, turn on led
      {
             digitalWrite(led, HIGH);
             Serial.print ("Sensor 2 value = ");
             Serial.println (sensor_2_value);
      }
    } 
   i++; 
  }
 currentState=newState;
}//<-----end of void loop()

If you just want on when sensor 1 comes around and off when sensor 2 comes around a binary state is ok.
In other words the state only changes when a sensor is activated, when a sensor has a value of zero, it's
not changed.

Modify your code with this and display "on" to see it does what you want.

bool on = false;

if (sensor_1_value) {
on = true;
}

if (sensor_2_value) {
on = false;
}

sonyhome:
If you just want on when sensor 1 comes around and off when sensor 2 comes around a binary state is ok.
In other words the state only changes when a sensor is activated, when a sensor has a value of zero, it's
not changed.

Modify your code with this and display "on" to see it does what you want.

bool on = false;

if (sensor_1_value) {
on = true;
}

if (sensor_2_value) {
on = false;
}

Sir, could you please add your ideas in my void loop. Please. It would be really helpful.
However, I actually, want to get the feedback from the sensor and led will turn on and off according. As when sensor 1 will bring closure to the magnet, it will turn on and then it will remain on until sensor 2 will come closure to the magnet. Similarly LED will remain off until sensor 1 will bring closure. and cycle will count only when the LED is turn on and off once.

Please help.

You can encode the state from the inputs, like newState = sensor1_value + sensor2_value*2; or newState = sensor1_value | sensor2_value<<1;, resulting in state values 0 to 3. #define meaningful names for these values.