Hall sensor based LEDs switching

I have to make a project, in which hall sensor turns on 1st LED when it senses magnet first time,and it has to turn on 2nd LED when it is sensed 2nd time, and it has to turn on 1st LED 3rd time and so on. But this program is not working at all. I don't know what is problem.

int hall=8;
int red = 11;
int green=12;

// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
digitalWrite(red,LOW);
digitalWrite(green,LOW);
}

// the loop routine runs over and over again forever:
void loop() {
int sensor=0;
sensor=digitalRead(hall);
if(sensor==HIGH && green==LOW)
{
digitalWrite(red,LOW);
}
else if( sensor==LOW && red==LOW)
{
digitalWrite(green,HIGH);
}
else if(sensor==HIGH && red==LOW)
{
digitalWrite(green,LOW);
}
else if(sensor==LOW && green==LOW)
{
digitalWrite(red,HIGH);
}
}

Use code tags when posting code.

You are using green and red as variables for both the pin number and the logic level of the LEDs. These need to be seprate.

you can implement this using a counter,
when you read a data using HALL sensor, increase a counter.
if counter value even
glow 2nd led.
if counter is odd
glow 1st led.
at end of loop reset the counter

how to reset counter? any specific command?
Can anyone correct the problem in this code ?
:roll_eyes:

bilawal:
how to reset counter? any specific command?

Counter = 0 would reset it, but it will not cure your fundamental mistake that I told you about.

I tried this code using counter , but it is also not working. :frowning:

const int hall = 8; // the number of the hall effect sensor pin
const int red = 11;
const int green = 12;

void setup()
{
pinMode (red, OUTPUT);
pinMode (green, OUTPUT);

}

void loop()
{
int counter=0;
int sensor;
digitalWrite(red,HIGH);
digitalWrite(green,LOW);

counter=2;
if(counter%2==1)
{
sensor=digitalRead(hall);
if(sensor==HIGH)
{
digitalWrite(red,HIGH);
}
else
{
digitalWrite(red,LOW);
}
counter++;
}
else
{
sensor=digitalRead(hall);
if(sensor==HIGH)
{
digitalWrite(green,HIGH);

}
else
{
digitalWrite(green,LOW);
}
counter++;
}
}

use below code.. see whether it is full filling your need..

const int hall = 8;     // the number of the hall effect sensor pin
const int red =  11; 
const int green = 12;
int counter = 0;
int state = 0;
void setup() 
{
  Serial.begin(9600);
  pinMode (red, OUTPUT);
  pinMode (green, OUTPUT);
  pinMode(hall, INPUT);
}

void loop()
{
  do
   {
     state = digitalRead(hall);
   }while(!state);
   if(state)
      counter++;     
   if(counter == 1)
    {
      Serial.println("LED1");
      digitalWrite(green,LOW);
      digitalWrite(red, HIGH);
      state = 0;
    }
    if(counter == 2)
    {
      Serial.println("LED2");
      digitalWrite(red, LOW);
      digitalWrite(green, HIGH);
      state = 0;
    }
    if(counter > 2)
       counter = 0;
  delay(1000);
}

This program is not waiting for input from "Hall effect sensor" , red and green are glowing alternatively. I think there is mistake between end of do loop and start of counter loops

This program is not waiting for input from "Hall effect sensor"

You never said you wanted it to wait.
It is clear that you are not wanting to learn, and you do not want to respect people here by not adhering to our rules.

You are very lucky to have been given the support you have with such disrespectful behavior.

It may be my mistake in explaining, but have been trying continuously from 8 hours on my hardware, and right now, i am still working on it.

I am clearly explaining it now.

  1. hall sensor reads the value from magnet
  2. If value is high, it turns on Red LED
  3. If value is low, it turns off red LED
  4. hall sensor reads the value second time from magnet
  5. If value is high, it turns on Green LED
  6. If value is low, it turns off Green LED
  7. hall sensor reads and repeaetas Red Green loop

When ever you post code you must use code tags - read this:-
http://forum.arduino.cc/index.php/topic,148850.0.html

void loop()
{
  do
   {
     state = digitalRead(hall);
   }while(!state);

This is wrong replace it with.

void loop()
{
      state = digitalRead(hall);

I already tried this but i tried it again, it is still giving the same result,

Both LEDs are automatically turning on and off alternatively.

I have tried these changings in the previous program, but still not working. :~

const int hall = 8;     // the number of the hall effect sensor pin
const int red =  11; 
const int green = 12;

void setup() 
{
  pinMode (red, OUTPUT);
  pinMode (green, OUTPUT);
  pinMode(hall, INPUT);
  digitalWrite(red, LOW);
  digitalWrite(green,LOW);
}

void loop()
{
  int counter = 0;
int state = 0;
int prevstate=0;
     state = digitalRead(hall);  
  if(state != prevstate)
    {
      counter++;
      prevstate = state;
    }   
    if(counter == 1)
    {
      digitalWrite(green,LOW);
      digitalWrite(red, HIGH);

    }
    else if(counter == 3)
    {
      digitalWrite(red, LOW);
      digitalWrite(green, HIGH);
    }
   
    if(counter > 3)
       counter = 0;
   
  delay(1000);
}

I have tried these changings in the previous program, but still not working

The way this forum works is that you have to say why you think it is not working. What do you see happening and how is this different from what you want to happen.

The code is doing what the code is written to do.
What you have is the case where you are not explaining exactly what you want it to do.
This code will do what you said previously that you wanted:-

  1. hall sensor reads the value from magnet
  2. If value is high, it turns on Red LED
  3. If value is low, it turns off red LED
  4. hall sensor reads the value second time from magnet
  5. If value is high, it turns on Green LED
  6. If value is low, it turns off Green LED
  7. hall sensor reads and repeaetas Red Green loop

Now between step 3 and 4 is a very short time, it is in a loop so it will run these things very quickly. So with a constant input on the sensor it will appear to turn on both LEDs or turn off both LEDs depending on the sensor's input.
So you need to explain better what you mean in step 4.

The Red and green LEDs are only for testing, My actual project is "Hall sensor based Electronic Fuel Injection" in a 4 stroke Engine. Magnet is mounted on a rotating part of Engine. Hall sensor senses Magnet in every rotation. In one cycle, Fuel is injected. In 2nd cycle Fuel is ignited by spark plug. So the steps for the Arduino code will be

Step1 : Hall Sensor senses Magnet==HIGH and turns on "Fuel Injector".
Step2: Hall Sensor senses magnet==LOW and turns off fuel injector

step3: Hall sensor senses Magnet==HIGH and turns on spark plug
step4: hall sensor senses Magnet==LOW and turns off spark plug

Step4: Hall sensor senses Magnet==HIgh and turns on "Fuel injector" and so on.

My Hall sensor is working perfectly, I have used its circuit to find the rpm of my engine, which found to be 1670 . So, i hope this program will also work.

Now that is altogether something different again.

What keeps the two steps of the sequence in sync?

This code will do this but you run the risk of getting the signals out of phase therefore while the code will do what you ask it will not work in your engine.:-

const int hall = 8;     // the number of the hall effect sensor pin
const int red =  11; 
const int green = 12;
int counter = 0;
int state = 0;
void setup() 
{
  Serial.begin(9600);
  pinMode (red, OUTPUT);
  pinMode (green, OUTPUT);
  pinMode(hall, INPUT);
}

void loop()
{
   while(HIGH != state){ // wait for high
       state = digitalRead(hall);
   }
   // turn on fuel injector here
   
      while(HIGH != state){ // wait for LOW
       state = digitalRead(hall);
   }
  // turn off fuel injector here
  
    while(HIGH != state){ // wait for high
       state = digitalRead(hall);
   }
   // turn on spark plug here
   
      while(HIGH != state){ // wait for LOW
       state = digitalRead(hall);
   }
  // turn off spark plug here
  
}

Can you turn on and off a spark plug? I thought you could only fire it once.

Thankx for the help. At least i have learned some little concepts. :slight_smile:

I have been trying this for 2 weeks. If the synchronization is the problem, i have to think something different. Like switching of only " fule injector" by "hall sensor". And using a "timer" for "spark" in 2nd cycle. But it is a risk. What you say?

If the synchronization is the problem

Yes it is. There is no way of knowing what stroke you are on from a single sensor.

You need another sensor to detect the stroke type.

Like switching of only " fuel injector" by "hall sensor". And using a "timer" for "spark" in 2nd cycle.

That still will not work because the sensor will be triggered on each stroke of the engine and you have to sort out compression from ignition. Perhaps another sensor on the distributor is what you need.

Code:
void loop()
{
do
{
state = digitalRead(hall);
}while(!state);
This is wrong replace it with.
Code:
void loop()
{
state = digitalRead(hall);

i use do while(), so that keep reading sensor input until it is high, does it wrong to use do while() in this way..