Not sure if the language I am using is correct

Hi,

I have having issues where the Arduino starts my program before waiting for the startCycle signal.

I am pretty new to writing like this, all my knowledge comes from writing PLC programs but it doesn't seem to function in the same way.

It also doesn't follow the cycle as intended even when it starts by itself.

Thank you in advance for all your help.

This is the intended cycle.

Step 0 -
startCycle input LOW
cycleStep = 0
Step 1 -
startCycle input HIGH
cycleStep = 1
turn cycleLed ON
Step 2 -
--------->it seems to start here and then just keeps the motor spinning without changing direction.
check if cycleStep = 1
motorDirection High
motorPulse High
increase pulseCount by 1
pause
motorPulse Low
check if pulseCount is equal to 200
if equal make cycleStep = 2
make pulseCount = 0

Step 3 -
check if cycleStep = 2
motorDirection Low
motorPulse High
increase pulseCount by 1
pause
motorPulse Low
check if pulseCount is equal to 200
if equal make cycleStep = 3
make pulseCount = 0

Step 4 -
check if cycleStep = 3
turn cycleComplete LED on and off
turn cycleLed OFF
make cycleStep = 0

------ CODE START -----

//INPUTS
const int startCycle = 1;

//OUTPUTS
const int motorPulse = 6;
const int motorDirection = 7;
const int cycleLed = 8;
const int cycleComplete = 9;

//Counters
int pulseCount = 0;

//Steps
int cycleStep = 0;

void setup() {
Serial.begin(9600);
pinMode(startCycle, INPUT);
digitalWrite(startCycle, LOW);
pinMode(motorPulse, OUTPUT);
pinMode(motorDirection, OUTPUT);
pinMode(cycleLed, OUTPUT);
pinMode(cycleComplete, OUTPUT);
}

void loop() {
if (startCycle == HIGH){
digitalWrite(cycleLed, HIGH);
cycleStep = 1;
}

if (cycleStep == 1){
digitalWrite(motorDirection, HIGH);
digitalWrite(motorPulse, HIGH);
pulseCount += 1;
delay(30);
digitalWrite(motorPulse, LOW);
if(pulseCount == 200){
cycleStep = 2;
pulseCount = 0;
}
}
if (cycleStep == 2){
digitalWrite(motorDirection, LOW);
digitalWrite(motorPulse, HIGH);
pulseCount += 1;
delay(30);
digitalWrite(motorPulse, LOW);
if(pulseCount == 200){
cycleStep = 3;
pulseCount = 0;
}
}
if (cycleStep == 3){
digitalWrite(cycleComplete, HIGH);
delay(1000);
digitalWrite(cycleComplete, LOW);
delay(1000);
digitalWrite(cycleComplete, HIGH);
delay(1000);
digitalWrite(cycleComplete, LOW);
delay(1000);
digitalWrite(cycleComplete, HIGH);
delay(1000);
digitalWrite(cycleComplete, LOW);
digitalWrite(cycleLed, LOW);
cycleStep = 0;
}
}

------ CODE STOP -----

Welcome. Please always use code tags not CODE START CODE STOP lines to post your code. Please read the forum instructions on how to do that. I did it for you, and also used ctrl-T to auto format it so we can see the code blocks better:

//INPUTS
const int startCycle = 1;

//OUTPUTS
const int motorPulse = 6;
const int motorDirection = 7;
const int cycleLed = 8;
const int cycleComplete = 9;

//Counters
int pulseCount = 0;

//Steps
int cycleStep = 0;

void setup() {
  Serial.begin(9600);
  pinMode(startCycle, INPUT);
  digitalWrite(startCycle, LOW);
  pinMode(motorPulse, OUTPUT);
  pinMode(motorDirection, OUTPUT);
  pinMode(cycleLed, OUTPUT);
  pinMode(cycleComplete, OUTPUT);
}

void loop() {
  if (startCycle == HIGH) {
    digitalWrite(cycleLed, HIGH);
    cycleStep = 1;
  }

  if (cycleStep == 1) {
    digitalWrite(motorDirection, HIGH);
    digitalWrite(motorPulse, HIGH);
    pulseCount += 1;
    delay(30);
    digitalWrite(motorPulse, LOW);
    if (pulseCount == 200) {
      cycleStep = 2;
      pulseCount = 0;
    }
  }
  if (cycleStep == 2) {
    digitalWrite(motorDirection, LOW);
    digitalWrite(motorPulse, HIGH);
    pulseCount += 1;
    delay(30);
    digitalWrite(motorPulse, LOW);
    if (pulseCount == 200) {
      cycleStep = 3;
      pulseCount = 0;
    }
  }
  if (cycleStep == 3) {
    digitalWrite(cycleComplete, HIGH);
    delay(1000);
    digitalWrite(cycleComplete, LOW);
    delay(1000);
    digitalWrite(cycleComplete, HIGH);
    delay(1000);
    digitalWrite(cycleComplete, LOW);
    delay(1000);
    digitalWrite(cycleComplete, HIGH);
    delay(1000);
    digitalWrite(cycleComplete, LOW);
    digitalWrite(cycleLed, LOW);
    cycleStep = 0;
  }
}

Also when you read the forum instructions, you will see that it asks you to give details of your hardware. Have you written test sketches to test the hardware independently from this program logic? It's a good idea.

We don't even know what Arduino you have, on the Uno and a few others, using pin 1 is a bad idea because it's tied to serial.

You have defined startCycle as an int variable, with a constant value of 1. HIGH is also defined (in the Arduino core code) to have a constant value of 1. So what do you expect the above line to do?

This is a demonstration of the consequences of ambiguous naming. I think 'startCycle' is supposed to be a pin number. That is likely why it was declared as 'const' after the heading '//INPUTS'. It would be clearer if it was named 'startCyclePin', then the error of not using it with digitalRead() would be more obvious.

To some people, at least...

Thank very much for your feedback, I have managed to get it sorted on the discord.

Many thanks,

It would be nice if you would post the corrected sketch here, as well as Discord. Future readers of this thread might like to know.

1 Like
//INPUTS
const int startCycle = 2;

//OUTPUTS
const int motorPulse = 6;
const int motorDirection = 7;
const int cycleLed = 8;
const int cycleComplete = 9;

//Counters
int pulseCount = 0;

//Steps
int cycleStep = 0;

void setup() {
  Serial.begin(9600);
  pinMode(startCycle, INPUT_PULLUP);
//Change the line above to use the internal PULLUP resister and then used an external ground to drive it down.//
  pinMode(motorPulse, OUTPUT);
  pinMode(motorDirection, OUTPUT);
  pinMode(cycleLed, OUTPUT);
  pinMode(cycleComplete, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (digitalRead(startCycle) == LOW){
//didn't include "digitalRead" in line above and therefore program was waiting without reading//
    digitalWrite(LED_BUILTIN, HIGH);
    cycleStep = 1;
  }
  
  if (cycleStep == 1){
    digitalWrite(motorDirection, HIGH);
    digitalWrite(motorPulse, HIGH);
    pulseCount += 1;
    delay(30);
    digitalWrite(motorPulse, LOW);
    if(pulseCount == 200){
      cycleStep = 2;
      pulseCount = 0;
    }
  }
  if (cycleStep == 2){
    digitalWrite(motorDirection, LOW);
    digitalWrite(motorPulse, HIGH);
    pulseCount += 1;
    delay(30);
    digitalWrite(motorPulse, LOW);
    if(pulseCount == 200){
      cycleStep = 3;
      pulseCount = 0;
    }
  }
  if (cycleStep == 3){
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    cycleStep = 0;
  }
}

I hope that is correct, sorry if it is wrong.

Thanks so much for supplying that. Enjoy.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.