Help with code structure

Hi, I'm a novice programmer, but in need of some guidance. My code is for a stepper motor clock using two stepper motors. The motors each use an A4988 driver module, and my code loop starts by checking the state change input pulses on pin 3 of my Nano. Each pulse has a 1 second duration, and the minutes motor steps once every minute. The second part of my code waits for each hour (3600 seconds) at which point the hours motor advances 5 steps, within each 5 minute interval, making a 12 hour period.

My code compiles and runs fine, with each minute rotation completed. However on each hour the hour dial should complete 4 steps, for each hour shown on the 12 hour dial, It will only move two steps at a time.

If I compile and run just the hour section of my code to test it, I can get the 4 steps as required to show each hour, which runs fine. My problem is running the minutes and hours together within the my program .

Maybe I'm missing something, also as each hour advances (3600 seconds) the pulse counter should reset back to zero, and I'm not certain it does?`/*

/*
 Clock experimental code. Timer module supplies 1 second pulses.
 After 60 one second pulses are counted, stepper drive motor is moved to next 1 minute segment
 Version Dated 30/06/2023
 Uses A4988 stepper driver module
*/

// this constant won't change:
const int clockPin = 3;  //  clock signal input pin
const int ledPin = 13;   // the pin that the LED is attached to
const int ledPinb = 9;   // no used
#define stepPina 2    // minutes driver
#define stepPinb 7   // hours driver
#define dirPina 4   // minutes motor direction
#define dirPinb 5   // motor hours direction
#define enPina 6  // enable motor driver module minutes
#define enPinb 8   // enable motor driver module hours

// Variables will change:
int clockCounter = 0;  // counter for the number of clcok pulses
int clockState = 0;        // current state of the clock signal pulse
int lastClockState = 0;    // previous state of the clock signal pulse

void setup() {
 
 pinMode(clockPin, INPUT);
 pinMode(ledPin, OUTPUT);
 pinMode(ledPinb, OUTPUT);
 pinMode(dirPina, OUTPUT);
}
void loop() {
 // read the clock input pin:  read 1sec pulse
 clockState = digitalRead(clockPin);
 // compare the clockState to its previous state
 if (clockState != lastClockState) {
   // if the state has changed, increment the counter
   if (clockState == LOW) {  // LOW
     // if the current state is HIGH then the clock went from off to on:
     clockCounter++;
   }
   // save the current state as the last state, for next time through the loop
   lastClockState = clockState;
   if (clockCounter % 60 == 0)  // 1 minute elapsed //// turns on the LED every 60 seconds
   { 

     // digitalWrite(ledPin, HIGH);  // not used
     //digitalWrite (enPina, LOW);  // stepper drive enable
     digitalWrite(dirPina, HIGH);  // Enables the motor to move in a particular direction
     for (int x = 0; x < 63; x++) {  // number of steps ( 5  > 10 > 15> 20.....)
       digitalWrite(stepPina, HIGH); // enable driver module
     digitalWrite(stepPina, LOW);     // disable driver module 
     //digitalWrite (enPina,HIGH);   // stepper drive enable
     // digitalWrite(ledPin, LOW);   // not used
     }
   //+++++++++++++++++++++++++++++++++++++++++++++++++ hour stepper driver +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

   if (clockCounter % 120 == 0)  // 1 hour elapsed ( enter seconds) 120 = test only
    {digitalWrite(dirPinb, HIGH);    // motor direction
      digitalWrite (enPinb,LOW);      // enable driver module (A4988)
     for (int x = 0; x <4; x++) {   // number of steps
       digitalWrite(stepPinb, HIGH);  // start stepping hours motor
        delayMicroseconds(7000);   
       digitalWrite(stepPinb, LOW);
       delayMicroseconds(7000);
        digitalWrite(enPinb, HIGH);  // disable drive motor
      
     }
   }
 }
}
}

If you have doubts, put in Serial.print() statements to check the value.

1 Like

shouldn't the code not continue within this condition if the clockState == HIGH? for example, won't that result in two executions for the condition when

?

why not

void loop()
{
    clockState = digitalRead(clockPin);
    if (clockState != lastClockState) {
        lastClockState = clockState;

        if (clockState == HIGH)
            return;

        if (clockCounter % 60 == 0)

Thanks for your prompt reply, I copied it from a push-button example and altered it to accept the 1 second clock pulses, which advances the time on each minute and when the count reaches 3600, the hour should advances the four steps as needed each hour. The pulse counter works fine, although I will try your code example which appears to be a better presentation, thanks.
As for a serial print out, I did make an attempt but couldn’t work out how to enter the actual pulse counting in code?

if (clockCounter % 120 == 0)  // 1 hour elapsed ( enter seconds) 120 = test only
    {digitalWrite(dirPinb, HIGH);    // motor direction
      digitalWrite (enPinb,LOW);      // enable driver module (A4988)
     for (int x = 0; x <4; x++) {   // number of steps
       digitalWrite(stepPinb, HIGH);  // start stepping hours motor
        delayMicroseconds(7000);   
       digitalWrite(stepPinb, LOW);
       delayMicroseconds(7000);
        digitalWrite(enPinb, HIGH);  // disable drive motor
      
     }
   }

This doesn't look right as you disable the driver first pass through the for loop and you are relying on the clock counter value for repeats.
if (clockCounter % 120 == 0)

With the 2x delay of 7 ms the clock counter might not be stable at 120 for the four passes required.

also as each hour advances (3600 seconds) the pulse counter should reset back to zero, and I'm not certain it does?`

I don't see anywhere in the code where you reset the counter.

i'm puzzled by your code

  • why aren't dirPinb, enPina, enPinb, stepPina and stepPinb configured as outputs?
  • why aren't there delays between the stepPina writes?
  • is the motor stepped 63 time (not 60) each minute instead of just once or 63 times over the course of a minute?
  • how is it that the hour has elapsed every other minute (clockCounter %120)?

look the following over. it's often much easier to debug code in simulation rather than on the target. the simulation can run much faster.

the following reorganizes the code into a few sub-functions making is easier to understand and i added prints to indicate when each is invoked

output:

tic: 1
tic: 2
tic: 3
tic: 4
tic: 5
tic: 6
tic: 7
tic: 8
tic: 9
tic: 10
tic: 11
tic: 12
tic: 13
tic: 14
tic: 15
tic: 16
tic: 17
tic: 18
tic: 19
tic: 20
tic: 21
tic: 22
tic: 23
tic: 24
tic: 25
tic: 26
tic: 27
tic: 28
tic: 29
tic: 30
tic: 31
tic: 32
tic: 33
tic: 34
tic: 35
tic: 36
tic: 37
tic: 38
tic: 39
tic: 40
tic: 41
tic: 42
tic: 43
tic: 44
tic: 45
tic: 46
tic: 47
tic: 48
tic: 49
tic: 50
tic: 51
tic: 52
tic: 53
tic: 54
tic: 55
tic: 56
tic: 57
tic: 58
tic: 59
tic: 60
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
tic: 61
tic: 62
tic: 63
tic: 64
tic: 65
tic: 66
tic: 67
tic: 68
tic: 69
tic: 70
tic: 71
tic: 72
tic: 73
tic: 74
tic: 75
tic: 76
tic: 77
tic: 78
tic: 79
tic: 80
tic: 81
tic: 82
tic: 83
tic: 84
tic: 85
tic: 86
tic: 87
tic: 88
tic: 89
tic: 90
tic: 91
tic: 92
tic: 93
tic: 94
tic: 95
tic: 96
tic: 97
tic: 98
tic: 99
tic: 100
tic: 101
tic: 102
tic: 103
tic: 104
tic: 105
tic: 106
tic: 107
tic: 108
tic: 109
tic: 110
tic: 111
tic: 112
tic: 113
tic: 114
tic: 115
tic: 116
tic: 117
tic: 118
tic: 119
tic: 120
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 2
  step: 8
  step: 8
  step: 8
  step: 8
tic: 121
tic: 122
tic: 123
tic: 124
tic: 125
/*
Clock experimental code. Timer module supplies 1 second pulses.
After 60 one second pulses are counted, stepper drive motor is moved to next 1 minute segment
Version Dated 30/06/2023
Uses A4988 stepper driver module
*/

// this constant won't change:
const int clockPin = 3;  //  clock signal input pin
const int ledPin = 13;   // the pin that the LED is attached to
const int ledPinb = 9;   // no used
#define stepPina 2    // minutes driver
#define stepPinb 7   // hours driver
#define dirPina 4   // minutes motor direction
#define dirPinb 5   // motor hours direction
#define enPina 6  // enable motor driver module minutes
#define enPinb 8   // enable motor driver module hours

// Variables will change:
int clockCounter = 0;  // counter for the number of clcok pulses
int clockState = 0;        // current state of the clock signal pulse
int lastClockState = 0;    // previous state of the clock signal pulse


// -----------------------------------------------------------------------------
void
step (
    const byte PinStep )
{
    Serial.print   ("  step: ");
    Serial.println (PinStep);

    digitalWrite      (PinStep, HIGH);
    delayMicroseconds (7000);
    digitalWrite      (PinStep, LOW);
    delayMicroseconds (7000);
}

// -----------------------------------------------------------------------------
void
tic (void)
{
    Serial.print   ("tic: ");
    Serial.println (clockCounter);

    if (clockCounter % 60 == 0) {
        for (int x = 0; x < 63; x++) 
            step (stepPina);

        // 1 hour elapsed ( enter seconds) 120 = test only
        if (clockCounter % 120 == 0) {
            for (int x = 0; x <4; x++)
                step (enPinb);
        }
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
#define Test
#ifndef Test
    clockState = digitalRead (clockPin);
    if (clockState != lastClockState) {
        lastClockState = clockState;

        if (clockState == LOW)
            tic ();
#endif
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (clockPin, INPUT);

    pinMode (ledPin,   OUTPUT);
    pinMode (ledPinb,  OUTPUT);

    pinMode (enPina,   OUTPUT);
    pinMode (enPinb,   OUTPUT);
    pinMode (dirPina,  OUTPUT);
    pinMode (dirPinb,  OUTPUT);

    digitalWrite (enPina,  LOW);
    digitalWrite (enPinb,  LOW);
    digitalWrite (dirPina, HIGH);
    digitalWrite (dirPinb, HIGH);

#ifdef Test
    for (int n = 0; n < 125; n++)  {
        clockCounter++;
        tic ();
    }
#endif
}

@ gcjr Thanks for your prompt response, I've made so many versions and alterations during this project over time, my limited programming skills don't help, thanks for pointing out my errors. I had missed off the outputs as you pointed out. There are no delays between stepPina & b it's purely experimental, espicially when using the steppers in rotation mode to vary the speed. I only use them to make small steps at a time clockwise. The hours stepPinb are 700Microsecond pulsed at 4 steps. Your comment regarding 60 rather than 63 just seem to work and give the correct stepping distance. The two stepper motors I had to hand are odd, 7.5dg per step = 48 steps per revolution. The 120 count I use for testing the hour clock stepping, I only have to wait 2 minutes rather than each hour (3600 seconds) to run my checks. Hope this answers some of your questions. I thankyou for your assistance which I can move forward with.
I did mention that splitting the code into two sections and testing the minutes and hours in seperate individual programs test and run without faults, its only when they are combined together to show the hours and minutes of the working clock.

@ cattledog, Thanks for your reply,

This doesn't look right as you disable the driver first pass through the for loop and you are relying on the clock counter value for repeats.
if (clockCounter % 120 == 0)

With the 2x delay of 7 ms the clock counter might not be stable at 120 for the four passes required.

also as each hour advances (3600 seconds) the pulse counter should reset back to zero, and I'm not certain it does?`
the 120 which equates to 2 minutes gives me a chance to check the hour step motor is working correctly i.e. 4 steps rather than 2. I will test it for a longer period and see if it makes a difference. My attempt at "Serial print" didn't work out! Thanks for your comments

``/*
Clock experimental code. Timer module supplies 1 second pulses.
After 60 one second pulses are counted, stepper drive motor is moved to next 1 minute segment
Version Dated 11/07/2023
Uses A4988 stepper driver module
*/

// this constant won't change:
const int clockPin = 3; // clock signal input pin
const int ledPin = 13; // the pin that the LED is attached to
const int sensorPin = 9; // opto sensor
int sensorState = 0; // current state of the button
int lastsensorState = 0;
#define stepPina 2 // minutes driver
#define stepPinb 7 // hours driver
#define dirPina 4 // minutes motor direction
#define dirPinb 5 // motor hours direction
#define enPina 6 // enable motor driver module minutes
#define enPinb 8 // enable motor driver module hours

// Variables will change:
int clockCounter = 0; // counter for the number of clock pulses
int clockState = 0; // current state of the clock signal pulse
int lastClockState = 0; // previous state of the clock signal pulse

void setup() {

pinMode(clockPin, INPUT);
pinMode(sensorPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(dirPina, OUTPUT);
pinMode(dirPinb, OUTPUT);
pinMode(enPina, OUTPUT);
pinMode(enPinb, OUTPUT);
}
void loop() {
// read the clock input pin: read 1sec pulse
clockState = digitalRead(clockPin);
// compare the clockState to its previous state
if (clockState != lastClockState)
// if the state has changed, increment the counter
if (clockState == LOW) { // LOW
// if the current state is HIGH then the clock went from off to on:
clockCounter++;
}
// save the current state as the last state, for next time through the loop
lastClockState = clockState;
if (clockCounter % 60 == 0) // 1 minute elapsed
{
//digitalWrite(enPinb, HIGH);
// digitalWrite(enPina, LOW); // stepper drive enable
digitalWrite(dirPina, HIGH); // Enables the motor to move in a particular direction
for (int x = 0; x < 5; x++) { // number of steps ( 5
digitalWrite(stepPina, HIGH); // enable driver module
digitalWrite(stepPina, LOW); // disable driver module
digitalWrite (enPinb,HIGH); // stepper drive enable
}
if (sensorState == HIGH) { // opto hour sensor
}
}
//++++++++++++++++++++++++++++++ hour stepper driver ++++++++++++++++++++++++++++++++++
// This next section is the hour timer and steps each hour on the hour, must run seperate to minutes timer program above
//void setupb () ??
{

      digitalWrite(ledPin, HIGH);
      digitalWrite(enPinb, LOW);  // enable driver module (A4988)
   { digitalWrite(dirPinb, HIGH);     // motor direction

    for (int x = 0; x < 4; x++) {    // number of steps
      digitalWrite(stepPinb, HIGH);  // start stepping hours motor
      delayMicroseconds(7000);
      digitalWrite(stepPinb, LOW);
      delayMicroseconds(7000);
     delay(500);
      digitalWrite(ledPin, LOW);
      //digitalWrite(enPinb, HIGH);  // disable drive motor
    }

 }

}
}```
`Hi, thanks again for the help I recently received. I've revised my code and I will try to explain how my program works. As already mentioned I'm very much a novice when it comes to writing code.
The code is for a twin dial clock run by two stepper motors, one motor for the minutes dial (0>60 in 5 mins interval steps) code prefixes ref (a) The other stepper is for the hours (1 > 12hrs) prefixes ref (b). The clock receives 1 second pulses from a reclaim clock module, the counter counts 60 seconds for each minute for the stepper to advance.
The next event happens once only on each hour, when the hours stepper motor receives a pulse via an opto coupler. This is where my problems start, because the code runs in a loop, and each motor is pulsed, individually, conflict with delays occur as each motor loops, causing erratic performance.

What I need to happen is as soon as the hour period is signalled to begin running the hours stepper motor (see code below my marked border line) the code loops independently within the hours section, then loops back the hour stepper motor has completed its task, until the next hour repeats the process. So is it possible to have two loops, one for the minutes, and another for the hours?but combined in the same program? I've included my code which compiles ok, and both the minutes and hours both work and step as individual programs. The only other alternative would to use two Nano's.
If it is possible can a member show me how it can be done, or even slot an alteration into my code.
Help most appreciated.``

Your code has gotten mangled without the use of code tags. Can you please post the individual programs which work, and your attempt to combine them. All code in code tags please.

What is providing the hour signal to pin 9? How long does the high pulse last? What resets it back to low?

@cattledog, thanks for your prompt reply, I used the code tags as I have always done? So I'm puzzled as to how it's gone wrong? I can read it. Pin9 is an input from a slotted opto device, which provides the hour pulse. The code for the minutes and hour were individual programs to test the stepper motors used in the project, as they were different types. I then combined them into one sketch as posted. Because the stepper motors are critical to pulse timing, the program looping upsets the working of the motors. What I want to achieve if possible is to have the second part of my sketch when the digital write HIGH pin 9 starts the hour stepper (This is labelled up 1 hour stepper) operates in its own loop, before returning to main. i.e. minutes set up in one loop and hours set up in another, but in one running program.

````Use code tags to format code for the forum`
 if (sensorState == HIGH) {  // opto hour sensor
    }

Where in the code do you read the sensorPin to determine the sensorState?

look this over

/*
Clock experimental code. Timer module supplies 1 second pulses.
After 60 one second pulses are counted, stepper drive motor is moved to next 1 minute segment
Version Dated 11/07/2023
Uses A4988 stepper driver module
*/

// this constant won't change:
const int clockPin  = 3;  //  clock signal input pin
const int ledPin    = 13; // the pin that the LED is attached to
const int sensorPin = 9;  // opto sensor

const int stepPina  = 2;  // minutes driver
const int stepPinb  = 7;  // hours driver
const int dirPina   = 4;  // minutes motor direction
const int dirPinb   = 5;  // motor hours direction
const int enPina    = 6;  // enable motor driver module minutes
const int enPinb    = 8;  // enable motor driver module hours

int sensorState     = 0;      // current state of the button
int lastsensorState = 0;

// Variables will change:
int clockCounter = 0;    // counter for the number of clock pulses
int lastClockState = 0;  // previous state of the clock signal pulse

// -----------------------------------------------------------------------------
void
step (
    const byte pin )
{
    Serial.print ("  step: ");
    Serial.println  (pin);

    digitalWrite      (pin, HIGH);
    delayMicroseconds (7000);
    digitalWrite      (pin, LOW);
    delayMicroseconds (7000);
}

// -----------------------------------------------------------------------------
void
tic ()
{
    clockCounter++;
    if (clockCounter % 60 == 0) { // 1 minute elapsed
        Serial.print ("tic: min ");
        Serial.println (clockCounter);

        for (int x = 0; x < 5; x++)
            step (stepPina);
    }

    if (clockCounter % 3600 == 0) { // 1 hour elapsed
        Serial.print ("tic: hour ");
        Serial.println (clockCounter);

        for (int x = 0; x < 4; x++)
            step (stepPinb);

        digitalWrite (ledPin, HIGH);
        delay (500);
        digitalWrite (ledPin, LOW);
    }
}


// -----------------------------------------------------------------------------
void loop ()
{
#define Test
#ifdef Test
    tic ();
    delay (10);
#else
    byte clockState = digitalRead (clockPin);
    if (clockState != lastClockState)  {
        lastClockState = clockState;

        if (clockState == LOW)
            tic ();
    }
#endif
}

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    pinMode (clockPin,  INPUT);
    pinMode (sensorPin, INPUT);

    pinMode (ledPin,  OUTPUT);

    pinMode (dirPina, OUTPUT);
    pinMode (dirPinb, OUTPUT);
    pinMode (enPina,  OUTPUT);
    pinMode (enPinb,  OUTPUT);

    digitalWrite (dirPina, HIGH);
    digitalWrite (dirPinb, HIGH);

    digitalWrite (enPina, HIGH);
    digitalWrite (enPinb, HIGH);
}
/*
  Clock experimental code. Timer module supplies 1 second pulses.
  After 60 one second pulses are counted, stepper drive motor is moved to next 1 minute segment
  Version Dated 11/07/2023
  Uses A4988 stepper driver module
*/

// this constant won't change:
const int clockPin = 3;   //  clock signal input pin
const int ledPin = 13;    // the pin that the LED is attached to
const int sensorPin = 9;  // opto sensor
int sensorState = 0;      // current state of the button
int lastsensorState = 0;
#define stepPina 2  // minutes driver
#define stepPinb 7  // hours driver
#define dirPina 4   // minutes motor direction
#define dirPinb 5   // motor hours direction
#define enPina 6    // enable motor driver module minutes
#define enPinb 8    // enable motor driver module hours

// Variables will change:
int clockCounter = 0;    // counter for the number of clock pulses
int clockState = 0;      // current state of the clock signal pulse
int lastClockState = 0;  // previous state of the clock signal pulse

void setup() {

  pinMode(clockPin, INPUT);
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(dirPina, OUTPUT);
  pinMode(dirPinb, OUTPUT);
  pinMode(enPina, OUTPUT);
  pinMode(enPinb, OUTPUT);
}
void loop() {
  // read the clock input pin:  read 1sec pulse
  clockState = digitalRead(clockPin);
  // compare the clockState to its previous state
  if (clockState != lastClockState)
    // if the state has changed, increment the counter
    if (clockState == LOW) {  // LOW
      // if the current state is HIGH then the clock went from off to on:
      clockCounter++;
    }
  // save the current state as the last state, for next time through the loop
  lastClockState = clockState;
  if (clockCounter % 60 == 0)  // 1 minute elapsed
  {
    //digitalWrite(enPinb, HIGH);
    // digitalWrite(enPina, LOW);       // stepper drive enable
    digitalWrite(dirPina, HIGH);     // Enables the motor to move in a particular direction
    for (int x = 0; x < 5; x++) {   // number of steps ( 5
      digitalWrite(stepPina, HIGH);  // enable driver module
      digitalWrite(stepPina, LOW);   // disable driver module
      digitalWrite (enPinb, HIGH);  // stepper drive enable
    }

    //++++++++++++++++++++++++++++++ hour stepper driver ++++++++++++++++++++++++++++++++++
    // This next section is the hour timer and steps each hour on the hour, must run seperate to minutes timer program above if possible?

    { if (sensorState == HIGH); {  // opto hour sensor
        digitalWrite(ledPin, HIGH);
        digitalWrite(enPinb, LOW);  // enable driver module (A4988)
        { digitalWrite(dirPinb, HIGH);     // motor direction

          for (int x = 0; x < 4; x++) {    // number of steps
            digitalWrite(stepPinb, HIGH);  // start stepping hours motor
            delayMicroseconds(7000);
            digitalWrite(stepPinb, LOW);
            delayMicroseconds(7000);
            delay(500);
            digitalWrite(ledPin, LOW);
            //digitalWrite(enPinb, HIGH);  // disable drive motor
          }
        }
      }
    }
  }
}

I've amended my code, apologise for error and missing statement

@gcjr thanks for your reply, will take a look.

@cattledog, sorry, have amended the code.

If you are refering to your post #15 you have not ammended correctly.

  1. There is still no reading of pin 9 to determine a sensorState.

  2. You have terminated the sensorState if( ) conditional statement with a ' ; ' which removes places any subsequent lines out of the condition.

//if (sensorState == HIGH);  //remove the terminating ';'
if (sensorState == HIGH)
  1. You have extra curly braces in several places.

If you use autoformat (Ctrl +T) on your code it will help see these things.

Thanks cattledog for your help, I'll go through it again, and come back