Debugging a published block control sketch

Firstly, I need to know if I am in the right place?

As a newbie I have copied an existing sketch from the project hub that does exactly what I want, but it comes as an addendum to an initial project "Block detection 3 aspect light control". The original code for this project compiles OK, but the addendum by '/Pilotincontrol', (which is a 2 aspect approach and exactly what I want) doesn't. There are clearly a couple of typos which I have corrected....e.g. Serial.printin, but the main issue seems to involve a define statement that has been commented out.

Is there a way of finding out the original authors (emails) of this project which has lain dormant for 2 years?

Or ,even better, is anybody out there familiar with this particular project?

In hope.

Dick Moger

What project?

As stated: " Block detection 3 aspect light control" as shown on Hackster.io
published july 9th 2017. Credits to lecurtis (/lecurtis)

This compiles OK, but is a bit short on what I want. A better configuration using only 2 aspects (red and Green) with an infra red sensor to open and close each block is presented on the same topic as a response to some of the failings in the original design. This alternative design was by PilotinControl.

Correspondence between the two then ensued and lecurtis then seems to have abandoned his initial 3 aspect design in favour of the sketch proposed by PilotinControl. At that point they then seem to have gone off -line as far as the Project Hub is concerned.

I am not party to the connection between ARDUINO and Hackster.

R Moger

The code by "Pilotincontrol" compiles fine for me.

// CONSTANT VARIABLES


const int eastblocksensor = A0; // BLOCK ONE
const int westblocksensor = A1; // BLOCK ONE
const int eastblocktwosensor = A2; // BLOCK TWO
const int westblocktwosensor = A3; // BLOCK TWO


// VARIABLES THAT WILL CHANGE


int tcrtEBlockState = 0; // BLOCK ONE variable for reading the TCRT5000 status
int tcrtWBlockState = 0; // BLOCK ONE
int tcrtEBlockTwoState = 0; // BLOCK TWO
int tcrtWBlockTwoState = 0; // BLOCK TWO


int BLOCKONEGREEN = 11; // BLOCK ONE
int BLOCKONERED = 12; // BLOCK ONE
int BLOCKTWOGREEN = 10; // BLOCK TWO
int BLOCKTWORED = 9; // BLOCK TWO


//#define BLOCKONESTATE
//#define BLOCKLIGHTSTATES


void setup()
{


  // initialize the LED pin as an output:


  Serial.begin(9600);
  pinMode(BLOCKONEGREEN, OUTPUT); // BLOCK ONE
  pinMode(BLOCKONERED, OUTPUT); // BLOCK ONE
  pinMode(BLOCKTWOGREEN, OUTPUT); // BLOCK TWO
  pinMode(BLOCKTWORED, OUTPUT); // BLOCK TWO


  //pinMode(led, OUTPUT);


  // initialize the tcrt5000 pin as an input, and turn on the internal pullup resistors:


  pinMode(westblocksensor, INPUT); // A0 BLOCK ONE
  pinMode(eastblocksensor, INPUT); // A1 BLOCK ONE
  pinMode(westblocktwosensor, INPUT); // A2 BLOCK TWO
  pinMode(eastblocktwosensor, INPUT); // A3 BLOCK TWO
  //digitalWrite(eastsensor, HIGH);
  //digitalWrite(westsensor, HIGH);
  digitalWrite(BLOCKONEGREEN, HIGH); // DEFAULT GREEN ON
  digitalWrite(BLOCKTWOGREEN, HIGH); // DEFAULT GREEN ON
} // END OF MASTER SETUP


// BLOCK LIGHT STATES


enum BLOCKLIGHTSTATES
{
  ST_GREEN, // GREEN
  ST_GREENTWO, // GREEN TWO
  ST_RED, // RED
  ST_REDTWO, // RED TWO
};


enum BLOCKTWOLIGHTSTATES
{
  ST_GREENTHREE, // GREEN THREE
  ST_GREENFOUR, // GREEN FOUR
  ST_REDTHREE, // RED THREE
  ST_REDFOUR, // RED FOUR
};


// STATE OF BLOCK ONE GREEN


BLOCKLIGHTSTATES BLOCKONESTATE = ST_GREEN; // BLOCK ONE
BLOCKTWOLIGHTSTATES BLOCKTWOSTATE = ST_GREENTHREE; // BLOCK TWO


// CURRENT TIME


static unsigned long currentTime;


void loop()
{
  // GET CURRENT TIME
  currentTime = millis();


  // read the state of the tcrt5000:
  tcrtEBlockState = digitalRead(eastblocksensor); // BLOCK ONE
  tcrtWBlockState = digitalRead(westblocksensor); // BLOCK ONE
  tcrtEBlockTwoState = digitalRead(eastblocktwosensor); // BLOCK TWO
  tcrtWBlockTwoState = digitalRead(westblocktwosensor); // BLOCK TWO
  // check if the tcrt5000 sensor detects something.
  // if it is, the tcrtState is LOW:
  //digitalWrite(RIGHT, LOW);
  //digitalWrite(LEFT, LOW);


  Serial.println(tcrtEBlockState); // BLOCK ONE
  Serial.print(tcrtWBlockState); // BLOCK ONE
  Serial.print(tcrtEBlockTwoState); // BLOCK TWO
  Serial.print(tcrtWBlockTwoState); // BLOCK TWO


  switch (BLOCKONESTATE)
  {


    // GREEN LIGHT WILL SWITCH TO RED IF SENSORS ARE TRIGGERED


    case ST_GREEN:
      BLOCK1GREEN(tcrtEBlockState, tcrtWBlockState);
      break;
    case ST_GREENTWO:
      BLOCK1GREEN1(tcrtEBlockState, tcrtWBlockState);
      break;
    case ST_RED:
      BLOCK1RED(tcrtEBlockState, tcrtWBlockState);
      break;
    case ST_REDTWO:
      BLOCK1RED1(tcrtEBlockState, tcrtWBlockState);
      break;
  }


  switch (BLOCKTWOSTATE)
  {


    // GREEN LIGHT WILL SWITCH TO RED IF SENSORS ARE TRIGGERED


    case ST_GREENTHREE:
      BLOCK2GREEN(tcrtEBlockTwoState, tcrtWBlockTwoState);
      break;
    case ST_GREENFOUR:
      BLOCK2GREEN2(tcrtEBlockTwoState, tcrtWBlockTwoState);
      break;
    case ST_REDTHREE:
      BLOCK2RED(tcrtEBlockTwoState, tcrtWBlockTwoState);
      break;
    case ST_REDFOUR:
      BLOCK2RED2(tcrtEBlockTwoState, tcrtWBlockTwoState);
      break;
  }


} // END MASTER LOOP


// BLOCK ONE STATE


void BLOCK1GREEN(int tcrtEBlockState, int tcrtWBlockState)
{
  digitalWrite(BLOCKONEGREEN, HIGH);
  digitalWrite(BLOCKONERED, LOW);


  if (tcrtEBlockState == LOW && tcrtWBlockState == HIGH)
  {
    // CHANGE TO RED
    BLOCKONESTATE = ST_RED;
  }
  else if (tcrtEBlockState == HIGH && tcrtWBlockState == LOW)
  {
    // CHANGE TO RED
    BLOCKONESTATE = ST_REDTWO;
  }


} //END VOID


void BLOCK1GREEN1(int tcrtEBlockState, int tcrtWBlockState)
{


  // DURATION RED TIMER
  const unsigned long duration = 2000;


  // START TIME
  static unsigned long startTime = 0;


  if (startTime == 0)
  {


    // TURN RED TIMER
    digitalWrite(BLOCKONEGREEN, LOW);
    digitalWrite(BLOCKONERED, HIGH);


    // START TIMER


    startTime = currentTime;


    // DO NOTHING
    return;
  }


  // IF UNKNOWN SECONDS HAVE PASSED
  if (currentTime - startTime >= duration)
  {


    // RESET VARIABLES START FROM ZERO
    startTime = 0;


    // CHANGE TO GREEN
    BLOCKONESTATE = ST_GREEN;
  }


} // END VOID


void BLOCK1RED(int tcrtEBlockState, int tcrtWBlockState)
{
  digitalWrite(BLOCKONEGREEN, LOW);
  digitalWrite(BLOCKONERED, HIGH);


  if (tcrtEBlockState == HIGH && tcrtWBlockState == LOW)
  {
    // CHANGE TO GREEN
    BLOCKONESTATE = ST_GREENTWO; // TIMER
  }


} // END VOID


void BLOCK1RED1(int tcrtEBlockState, int tcrtWBlockState)
{
  digitalWrite(BLOCKONEGREEN, LOW);
  digitalWrite(BLOCKONERED, HIGH);


  if (tcrtEBlockState == LOW && tcrtWBlockState == HIGH)
  {


    // CHANGE TO GREEN
    BLOCKONESTATE = ST_GREENTWO; // TIMER
  }


} // END VOID


// BLOCK TWO STATE


void BLOCK2GREEN(int tcrtEBlockTwoState, int tcrtWBlockTwoState)
{
  digitalWrite(BLOCKTWOGREEN, HIGH);
  digitalWrite(BLOCKTWORED, LOW);


  if (tcrtEBlockTwoState == LOW && tcrtWBlockTwoState == HIGH)
  {
    // CHANGE TO RED
    BLOCKTWOSTATE = ST_REDTHREE;
  }
  else if (tcrtEBlockTwoState == HIGH && tcrtWBlockTwoState == LOW)
  {
    // CHANGE TO RED
    BLOCKTWOSTATE = ST_REDFOUR;
  }
}


void BLOCK2GREEN2(int tcrtEBlockTwoState, int tcrtWBlockTwoState)
{


  // DURATION RED TIMER
  const unsigned long duration = 2000;


  // START TIME
  static unsigned long startTime = 0;


  if (startTime == 0)
  {


    // TURN RED TIMER
    digitalWrite(BLOCKTWOGREEN, LOW);
    digitalWrite(BLOCKTWORED, HIGH);


    // START TIMER


    startTime = currentTime;


    // DO NOTHING
    return;
  }


  // IF UNKNOWN SECONDS HAVE PASSED
  if (currentTime - startTime >= duration)
  {


    // RESET VARIABLES START FROM ZERO
    startTime = 0;


    // CHANGE TO GREEN
    BLOCKTWOSTATE = ST_GREENTHREE;
  }
}


void BLOCK2RED(int tcrtEBlockTwoState, int tcrtWBlockTwoState)
{
  digitalWrite(BLOCKTWOGREEN, LOW);
  digitalWrite(BLOCKTWORED, HIGH);


  if (tcrtEBlockTwoState == HIGH && tcrtWBlockTwoState == LOW)
  {


    // CHANGE TO GREEN
    BLOCKTWOSTATE = ST_GREENFOUR;
  }
}


void BLOCK2RED2(int tcrtEBlockTwoState, int tcrtWBlockTwoState)
{
  digitalWrite(BLOCKTWOGREEN, LOW);
  digitalWrite(BLOCKTWORED, HIGH);


  if (tcrtEBlockTwoState == LOW && tcrtWBlockTwoState == HIGH)
  {


    // CHANGE TO GREEN
    BLOCKTWOSTATE = ST_GREENFOUR;
  }
}

This is excellent news! The only difference I can spot (alas with ageing eyesight) is that I read the first line of the serial.print section as "serial.println and assumed it was a typo.....
You have it as "serial.print1n"... (1 not l!)

Let us hope that it is simply that! By the way, did type the whole thing in from scratch like I did, or did you find a way to upload it?

I am grateful for your help here..

Dick Moger

dickmoger:
By the way, did type the whole thing in from scratch like I did, or did you find a way to upload it?

Why the heck would you type it all out? Stop whatever you're doing right now and learn how to copy and paste. This is an absolutely essential skill for any computer user, especially someone who is involved in programming:

dickmoger:
As stated: " Block detection 3 aspect light control" as shown on Hackster.io
published july 9th 2017. Credits to lecurtis (/lecurtis)

While you're at it, learn what URLs are and how to use them. It's crazy to expect the people you're asking free help from to go searching for a page when you could provide a link we could just click to get right there.

dickmoger:
By the way, did type the whole thing in from scratch like I did, or did you find a way to upload it?
I am grateful for your help here..
Dick Moger

I used Select, Copy, and Paste which is the THIRD thing to try.
The first method to look for is some kind of 'download' button. It sounds like you know what to do there.
If you don't see one, the next thing to look for is a "RAW" link. This will typically open the source file as a page of plain text. Then you can use Select All (Ctrl-A, Command-A, or Edit->Select All) to select the contents of the file. Then you can use Copy (Ctrl-C, Command-C, or Edit->Copy) to make a copy in your computer's clipboard and use Paste (Ctrl-V, Command-V, or Edit->Paste) to paste the contents of the clipboard into an editor window. If the editor window contains text you want to get rid of (like an old sketch) use Select All in the editor window before the Paste. Any text selected when you do the Paste will be removed.
If neither of these methods is available you will have to manually select the part of the web page that contains the source code. That's not always easy but generally holding down the mouse button just before the first letter and releasing the mouse button after dragging just past the last character will get you close. You might also be able to Click the mouse button just before the first character and Shift-Click the mouse button just after the last character. With the desired text select, use the Copy and Paste as above. WARNING: This will often include strange characters like 'non-breaking spaces' and 'em-dash' which look like normal characters but cause "Unexpected character" errors when you try to compile. You may have to re-type each line that generates an error.

dickmoger:
As stated: " Block detection 3 aspect light control" as shown on Hackster.io
published july 9th 2017. Credits to lecurtis (/lecurtis)

It's a million times easier for those that want to help you if you post a direct link, preferably clickable.

Point taken... But I note that you are 'returning after 15 years' ... I retired from IBM some 27 years ago where I was , among other things a hardware specialist and technical writer... "Software" for me was entering loops into a 360 /40 by hand in machine language!

Things have moved on ever so slightly and I have tried to brave this new world by moving out of my hardware comfort zone, which is not so easy when your eyesight is no longer 20/20.... Taking short cuts is going to be something of a necessity.

I have built a fairly large steam operated garden railway, half of which is semaphore signalled entirely by hardware using laser sensors.. The remaining half has passing loops as an additional complication, hence the thought that the Arduino approach will give more versatility.

It will take time, so your patience will be appreciated :slight_smile:

As a footnote, I now realise that the serial.println is nothing more than a carriage return and nothing to do with the assembling problem... Which is still unresolved and almost certainly due to my mistyping.

It currently stops on the first switch...case statement... switch (BLOCKONESTATE) {

case ST_GREEN:

BLOCK1GREEN (tcrtEBlockState, tcrtWBlockState);

This last line it says is not in the scope

I can't detect any mistype, but then I can't tell my l from my 1 anyway.

My best shot is to try and upload the code as suggested so that mistypes are eliminated.

Thanks again to all..

Dick Moger

dickmoger:
It currently stops on the first switch...case statement... switch (BLOCKONESTATE) {

case ST_GREEN:

BLOCK1GREEN (tcrtEBlockState, tcrtWBlockState);

This last line it says is not in the scope

The error message will say which name is "not declared in this scope". If the name is a function name there is a chance that the function failed to compile because of some error further down. Look for error messages relating to the part of the file containing the missing function.

I had assumed that such was the case, but so far not identified the flaw in my typing!

Fortunately the cut and paste worked and I now have a fully assembled set of code.

A bit too easy that... I naturally assumed that a proper download would be required..

I will compare them bit by bit when I get the enthusiasm..

In the meantime, thanks to all of you!

All I have to do now is change the outputs from LEDs to servos for operating the semaphore arms via an MERG quad servo driver.

Signing off

Dick Moger