Please Check My code

Good Day Everyone.
I'm John and I am a novice at code
It seems that somehow, I have my sensor triggers going to Analog side of my Arduino and I think I need to use or would like to use the digital side like 9 and 10 to possibly increase the sensitivity of my infrared sensors.

I just don't know how far down or how many line down the sketch I need to change I will send a copy of my sketch.

another set of eyes would be greatly appreciated

Thanks,
John D

int sensePin1 = A0; //declares A0 as our sensor1 input
int sensePin2 = A1; //declares A1 as our sensor2 input
int Flash = 6; //control for our flasher board
#include<Servo.h>//adds servo control library
int flashcount;//count of flash cycles
int pos = 90; //start position for servos
Servo myservo;//names servo control

void setup() {
  Serial.begin(9600);//enables serial monitoring
  pinMode(Flash, OUTPUT);//establishing pin 6 as an output pin
  myservo.attach(9);//sets up pin 9 as our servo control
}

enum CROSSINGSTATES
{
  ST_OFF,
  ST_FLASHING1,
  ST_FLASHING2,
  ST_FLASHING3,
  ST_FLASHING4,
  ST_ON1,
  ST_ON2,
}; //Identifies our 7 crossing states

CROSSINGSTATES crossingState = ST_OFF;// default crossing state

void loop() {
  int valA1 = analogRead(sensePin1);//reads sensor 1
  int valA2 = analogRead(sensePin2);//reads sensor 2
  Serial.println(valA1);
  Serial.println(valA2);//prints values in serial monitor
  delay(200);

  switch (crossingState)
  {
    case ST_OFF:
      crossingoff(valA1, valA2);
      break;
    case ST_FLASHING1:
      crossingflashing1(valA1, valA2);
      break;
    case ST_FLASHING2:
      crossingflashing2(valA1, valA2);
      break;
    case ST_FLASHING3:
      crossingflashing3(valA1, valA2);
      break;
    case ST_FLASHING4:
      crossingflashing4(valA1, valA2);
      break;
    case ST_ON1:
      crossingon1(valA1, valA2);
      break;
    case ST_ON2:
      crossingon2(valA1, valA2);
      break;
  }
}

void crossingoff(int valA1, int valA2) {
  digitalWrite(Flash, LOW);//keeps power down on flasher board
  delay(200);
  flashcount = 0; //resets flashcounter
  if (valA1 > 200 && valA2 < 200) {
    crossingState = ST_FLASHING1;
  }//if sensePin1 is triggered
  if (valA1 < 200 && valA2 > 200) {
    crossingState = ST_FLASHING2;
  }//if sensePin2 is triggered
}

void crossingflashing1(int valA1, int valA2) {
  digitalWrite(Flash, HIGH);//turns on flasher board
  for (pos = 90; pos <= 180; pos += 1) //the servo goes from 90 to 180 degreesin steps of 1 degree
  {
    myservo.write(pos);//controls the servo
    delay(15);
    crossingState = ST_ON1;
  }
}

void crossingflashing2(int valA1, int valA2) {
  digitalWrite(Flash, HIGH);//turns on flasher board
  for (pos = 90; pos <= 180; pos += 1) //the servo goes from 90 to 180 degreesin steps of 1 degree
  {
    myservo.write(pos);//controls the servo
    delay(15);
    crossingState = ST_ON2;
  }
}

void crossingon1(int valA1, int valA2) {
  digitalWrite(Flash, HIGH); // keeps the flasher on
  delay(1000); //1 second delay
  flashcount++;//adds 1 to the flashcount
  if (valA1 < 200 && valA2 > 200) {
    flashcount = 0;
    crossingState = ST_FLASHING3; //switches to flashing 3 if the exit sensor is tripped
  }
  else if (valA1 > 200 && valA2 > 200 && flashcount > 5) {
    crossingState = ST_FLASHING4; //if the train is shorter than the distance between the crossing sensors or is sitting in between the sensors
  }
}

void crossingon2(int valA1, int valA2) {
  digitalWrite(Flash, HIGH); // keeps the flasher on
  delay(1000); //1 second delay
  flashcount++;//adds 1 to the flashcount
  if (valA1 > 200 && valA2 < 200) {
    flashcount = 0;
    crossingState = ST_FLASHING3; //switches to flashing 3 if the exit sensor is tripped
  }
  else if (valA1 > 200 && valA2 > 200 && flashcount > 5) {
    crossingState = ST_FLASHING4; //if the train is shorter than the distance between the crossing sensors or is sitting in between the sensors
  }
}

void crossingflashing3(int valA1, int valA2) {
  digitalWrite(Flash, HIGH); // keeps the flasher on
  delay(1000); //1 second delay
  flashcount++;//adds 1 to the flashcount
  if (valA1 > 200 && valA2 > 200 && flashcount > 5) {
    {
      for (pos = 180; pos >= 90; pos -= 1) // goes from 180 to 90 in increments of 1 degree
        myservo.write(pos);
      delay(15);
    }
    delay(1000);
    crossingState = ST_OFF;
  }
}

void crossingflashing4(int valA1, int valA2) {
  digitalWrite(Flash, HIGH); // keeps the flasher on
  delay(1000); //1 second delay
  flashcount++;//adds 1 to the flashcount
  if (valA1 < 200 || valA2 < 200) {
    flashcount = 0;
    crossingState = ST_FLASHING3;
  }
  else if (flashcount > 120) {
    {
    for (pos = 180; pos >= 90; pos -= 1) // goes from 180 to 90 in increments of 1 degree
      myservo.write(pos);
    delay(15);
  }
    crossingState = ST_OFF;
  }
}

I think you need to edit the code like this:

#include <Servo.h> // adds servo control library
int sensePin1 = 9; // declares pin 9 as sensor1 input
int sensePin2 = 10; // declares pin 10 as sensor2 input
int Flash = 6; // control for our flasher board
int flashcount; // count of flash cycles
int pos = 90; // start position for servos
Servo myservo; // names servo control

void setup() {
  Serial.begin(9600); // enables serial monitoring
  pinMode(Flash, OUTPUT); // establishing pin 6 as an output pin
  pinMode(sensePin1, INPUT); // sets pin 9 as input
  pinMode(sensePin2, INPUT); // sets pin 10 as input
  myservo.attach(9); // sets up pin 9 as our servo control
}

enum CROSSINGSTATES {
  ST_OFF,
  ST_FLASHING1,
  ST_FLASHING2,
  ST_FLASHING3,
  ST_FLASHING4,
  ST_ON1,
  ST_ON2,
}; // Identifies our 7 crossing states

CROSSINGSTATES crossingState = ST_OFF; // default crossing state

void loop() {
  int valA1 = digitalRead(sensePin1); // reads sensor 1
  int valA2 = digitalRead(sensePin2); // reads sensor 2
  Serial.println(valA1);
  Serial.println(valA2); // prints values in serial monitor
  delay(200);

  switch (crossingState) {
    case ST_OFF:
      crossingoff(valA1, valA2);
      break;
    case ST_FLASHING1:
      crossingflashing1(valA1, valA2);
      break;
    case ST_FLASHING2:
      crossingflashing2(valA1, valA2);
      break;
    case ST_FLASHING3:
      crossingflashing3(valA1, valA2);
      break;
    case ST_FLASHING4:
      crossingflashing4(valA1, valA2);
      break;
    case ST_ON1:
      crossingon1(valA1, valA2);
      break;
    case ST_ON2:
      crossingon2(valA1, valA2);
      break;
  }
}

void crossingoff(int valA1, int valA2) {
  digitalWrite(Flash, LOW); // keeps power down on flasher board
  delay(200);
  flashcount = 0; // resets flashcounter
  if (valA1 == HIGH && valA2 == LOW) {
    crossingState = ST_FLASHING1;
  } // if sensePin1 is triggered
  if (valA1 == LOW && valA2 == HIGH) {
    crossingState = ST_FLASHING2;
  } // if sensePin2 is triggered
}

// Other functions (e.g., crossingflashing1, crossingflashing2, crossingon1, crossingon2, crossingflashing3, crossingflashing4)
// remain the same except replace "valA1 > 200" with "valA1 == HIGH" and "valA1 < 200" with "valA1 == LOW".

Describe the edit and results.

1 Like

Unfortunately, using digital inputs instead of analog inputs will not make your infrared sensors more sensitive.

Please post a link to tech details of the sensors you are using, and a diagram showing how you wired them to the Arduino.

Please also, post a link to the source of the code you posted, as it's clearly not a novice at the helm.

For testing if it will work you can simply use e.g. digitalread(A0) instead of analogRead(A0); analogue pins usually work as digital pins as well (e.g. on an Uno or Mega).

You did not mention which board you're using; you also did not mention the sensor or provide a datasheet for it. You will need to consult the datasheet of the main processor and probably of the sensor to find out if it will work.

Your limits are 200 meaning that on a 5V processor anything below roughly 1V will be considered false (or LOW) and anything above 1V will be considered true (or HIGH).

The datasheet of the 328P states (for digital inputs)

Which for 5V reads as

  • The highest voltage on a pin that is considered a LOW is 0.3 * Vcc = 1.5V.
  • The lowest voltage on a pin that is considered a HIGH is 0.6 * Vcc = 3V.
  • Between 0.3 * Vcc and 0.6 * Vcc is grey area.

So now check the datasheet of the sensor for the VOH and VOL and see if it will work.

Using a digital pin for a analog signal makes no sense whether you want to increase sensitivity or not
You may need to make changes to you hardware in order to increase sensitivity.
What are these sensors you are using and what are you trying to sense?

The servo will never move away from zero degrees because flashcount is set to zero in three out of four conditions.

OK so to continue the sketch was a download from a fellow model railroader
the sensors I am using is an IR Infrared Sensor 3 wire Reflective Photoelectric Module for Arduino

it takes 3 to 5 volts I'm giving it 5>volts I have the positive to the 5-volt Arduino pin and the ground-to-ground pin and for the trigger to pin A1 for closing and operating the lights and A2 for raising and stopping the lights as you can see in my sketch

as you probably get by now it's for a model railroad train layout

now I'll try to explain this for reasons of space I tried to tie 5 infrared red diodes and 5 triggers to one sensor

Question? so I'm thinking outload here I know I'm close
am I over taxing the sensor?
do I need to use one sensor for each section of track?
there are 11 in all 6 entering the crossing and 5 leaving

I also added a 25v 1000 capacitor to the sensor to hold the gates down as multiple railroad cars pass over each location

it does work however not every time

sometimes it misses a train and that's why I think increasing the sensitivity some way may help

It seems to be I don't have enough in the potentiometer to get the sensitivity I need

Again, do you think I'm overtaxing the sensor asking it to do too much?

thanks for the feedback
John

What is the device output range when connected to an analog pin? Digital pin?

1 Like

OK so to continue the sketch was a download from a fellow model railroader
the sensors I am using is an IR Infrared Sensor 3 wire Reflective Photoelectric Module for Arduino

it takes 3 to 5 volts I'm giving it 5>volts I have the positive to the 5-volt Arduino pin and the ground-to-ground pin and for the trigger to pin A1 for closing and operating the lights and A2 for raising and stopping the lights as you can see in my sketch

as you probably get by now it's for a model railroad train layout

now I'll try to explain this for reasons of space I tried to tie 5 infrared red diodes and 5 triggers to one sensor

Question? so I'm thinking outload here I know I'm close
am I over taxing the sensor?
do I need to use one sensor for each section of track?
there are 11 in all 6 entering the crossing and 5 leaving

I also added a 25v 1000 capacitor to the sensor to hold the gates down as multiple railroad cars pass over each location

it does work however not every time

sometimes it misses a train and that's why I think increasing the sensitivity some way may help

It seems to be I don't have enough in the potentiometer to get the sensitivity I need

Again, do you think I'm overtaxing the sensor asking it to do too much?

thanks for the feedback
John

That simply just won't work.

1 Like

Yes.
Firstly, five LEDs in parallel should have individual series resistors, or they are not likely to share the current available (one series resistor) equally; that current will be split unevenly across the five LEDs.
Secondly, all five sensors in parallel results in the likelihood that ambient IR will produce enough current to leave you in the 'always triggered' state, or possibly flickering on the threshold of there-not there.
Neither will result in the desired behavior.

I presume you haven't enough inputs on the Arduino, otherwise you'd simply wire in five sensor boards. Your space comment makes me wonder, as likely scenarios for using these sensors for crossing gates have them spread out along the tracks - they wouldn't be crowding each other anyway, so what's the real problem?

I think if i use individual IR sensors for each location (11) in all 6 entering and 5 exits i have a separate power sorce so power ist a problem

Then i will tie the signal wires togearther and send it to the Arduino Uno and that may be the solution to this

I will let you know of my progress as for now i have some bench work to do i need ro get all my components together and secured and connected

Thanks to all

Hello Jim
and thank you for your help. . ..

I decided to use individual sesors for each location and then of course modified my code and since the Arduino Uno didn't have enough, I went with the mega
would you mind checking my code??

Thanks John

int sensePin1 = A0; //declares A0 as our sensor1 input
int sensePin2 = A1; //declares A1 as our sensor2 input
int sensePin3 = A2; //declares A2 as our sensor3 input
int sensePin4 = A3; //declares A3 as our sensor4 input
int sensePin5 = A4; //declares A4 as our sensor5 input
int sensePin6 = A5; //declares A5 as our sensor6 input
int sensePin7 = A6; //declares A6 as our sensor7 input
int sensePin8 = A7; //declares A7 as our sensor8 input
int sensePin9 = A8; //declares A8 as our sensor9 input
int sensePin10 = A9; //declares A9 as our sensor10 input
int Flash = 6; //control for our flasher board
#include<Servo.h>//adds servo control library
int flashcount;//count of flash cycles
int pos = 90; //start position for servos
Servo myservo;//names servo control

void setup() {
  Serial.begin(9600);//enables serial monitoring
  pinMode(Flash, OUTPUT);//establishing pin 6 as an output pin
  myservo.attach(9);//sets up pin 9 as our servo control
}

enum CROSSINGSTATES
{
  ST_OFF,
  ST_FLASHING1,
  ST_FLASHING2,
  ST_FLASHING3,
  ST_FLASHING4,
  ST_ON1,
  ST_ON2,
}; //Identifies our 7 crossing states

CROSSINGSTATES crossingState = ST_OFF;// default crossing state

void loop() {
  int valA1 = analogRead(sensePin1);//reads sensor 1
  int valA2 = analogRead(sensePin2);//reads sensor 2
  int valA3 = analogRead(sensePin2);//reads sensor 3
  int valA4 = analogRead(sensePin2);//reads sensor 4
  int valA5 = analogRead(sensePin2);//reads sensor 5
  int valA6 = analogRead(sensePin2);//reads sensor 6
  int valA7 = analogRead(sensePin2);//reads sensor 7
  int valA8 = analogRead(sensePin2);//reads sensor 8
  int valA9 = analogRead(sensePin2);//reads sensor 9
  int valA10 = analogRead(sensePin2);//reads sensor10
  Serial.println(valA1);//prints values in serial monitor
  Serial.println(valA2);//prints values in serial monitor
  Serial.println(valA3);//prints values in serial monitor
  Serial.println(valA4);//prints values in serial monitor
  Serial.println(valA5);//prints values in serial monitor
  Serial.println(valA6);//prints values in serial monitor
  Serial.println(valA7);//prints values in serial monitor
  Serial.println(valA8);//prints values in serial monitor
  Serial.println(valA9);//prints values in serial monitor
  Serial.println(valA10);//prints values in serial monitor
  delay(200);

  switch (crossingState)
  {
    case ST_OFF:
      crossingoff(valA1, valA2, valA3, valA4, valA5, valA6, valA7, valA8, valA9, valA10);
      break;
    case ST_FLASHING1:
      crossingflashing1(valA1, valA2, valA3, valA4, valA5, valA6, valA7, valA8, valA9, valA10);
      break;
    case ST_FLASHING2:
      crossingflashing2(valA1, valA2, valA3, valA4, valA5, valA6, valA7, valA8, valA9, valA10);
      break;
    case ST_FLASHING3:
      crossingflashing3(valA1, valA2, valA3, valA4, valA5, valA6, valA7, valA8, valA9, valA10);
      break;
    case ST_FLASHING4:
      crossingflashing4(valA1, valA2, valA3, valA4, valA5, valA6, valA7, valA8, valA9, valA10);
      break;
    case ST_ON1:
      crossingon1(valA1, valA2, valA3, valA4, valA5, valA6, valA7, valA8, valA9, valA10);
      break;
    case ST_ON2:
      crossingon2(valA1, valA2, valA3, valA4, valA5, valA6, valA7, valA8, valA9, valA10);
      break;
  }
}

void crossingoff(int valA1, int valA2, int valA3, int valA4, int valA5, int valA6, int valA7, int valA8, int valA9, int valA10) {
  digitalWrite(Flash, LOW);//keeps power down on flasher board
  delay(200);
  flashcount = 0; //resets flashcounter
  if (valA1 > 200 && valA2 < 200) {
    crossingState = ST_FLASHING1;
  }//if sensePin1 is triggered
  if (valA1 < 200 && valA2 > 200) {
    crossingState = ST_FLASHING2;
  }//if sensePin2 is triggered
}

void crossingflashing1(int valA1, int valA2, int valA3, int valA4, int valA5, int valA6, int valA7, int valA8, int valA9, int valA10) {
  digitalWrite(Flash, HIGH);//turns on flasher board
  for (pos = 90; pos <= 180; pos += 1) //the servo goes from 90 to 180 degreesin steps of 1 degree
  {
    myservo.write(pos);//controls the servo
    delay(15);
    crossingState = ST_ON1;
  }
}

void crossingflashing2(int valA1, int valA2, int valA3, int valA4, int valA5, int valA6, int valA7, int valA8, int valA9, int valA10) {
  digitalWrite(Flash, HIGH);//turns on flasher board
  for (pos = 90; pos <= 180; pos += 1) //the servo goes from 90 to 180 degreesin steps of 1 degree
  {
    myservo.write(pos);//controls the servo
    delay(15);
    crossingState = ST_ON2;
  }
}

void crossingon1(int valA1, int valA2, int valA3, int valA4, int valA5, int valA6, int valA7, int valA8, int valA9, int valA10) {
  digitalWrite(Flash, HIGH); // keeps the flasher on
  delay(1000); //1 second delay
  flashcount++;//adds 1 to the flashcount
  if (valA1 < 200 && valA2 > 200) {
    flashcount = 0;
    crossingState = ST_FLASHING3; //switches to flashing 3 if the exit sensor is tripped
  }
  else if (valA1 > 200 && valA2 > 200 && flashcount > 5) {
    crossingState = ST_FLASHING4; //if the train is shorter than the distance between the crossing sensors or is sitting in between the sensors
  }
}

void crossingon2(int valA1, int valA2, int valA3, int valA4, int valA5, int valA6, int valA7, int valA8, int valA9, int valA10) {
  digitalWrite(Flash, HIGH); // keeps the flasher on
  delay(1000); //1 second delay
  flashcount++;//adds 1 to the flashcount
  if (valA1 > 200 && valA2 < 200) {
    flashcount = 0;
    crossingState = ST_FLASHING3; //switches to flashing 3 if the exit sensor is tripped
  }
  else if (valA1 > 200 && valA2 > 200 && flashcount > 5) {
    crossingState = ST_FLASHING4; //if the train is shorter than the distance between the crossing sensors or is sitting in between the sensors
  }
}

void crossingflashing3(int valA1, int valA2, int valA3, int valA4, int valA5, int valA6, int valA7, int valA8, int valA9, int valA10) {
  digitalWrite(Flash, HIGH); // keeps the flasher on
  delay(1000); //1 second delay
  flashcount++;//adds 1 to the flashcount
  if (valA1 > 200 && valA2 > 200 && flashcount > 5) {
    {
      for (pos = 180; pos >= 90; pos -= 1) // goes from 180 to 90 in increments of 1 degree
        myservo.write(pos);
      delay(15);
    }
    delay(1000);
    crossingState = ST_OFF;
  }
}

void crossingflashing4(int valA1, int valA2, int valA3, int valA4, int valA5, int valA6, int valA7, int valA8, int valA9, int valA10) {
  digitalWrite(Flash, HIGH); // keeps the flasher on
  delay(1000); //1 second delay
  flashcount++;//adds 1 to the flashcount
  if (valA1 < 200 || valA2 < 200) {
    flashcount = 0;
    crossingState = ST_FLASHING3;
  }
  else if (flashcount > 120) {
    {
    for (pos = 180; pos >= 90; pos -= 1) // goes from 180 to 90 in increments of 1 degree
      myservo.write(pos);
    delay(15);
  }
    crossingState = ST_OFF;
  }
}


All I can tell you is that it compiles. Whether it does what you want, I have no way of knowing, since I don't know what it is supposed to do.

I would replace all those 200s with a constant that is declared at the beggining
const int threshold = 200;
So you don't need to make 18 edits if you need to make a change

  int valA1 = analogRead(sensePin1);//reads sensor 1
  int valA2 = analogRead(sensePin2);//reads sensor 2
  int valA3 = analogRead(sensePin2);//reads sensor 3
  int valA4 = analogRead(sensePin2);//reads sensor 4
  int valA5 = analogRead(sensePin2);//reads sensor 5
  int valA6 = analogRead(sensePin2);//reads sensor 6
  int valA7 = analogRead(sensePin2);//reads sensor 7
  int valA8 = analogRead(sensePin2);//reads sensor 8
  int valA9 = analogRead(sensePin2);//reads sensor 9
  int valA10 = analogRead(sensePin2);//reads sensor10

10 analog reads, but all but 1 are reading sensePin2? Editing error, or intentional?

Thank you

When you do this:

void crossingoff(int valA1, int valA2, int valA3, int valA4, int valA5, int valA6, int valA7, int valA8, int valA9, int valA10)

you create local variables, unrelated to the globals that actually hold your recently read analog reading. Consequently, all values will be random garbage from the stack.
Instead, do this:
void crossingoff()
and your code within crossingoff() will use the values you are intending to check - if you create them as globals.

In the microcontroller world, IMO, the evils of global variables can be managed reasonably.

There is more, but fix that, in that function and the others, then report back, posting a new version to ensure you've understood the lesson.