Physcis Project

Hello,
For chool I had to do Project with the Arduino MEGA 2560.
I`m useing the halleffect-sensor Honeywell ss41.
My plan is that, if one or more Sensor detect an Magnetic FIied an LED should light up.
With one sensor it works perfectly, but i dont know what i need to change about the Code if i whats to use more sensores (but all of them are the ss41 sensor).

my code at the moment is.

int hePIN = 12;
int ledPIN = 13;
int heState = 0;

void setup (){
pinMode (ledPIN, OUTPUT);
pinMode (hePIN, INPUT );
}
void loop(){
heState = digitalRead (hePIN);
if (heState == LOW){
digitalWrite (ledPIN,HIGH);
}
else {
digitalWrite (ledPIN,LOW);}}

so i would like tu include like 5 or more of the same sensor to this code..

I really hope someone can help me.
So thanks in advance..

(sry for no perfect english )

You need to improve your coding layout, that makes code more readable.
second when posting code use code tags. # button above smiley's

When you want to use more identical sensors arrays is the way to go:
look at the code below and add comments, version number, author etc.

int hePIN[4] = { 9,10,11,12 };
int ledPIN[4]= { 2,3,4,5};
int led = 13;

int heState[4]; // will be read in loop()

void setup ()
{
  for (int i=0; i<4; i++)
  {
    pinMode (ledPIN[i], OUTPUT);
    pinMode (hePIN[i], INPUT );
  }
  pinMode(led, OUTPUT);
}

void loop()
{
  // READ THE STATES
  for (int i=0; i<4; i++)
  {
    heState[i] = digitalRead (hePIN[i]);
  }

  // SOMETHING EXTRA - if one pin is low led 13 will also trigger
  int state13 = LOW;
  for (int i=0; i<4; i++)
  {
    if (heState == LOW) state13 = HIGH;
  }

  // REFLECT STATE IN THE LEDS
  for (int i=0; i<4; i++)
  {
    if ( heState[i] == LOW)
    {
      digitalWrite (ledPIN, HIGH);
    }
    else
    {
       digitalWrite (ledPIN, LOW);
    }
  }
  digitalWrite(led, state13);

}

hey everyone,

for school i had to do a project with the arduino.
i wanted to install some halleffect-sensors that trigger an led if they detact an megnetic field.
with one sensor it works proberly, but i dont know how to change the code in order to have more sensors installed.(sensor is the honeywell ss41 digital hallsensor)

my current code is:

int hePIN = 12;
int ledPIN = 13;
int heState = 0;

void setup (){
pinMode (ledPIN, OUTPUT);
pinMode(hePIN, INPUT);
}
void loop(){
hestate = digitalRead (hePIN);
if (heState == LOW);
{digitalWrite(ledPIN,HIGH);}
else {
digitalWrite (ledPIN,LOW9;
}}

I would be very happy i someone could help me out :slight_smile:
thanks in advance :))

(deleted)

If you have to ask with such a simple code, then you have some more homework to do.

Go through and experiment with all the Arduino's example sketches and learn how they work.

Once you have done that, break down your problem and figure out what is making your first sensor work, then add more.

HazardsMind:
then add more.

I love it.

And I agree 100%

...R

already answered here - Physcis Project - Project Guidance - Arduino Forum -

Rob, can you merge the two posts?

merged,

@Michael1996
Crossposting wastes time. Forgetting you posted the question already even wastes your time. :slight_smile:

robtillaart:
You need to improve your coding layout, that makes code more readable.
second when posting code use code tags. # button above smiley's

When you want to use more identical sensors arrays is the way to go:
look at the code below and add comments, version number, author etc.

int hePIN[4] = { 9,10,11,12 };

int ledPIN[4]= { 2,3,4,5};
int led = 13;

int heState[4]; // will be read in loop()

void setup ()
{
  for (int i=0; i<4; i++)
  {
    pinMode (ledPIN[i], OUTPUT);
    pinMode (hePIN[i], INPUT );
  }
  pinMode(led, OUTPUT);
}

void loop()
{
  // READ THE STATES
  for (int i=0; i<4; i++)
  {
    heState[i] = digitalRead (hePIN[i]);
  }

// SOMETHING EXTRA - if one pin is low led 13 will also trigger
  int state13 = LOW;
  for (int i=0; i<4; i++)
  {
    if (heState == LOW) state13 = HIGH;
  }

// REFLECT STATE IN THE LEDS
  for (int i=0; i<4; i++)
  {
    if ( heState[i] == LOW)
    {
      digitalWrite (ledPIN, HIGH);
    }
    else
    {
       digitalWrite (ledPIN, LOW);
    }
  }
  digitalWrite(led, state13);

}

I think you mean

 for (int i=0; i<4; i++)
  {
    if ( heState[i] == LOW)
    {
      digitalWrite (ledPIN[i], HIGH); //select the required LED
    }
    else
    {
       digitalWrite (ledPIN[i], LOW);   //select the required LED
    }

Also, that contracts to:

for (int i=0; i<4; i++)    {
      digitalWrite (ledPIN[i], !heState[i]); //select the required LED
    }