Help with programming

Hello,
I'm trying to make vehicles that can be recognize for a layout, I have 3 halls and want to lichtup seven LEDs so BYTE control basically, but it will not work with me, can somebody help me on the right track.
If the car runs over de halls the wrong ID coms treu, because of the problem that not all halls will respons or be active by the car, example,

Car 7,

First read out is Car 1
than 2 times car 7 (THATS RIGHT But it must by once) But i can live with this.
Thanx in advance
4 read out gives car 5

the first and last car is randomize not always the same, he does it 3 to 4 times good and then he gives a different readout But there is not car 7. :~

Programming.

/*
  Hall sensor schakeling EMV test modelbaan
  
 
 Schakelt (IR-)LED's aan en uit m.b.v. hall sensoren 
 
 Schema: Willy van As
Principe 
 * LED van pin via weerstand 220 aan aarde
 * Hall sensor aan pin met pull up weerstand van 10k
 */
  
// set pin numbers:
const int HS1Pin = 5;     // the number of the Hall 1 sensor pin Sleepcontact
const int HS2Pin = 2;     // the number of the Hall 2 sensor pin Voertuigen herkenning
const int HS3Pin = 3;     // the number of the Hall 3 sensor pin Voertuigen herkenning
const int HS4Pin = 4;     // the number of the Hall 4 sensor pin Voertuigen herkenning
const int IR1Pin =  6;      // the number of the LED 1 pin Sleepcontact (Groen)
const int IR2Pin =  7;      // the number of the LED 2 pin Voertuig 1 (Groen)
const int IR3Pin =  8;      // the number of the LED 3 pin Voertuig 2 (Groen)
const int IR4Pin =  9;      // the number of the LED 4 pin Voertuig 3 (Groen)
const int IR5Pin =  10;     // the number of the LED 5 pin Voertuig 4 (Geel)
const int IR6Pin =  11;     // the number of the LED 6 pin Voertuig 5 (Geel)
const int IR7Pin =  12;      // the number of the LED 7 pin Voertuig 6 (Geel)
const int IR8Pin =  13;     // the number of the LED 8 pin Voertuig 7 (Geel)

// variables will change:
int HS1State = 0;         // variable for reading the Hall 1 status
int HS2State = 0;         // variable for reading the Hall 2 status
int HS3State = 0;         // variable for reading the Hall 3 status
int HS4State = 0;         // variable for reading the Hall 4 status

long time = 0;
long debounce = 1;

void setup() {
// initialize serial communication:
  Serial.begin(9600);
  
  // initialize the LED pin as an output:
  pinMode(IR1Pin, OUTPUT);  // Sleepcontact  
  pinMode(IR2Pin, OUTPUT);  // Voertuig 1 001 HS2
  pinMode(IR3Pin, OUTPUT);  // Voertuig 2 010 HS3
  pinMode(IR4Pin, OUTPUT);  // Voertuig 3 100 HS4
  pinMode(IR5Pin, OUTPUT);  // Voertuig 4 011 HS2 en HS3
  pinMode(IR6Pin, OUTPUT);  // Voertuig 5 101 HS2 en HS4
  pinMode(IR7Pin, OUTPUT);  // Voertuig 6 110 HS3 en HS4
  pinMode(IR8Pin, OUTPUT);  // Voertuig 7 111 HS2 en HS3 en HS4
  
  // initialize the Hall sensor pin as an input:
  pinMode(HS1Pin, INPUT_PULLUP); 
  pinMode(HS2Pin, INPUT_PULLUP);
  pinMode(HS3Pin, INPUT_PULLUP); 
  pinMode(HS4Pin, INPUT_PULLUP);
  }

void loop(){
 
// Read HS 1 Sleepcontact herkenning
  HS1State = digitalRead(HS1Pin); 
if (HS1State == LOW) {     
    // turn IR1 on:    
    digitalWrite(IR1Pin, HIGH); //Voertuig sleeper herkend 
    Serial.println("-----------------------------");
    Serial.println("Sleper gezien van voertuig");
   } 
  else {
    // turn LED off:
    digitalWrite(IR1Pin, LOW); // Sleeper weg
    
  }

// Voertuigen herkennings tabel.
 HS2State = digitalRead(HS2Pin);
 HS3State = digitalRead(HS3Pin);
 HS4State = digitalRead(HS4Pin);
 
// Read HS2
 delay(4);
  if (HS2State == LOW && HS3State == HIGH && HS4State == HIGH && millis() - time > debounce)  {   //Voertuig 1  Bit 100
   // turn IR2 on:    
    digitalWrite(IR2Pin, HIGH);
    Serial.println("Voertuig type 1 gepasseerd");
  } 
  else {
    // turn LED off:
    digitalWrite(IR2Pin, LOW); 
  }
 
// Read HS3
 delay(4);
  if (HS2State == HIGH && HS3State == LOW && HS4State == HIGH && millis() - time > debounce) {   //Voertuig 2  Bit 010
    // turn IR3 on:    
    digitalWrite(IR3Pin, HIGH);  
     Serial.println("Voertuig type 2 gepasseerd");
  } 
  else {
    // turn LED off:
    digitalWrite(IR3Pin, LOW); 
  }   

// Read HS4 
 delay(4);
  if (HS2State == HIGH && HS3State == HIGH && HS4State == LOW && millis() - time > debounce) {  //Voeruig 3  Bit 001
    // turn IR4 on:
    digitalWrite(IR4Pin, HIGH);  
     Serial.println("Voertuig type 3 gepasseerd");
  } 
  else {
    // turn LED off:
    digitalWrite(IR4Pin, LOW); 
  }
  
// Read Halss HS2 and HS3 110
 delay(4);
  if (HS2State == LOW  && HS3State == LOW && HS4State ==HIGH && millis() - time > debounce) {     //Voertuig 4 Bit 110
    // turn IR5 on:    
digitalWrite(IR5Pin, HIGH);
     Serial.println("Voertuig type 4 gepasseerd"); 
       } 
  else {
    // turn LED off:
    digitalWrite(IR5Pin, LOW);
  }

  HS1State = digitalRead(HS1Pin); 
if (HS1State == LOW) {     
    // turn IR1 on:    
    digitalWrite(IR1Pin, HIGH); //Voertuig sleeper herkend 
    Serial.println("-----------------------------");
    Serial.println("Sleper gezien van voertuig");
   } 
  else {
    // turn LED off:
    digitalWrite(IR1Pin, LOW); // Sleeper weg
    
  }

// Read halls HS2 and HS4 101
 delay(4);
    if (HS2State == LOW  && HS3State == HIGH && HS4State == LOW && millis() - time > debounce) {  //Voeruig 5   Bit 101
    // turn IR6 on:    
    digitalWrite(IR6Pin, HIGH);
 Serial.println("Voertuig type 5 gepasseerd");
       } 
  else {
    // turn LED off:
    digitalWrite(IR6Pin, LOW);

  } 

// Read the halls HS3 and HS4 011
 delay(4);
  if (HS2State == HIGH  && HS3State == LOW && HS4State == LOW && millis() - time > debounce) {  //Voeruig 6 Bit 011  
// turn IR7 on:    
    digitalWrite(IR7Pin, HIGH);
 Serial.println("Voertuig type 6 gepasseerd");
       } 
  else {
    // turn LED off:
    digitalWrite(IR7Pin, LOW);
       
  } 

//Read the halls HS2 and HS3 and HS4 111
 delay(4);
  if (HS2State == LOW  && HS3State == LOW && HS4State == LOW && millis() - time > debounce) {  //Voeruig 7   Bit 111
// turn IR8 on:    
    digitalWrite(IR8Pin, HIGH); 
       Serial.println("Voertuig type 7 gepasseerd");
        } 
  else {
    // turn LED off:
    digitalWrite(IR8Pin, LOW);
       
      }
//   Serial.println(" ");
// delay(27);  
// delay(4);
}

Welcome to the Forum. To make sure you get good responses to your question, please read the two posts at the top of this Forum which details the way to make a post to this Forum. Also, always use code tags, as explained in the two posts, when your question presents a code listing. Following Nick's suggestions makes this a more effective tool.

Especially its important to explain clearly what you expect to happen, and what is
actually happening - don't expect us to guess. And re-edit your post with code tags :slight_smile:

I suggest you reorganize your code so that you you have one function to read all the sensors at one time and save the values and then another function (maybe several functions) that act on the values.

Try to avoid the need for IF statements with several tests. It is very easy to get the logic wrong.

This IF statement deals with 2**4 = 16 possible outcomes. Are you sure you have the correct one?

if (HS2State == LOW && HS3State == HIGH && HS4State == HIGH && millis() - time > debounce)

I strongly suspect there is a better way to deal with "bounce" - probably by saving the state of the sensors and not reading them again until the states have been reset after the values have been used. Reading all the sensors at the same time will greatly help to manage this.

For example

void loop() {
   readSensors();
   updateLeds();
}

...R

Robin2

Do you have a link white an example for me then i can try that code. Google can`t find it :slight_smile:

@ MarkT

When hall1 is hit Led 1 has to light up
When hall2 is hit Led2 has to light up
When hall3 is hit Led3 has to light up
When hall1 and hall2 are hit then Led4 light up
When hall1 and hall3 are hit then Led5 light up
When hall2 and hall3 are hit then Led6 light up
When all halls are hit Led7 lights up,

This for 1 second and then everything all over again.

The first Hall (Sleper auto) is not inportant for this program if its not working i can live with that.
Hope this is clear enough.

And when you have questions about it please ask and i try to give you an answer.

Willy112:
Do you have a link white an example for me then i can try that code. Google can`t find it :slight_smile:

I'm not sure what example you are looking for. I didn't have any specific link in mind.

These two Threads illustrate the use of functions planning and implementing a program and several things at a time. In both cases start with the first Post.

...R

Thanx Robin2,

I have got same reading to do and programming :cold_sweat: