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);
}
}
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
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.
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:-
hall sensor reads the value from magnet
If value is high, it turns on Red LED
If value is low, it turns off red LED
hall sensor reads the value second time from magnet
If value is high, it turns on Green LED
If value is low, it turns off Green LED
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.
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.
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?
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.