Model Railway Turntable - in a spin so much Im dizzy

Since using CLOSED worked backwards with your Opto Sensor we could have swappd the definition around:

//original
#define CLOSED             LOW    
#define OPENED             HIGH

TO

//after swapping
#define CLOSED             HIGH
#define OPENED             LOW
  • That would fix the Opto sensor problem BUT what would happen to the code that looks at all the switches in your project ?
    :thinking:

I presume as we had dropped the word OPTO that every OPEN and every CLOSED would then also work backwards to what we currently have. In this case every switch would work incorrectly.

  • Yes we are just talking about the OPENED and CLOSED macros.

  • If we did the swap, all the switches would work in reverse.

Hence

  • We needed to create new Macros OPTOclosed and OPTOopened that defined the way the Opto Sensor works. (opposite to the switches)
  • You do understand the code that examines the Opto Sensor needs to be modified ?
  //========================================================================  OptoSensor.pin
  currentState = digitalRead(OptoSensor.pin);

  //===================================
  //has this switch changed state ?
  if (OptoSensor.lastState != currentState)
  {
    OptoSensor.counter++;

    //is this change in state stable ?
    if (OptoSensor.counter >= filter)
    {
      //get ready for the next sequence
      OptoSensor.counter = 0;

      //update to this new state
      OptoSensor.lastState = currentState;

      //========================
      //did this Opto sensor close ?
      if (currentState == OPTOclosed)
      {
        Serial.println("Opto Sensor Has Closed");
      }

      //========================
      //did this switch open ?
      else if (currentState == OPTOopened)
      {
        Serial.println("Opto Sensor has Opened");
      }
    }
  }

  //===================================
  //a valid switch change has not been confirmed
  else
  {
    OptoSensor.counter = 0;
  }

  //END of OptoSensor.pin

} //END of   checkSwitches()

I was aware that we needed to change the code but i couldn't work out where.
I had looked at the macro initially because we had used OPTO in the definition so didn't think it would effect the wider system as you described if we removed OPTO.
What i cant locate is where we are telling the system that the optical beam is normally made and then to register zero when broken.
I have updated my optoSensor.pin section of code to match the above and im struggling to work out where the initial current state is determined. even if i swap the
(currentState == OPTOclosed)
around with
//did this switch open ? else if (currentState == OPTOopened)
I am still not getting the desired effect.
Any Clues?

  • What you need to do is visit every line of code that has anything to do with the Opto Sensor.

For example:
Move your cursor to the top of the IDE.
Press <CTRL> <F>
This brings up the search Window.
Enter OPTOsensor in the Find box, click Find
2024-05-27_16-44-14

The first HIT should be:

//========================
makeInput OptoSensor =
{
  18, 0, OPENED, 0            //pin, switchTime, lastState, counter
};

What do you think needs to be done ?

FIrstly, that find bar is awesome, thank you. Along with Ctrl T yesterday, are there any other shortcuts i need to know as i didn't know any when we started.

So we need to change the OPENED to CLOSED.
I then needed to find the correct location to tell the motor to stop when this changes.
So on line 384

      //has the OptoSensor gone OPENED ?
      if (OptoSensor.lastState == OPENED) {
        //reset the "Reset State Machine"
        resetMachine = ResetFinished;

I changed this part of the code and bingo!
It now works.
Im not getting the reset part of code in the serial monitor though.
So i am currently looking at the checkmachine around line 444 to see if there is something in that.

  • No :frowning_face:

  • You need to make the change below.

//========================
makeInput OptoSensor =
{
  18, 0, OPTOopened, 0            //pin, switchTime, lastState, counter
};

Press Find for the next search, you should see:

        stepIndex = 7;
      }

      //has the OptoSensor gone CLOSED ?
      if (OptoSensor.lastState == CLOSED)
      {
        //reset the "Reset State Machine"
        resetMachine = ResetFinished;
        //we are done, disable this TIMER
        stepperTIMER.disableTIMER();
  • What needs to be done ?
  • You need to make the change below.
//========================
makeInput OptoSensor =
{
  18, 0, OPTOopened, 0            //pin, switchTime, lastState, counter
};

My bad. Code corrected. I thought i was doing so well there lol.
Gotta made some mistakes to learn from them i suppose.

  • Question is in Post #169

Press Find for the next search, you should see:

        stepIndex = 7;
      }

      //has the OptoSensor gone CLOSED ?
      if (OptoSensor.lastState == CLOSED)
      {
        //reset the "Reset State Machine"
        resetMachine = ResetFinished;
        //we are done, disable this TIMER
        stepperTIMER.disableTIMER();
  • What needs to be done ?

That is where i removed the
gone CLOSED ?
and

if (OptoSensor.lastState == CLOSED)

and replaced it with

//has the OptoSensor gone OPENED ?
      if (OptoSensor.lastState == OPENED)

Hi, @LarryD

Keep going mate, I think there are quite a few watching from the sidelines and learning quite a bit.

Almost a tutorial on its own.

Tom.. :smiley: :+1: :coffee: :australia:
PS @notridge13 Note, you are not the only one learning here, good project. (Model Railway
enthusiast here as well.) :+1: :+1: :+1:

1 Like

Hi Tom. Thank you! The words of encouragement are very much appreciated. Plus I'm glad this can also be of help to others.

        stepIndex = 7;
      }

      //has the OptoSensor gone CLOSED ?
      if (OptoSensor.lastState == CLOSED)
      {
        //reset the "Reset State Machine"
        resetMachine = ResetFinished;
        //we are done, disable this TIMER
        stepperTIMER.disableTIMER();

The 2nd code found should be changed to:

        stepIndex = 7;
      }

      //has the OptoSensor gone CLOSED ?
      if (OptoSensor.lastState == OPTOclosed)
      {
        //reset the "Reset State Machine"
        resetMachine = ResetFinished;
        //we are done, disable this TIMER
        stepperTIMER.disableTIMER();
  • What we are doing is, for the Opto Sensor only, changing all the CLOSED Macros to OPTOclosed and OPENED Macros to OPTOopened

  • At this point the changes made should get your setup working.

  • Does it ?

yes it does work as desired perfectly.
the only thing that is not occurring is the system reset message is not being printed in the serial monitor any longer.
Does that effect the way the system operates?
on line 323 under setup

  //configure the output pins
  for (byte x = 0; x < sizeof(outputPins); x++) {
    digitalWrite(outputPins[x], LOW);
    pinMode(outputPins[x], OUTPUT);
  }

  Serial.println("System Reset");

Is it not appearing because we have set this section as only writing 'system reset' when the pin goes LOW?
Im not sure why else this is nolonger occurring. although im also unsure of the above as it refers to an output pin and a sensor is an input. so even in writing this i think its wrong. But i cant find 'system reset' other than this entry.

Serial.println("System Reset");

  • The above code is found in setup( )

  • You could it with <CTRL> <F> to find it :wink:

  • There is a subtle item which we must consider.
    In the setup( ) routine the following code turns on pullup resistors for all switch inputs.
  //======================
  //use INPUT_PULLUP so the pin does not float, floating pins can cause faulty readings
  //configure the input pins
  for (byte x = 0; x < sizeof(inputPins); x++)
  {
    pinMode(inputPins[x], INPUT_PULLUP);
  }

The schematic below, I think, is your Opto Sensor
2024-05-27_17-18-04

Since the LM339 has a pull up resistor on the board we do not need to turn on the pin 18 pull up resistor (where your Opto sensor is connected).
Suggest you change this line of code from this:

}; //END of   struct makeInput
const byte inputPins[]           = {4, 14, 15, 16, 17, 18};

to this

}; //END of   struct makeInput
const byte inputPins[]           = {4, 14, 15, 16, 17};  //  remove 18
  • Have lost track, are there any questions we have not answered ?

Ok 18 removed from the code.
I dont believe there are any ive missed. But can you please clarify the areas i mentioned in post #176. Just so i know im understanding everything.

Also, you mention the opto sensor has an internal pullup resistor. Are you referring to the 10k attached to the '-' pin of LM393 and ground?
I know from my understanding of these resistors that effectively the LM393 would compare its + and - pins to give a 0 or 1 output.

  • If you want System Reset printed when the Opto Sensor goes closed, add the Serial.println line as seen below, found in the resetMachine.
    //========================
    case ResetFinished:
      {
        Serial.println("System Reset");
        
        //reset this State Machine
        resetMachine = ResetStartup;
      }
      break;