This sketch is part of a bigger thing, the rest works fine

This sketch stalls within the if statement as indicated and I can't see what is wrong. Can anybody see what is wrong?

if(lapCount>3){
lapCount=0;
Serial.println("Train slowing");
delay(dv);
digitalWrite(orangePin,HIGH);
digitalWrite(redPin,HIGH);
digitalWrite(bluePin,HIGH);
Serial.println("Train stopped");
delay(dv);
delay(StStop);
digitalWrite(bluePin,LOW);
                                      // sketch does not get past this point
Serial.print("Lap count = ");
Serial.println(lapCount);
delay(dv);
}

Next line is a Serial.print so my guess is a memory overrun elsewhere in the code.

Sorry, I don't work with snippets I can't test. There are so many things that could be the cause of your bug and could be sitting elsewhere in your code appearing to work for the time being. It could be a divide by zero in a completely different function.

1 Like

What makes you think that this is enough information for us to be able to find the problem?

Post the whole sketch.

1 Like

Here is the whole thing :-

#include <LiquidCrystal.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

int lightPen=A0;
int lightVal;
int dv=2000;
int lival=450;
int j;
int lapCount;

int greenPin=2; //slow in zone B
int redPin=3; // Zone A stop
int bluePin=4; //Zone A slow
int greePin=5; //Zone A fast
int orangePin=6; //Zone B slow
int grePin=13; //Zone B fast

int dvl=500;
int dvs=2000;
int StStop=30000;

void setup() {
Serial.begin(9600);
lcd.begin(16,2);
lapCount=0;
pinMode(lightPen,INPUT);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(greePin,OUTPUT);
pinMode(grePin,OUTPUT);
pinMode(bluePin,OUTPUT);
pinMode(orangePin,OUTPUT);
}

void loop() {
lcd.setCursor(0,0);
lcd.print("Lap Count = ");
lcd.print(lapCount);
lightVal=analogRead(lightPen);
lcd.setCursor(0,2);
lcd.print("lightVal = ");
lcd.println(lightVal);
Serial.print("Lightval = ");
Serial.println(lightVal);
delay(dv);

if(lightVal<lival){
digitalWrite(orangePin,LOW); // ISO 1 & 3 closed - conducting current green
digitalWrite(redPin,LOW); // controller to fast in zone B green
digitalWrite(bluePin,LOW); // ISO 2 closed (current flow) green

lapCount=lapCount+1;

Serial.print("lapCount = ");
Serial.println(lapCount);
delay(dv);

if(lapCount>3){

Serial.println(" laps>3");
delay(dv);

digitalWrite(orangePin,HIGH); //Zone B slow, ISO 1 & 3 open (isolated) red
digitalWrite(redPin,HIGH); // Zone B slow - feed on slow red
digitalWrite(bluePin,HIGH); //Zone A stopped ISO 2 open (isolated) red

Serial.println("train stopped at station");
delay(dv);

delay(StStop); //station stop

digitalWrite(bluePin,LOW); //Zone A slow ISO 2 closed (current flow) green

lapCount=0;
delay(dv);

}

Serial.println("Moving out of station");
delay(dv);
}
Serial.println("Train cycling");
delay(dv);

}

Code in code tags, please.

And I think we might need to see a schematic. I don't know what your outputs are connected to and I'm wondering if one or more of them is behind the problem.

Add functional comments to the sketch to see what you want to happen during code processing.

Will this be a block controller for the model railway?

Hi,
Using functional comments has found the dead end. The program is intended to stop at the station after 3 laps of the layout.

Hi,
The outputs are to LEDs.
Rod

Hi,
What is the application?

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks... Tom... :grinning: :+1: :coffee: :australia:

Here is the whole thing:-Use code tags to format code for the forum

//LOAD NANO FROM COM5

#include <LiquidCrystal.h>
int rs = 7;
int en = 8;
int d4 = 9;
int d5 = 10;
int d6 = 11;
int d7 = 12;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int lightPen = A0;
int lightVal;
int dv = 2000;
int lival = 450;
int j;
int lapCount;

int greenPin = 2;   //slow in zone B
int redPin = 3;     // Zone A stop
int bluePin = 4;    //Zone A slow
int greePin = 5;    //Zone A fast
int orangePin = 6;  //Zone B slow
int grePin = 13;    //Zone B fast

int dvl = 500;
int dvs = 2000;
int StStop = 30000;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  lapCount = 0;
  pinMode(lightPen, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(greePin, OUTPUT);
  pinMode(grePin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(orangePin, OUTPUT);
}

void loop() {
  lcd.setCursor(0, 0);
  lcd.print("Lap Count = ");
  lcd.print(lapCount);
  lightVal = analogRead(lightPen);
  lcd.setCursor(0, 2);
  lcd.print("lightVal = ");
  lcd.println(lightVal);
  Serial.print("Lightval = ");
  Serial.println(lightVal);
  delay(dv);

  if (lightVal < lival) {
    digitalWrite(orangePin, LOW);  // ISO 1 & 3 closed - conducting current green
    digitalWrite(redPin, LOW);     // controller to fast in zone B green
    digitalWrite(bluePin, LOW);    // ISO 2 closed (current flow) green

    lapCount = lapCount + 1;

    Serial.print("lapCount = ");
    Serial.println(lapCount);
    delay(dv);

    if (lapCount > 3) {

      Serial.println(" laps>3");
      delay(dv);

      digitalWrite(orangePin, HIGH);  //Zone B slow, ISO 1 & 3 open (isolated) red
      digitalWrite(redPin, HIGH);     // Zone B slow - feed on slow red
      digitalWrite(bluePin, HIGH);    //Zone A stopped ISO 2 open (isolated)  red

      Serial.println("train stopped at station");
      delay(dv);

      delay(StStop);  //station stop

      digitalWrite(bluePin, LOW);  //Zone A slow ISO 2 closed  (current flow) green

      lapCount = 0;
      delay(dv);
    }

    Serial.println("Moving out of station");
    delay(dv);
  }
  Serial.println("Train cycling");
  delay(dv);
}

}`Use code tags to format code for the forum`

StStopSchematic.pdf (363.3 KB)

StStopSchematic.pdf (363.3 KB)

HiTom,

Try this.

Hi,
You only need to respond once with an answer, not once for each request.
Please post your images as jpg or png, then they will appear in you post.

Thanks for the schematic;

Where are the gnd connections to your relays?
How have you actually got the LDR connected?
Is it as a potential divider?

Please include how you have connected your LEDS.

Tom.. :grinning: :+1: :coffee: :australia:

Your code are simulated here:
"Trains-and-boats-and-plans - Wokwi ESP32, STM32, Arduino Simulator
When lapCount > 3 the delay is 30000, = 30 seconds, not a stall forever.

int StStop = 30000; delay(StStop); //station stop

Wait for 30 second and you see the program going ahead.

Hi Tom,

See attached photo. I have taken out the wires connecting the rails for clarity.

REgards

Rod

Hi,

The sketch stalls past the next line of code :-

delay(StStop); //station stop

digitalWrite(bluePin, LOW); //Zone A slow ISO 2 closed (current flow) green
// the sketch stalls here

lapCount = 0;

Regards

Rod

Hi, @rodsadgrove

Can you please post a link to spec/data of the relays?
How much current do the relay coils consume?

You do not have back EMF protection diodes on the relay coils.

How are you powering your project?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

It appears that your output pins are driving relays, not LEDs as you claimed. Are those SSRs, or are you driving inductive coils straight from an output pin without driving transistors or flyback diodes?

OK, I am driving LEDs thru the non latching relays. The green LEDs come on when the relay is not energised and the red LEDs are on when the relays are charged thru the output pins. When I get this sketch sorted out the LEDs will be replaced by other functions. The relays are Shrack PE014006.6VDC.

Hi,

The relays are Shrack PE 014006 6VDC non latching.
I intend energising the relays thru the OUTPUT pins.
I have a 5V charger to power the NANO.
What do you suggest for EMF protection?

Rod