Hall effect sensor project help

This project is pretty simple but i am new to all this. I am using an arduino uno, 6 leds and I have 6 hall effect sensors (a3144) monitoring magnets attached to 6 different rotating shafts. I would like the leds to turn on and stay on when the magnet to their respective shaft passes by and turn off when the magnet has not passed by in X amount of time. I would also like a relay to turn on when any of the hall effect sensors have not sensed the magnet in Y amount of time.

This project is for my farm to sense when our different grain augers are on, to turn an led on for each auger when it is turning and if one of them turns off to automatically turn off the auger that picks up grain out of the grain dryer.
The blower is not critical and won't result in a failure of equipment if it turns off.

This is the code i have so far.

<const int ledPin0 = A0; // LEDs connected to analog pins
const int ledPin1 = A1;
const int ledPin2 = A2;
const int ledPin3 = A3;
const int ledPin4 = A4;
const int ledPin5 = A5;
const int BottomAuger1 = 1; // the sensors are connected to digital pins
const int Cleaner2 = 2;
const int Cleaner3 = 3;
const int Conveyer4 = 4;
const int Leg5 = 5;
const int Blower6 = 6;
const int RelayPin = 10; // Relay pin for shutoff of metering rolls

int sensorReading0 = 0;// variable to store the value read from the sensor pin
int sensorReading1 = 0;
int sensorReading2 = 0;
int sensorReading3 = 0;
int sensorReading4 = 0;
int sensorReading5 = 0;
int sensorReading6 = 0;

void setup() {
// put your setup code here, to run once:
pinMode(ledPin0, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(RelayPin, OUTPUT);

pinMode(BottomAuger1, INPUT);
pinMode(Cleaner2, INPUT);
pinMode(Cleaner3, INPUT);
pinMode(Conveyer4, INPUT);
pinMode(Leg5, INPUT);
pinMode(Blower6, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:

sensorReading0 = digitalRead(BottomAuger1);

if (sensorReading0 == LOW) {
analogWrite(ledPin0, HIGH);
}
else {
analogWrite(ledPin0, LOW);
}

sensorReading1 = digitalRead(Cleaner2);

if (sensorReading1 == LOW) {
analogWrite(ledPin1, HIGH);
}
else {
analogWrite(ledPin1, LOW);
}
sensorReading2 = digitalRead(Cleaner3);

if (sensorReading2 == LOW) {
analogWrite(ledPin2, HIGH);
}
else {
analogWrite(ledPin2, LOW);
}
sensorReading3 = digitalRead(Conveyer4);

if (sensorReading3 == LOW) {
analogWrite(ledPin3, HIGH);
}
else {
analogWrite(ledPin3, LOW);
}
sensorReading4 = digitalRead(Leg5);

if (sensorReading4 == LOW) {
analogWrite(ledPin4, HIGH);
}
else {
analogWrite(ledPin4, LOW);
}

// non critical modual
sensorReading5 = digitalRead(Blower6);

if (sensorReading5 == LOW) {
analogWrite(ledPin5, HIGH);
}
else {
analogWrite(ledPin5, LOW);
}
delay(2);
}>

I see You tried to use code tags. You should click on the symbol up to the left in this window, and place the code in the middle of the cryptics coming up.

Here's a snippet I made:

void loop() {
  unsigned long led0TurnOffTime;
  // put your main code here, to run repeatedly:

  sensorReading0 = digitalRead(BottomAuger1);

  if (sensorReading0 == LOW) {
    analogWrite(ledPin0, HIGH);
    led0TurnOffTime = millis() + X_Time;// X_Time to turn off led. Will be upated when pulses occurs
  }
  else {
    if ( millis() >= led0TurnOffTime) {
      analogWrite(ledPin0, LOW);
    }
  }

  sensorReading1 = digitalRead(Cleaner2);

Learn how using arrays can simplify your code.

DrDiettrich:
Learn how using arrays can simplify your code.

Simplify..... I would rather say shortening it but in order to awoid putting preasure on OP I dropped the idea.

Hello
you can simplify your sketch for all stages as follows:
OLD:

  sensorReading0 = digitalRead(BottomAuger1);

  if (sensorReading0 == LOW) {
    analogWrite(ledPin0, HIGH);
  }
  else {
    analogWrite(ledPin0, LOW);
  }

NEW:

  analogWrite(ledPin0, digitalRead(BottomAuger1));

paulpaulson:

  analogWrite(ledPin0, digitalRead(BottomAuger1));

How to create the delayed turn off after X or Y time using that construction?

@OP
Why not use digitalWrite? There's no PWM on those pins as far as I know.

Railroader:
How to create the delayed turn off after X or Y time using that construction?

@OP
Why not use digitalWrite? There's no PWM on those pins as far as I know.

that will be magic - it´s weekend :slight_smile:

Snippet, including code for the relay handling.

void loop() {
  // put your main code here, to run repeatedly:
  unsigned long led0TurnOffTime;//Add up for the other channels
  bool relayTrigger0;//Add up for the other channels

  sensorReading0 = digitalRead(BottomAuger1);

  if (sensorReading0 == LOW) {
    analogWrite(ledPin0, HIGH);
    led0TurnOffTime = millis() + X_Time;// Time to turn of led. Will be upated when pulses occurs
    relayTrigger0 = true;
  }
  else {
    if ( millis() >= led0TurnOffTime) {
      analogWrite(ledPin0, LOW);
      relayTrigger0 = false;
    }
  }

  sensorReading1 = digitalRead(Cleaner2);

  if (sensorReading1 == LOW) {

After the last input is checked add somecode like this:

if( relayTrigger0 || relayTrigger1 || ....... ){
  digitalWrite(relayPin, true);//relay is on
else
  digitalWrite(relayPin, false);//No activity, relay off

Railroader:
How to create the delayed turn off after X or Y time using that construction?

@OP
Why not use digitalWrite? There's no PWM on those pins as far as I know.

I didn't use the digital pins for the leds because i wanted to keep them separate, all outputs are on one side of the board and all inputs are on the other side. Besides the relay but the relay goes a completely different direction than where the leds are and where the wires for the sensors will come in. It made sense in my brain

The analog pins are digital having an option to read analog signals!
analogWrite can used, on some digital pins(1). The syntax is analogWrite(pinNumber, PWM). PWM has a value from 0 to 255 on most controllers.

Ah alright Ill have to look into that more. thank you! your answers have been very helpful!

Do that! Giving You tips and You do the work Yourself is the best way to go, in my mind.
We're here....

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.