Using Controllino PLC Library. Delay function not working

#include <SPI.h>
#include <Controllino.h>

const int t7 = CONTROLLINO_D1; //set t7 (DISABLE IN) to D1 pin of Controllino (this can be changed if wiring on PLC is different); 5V = start spinning 0V= stop spinning
const int t8 = CONTROLLINO_D4; //set t8 (DIR IN) to D4 pin of Controllino; 5V = CW movement
const int t9 = CONTROLLINO_D2; //set t9 (STEP IN) to D2 pin of Controllino; 5V = CCW movement

char in; //input to decide what the motor should do

/*

  • INPUT GUIDE
  • a = CW motion
  • b = CCW motion
  • c = stop
  • SPEED IS SET ON THE DRIVE
  • REFER TO P5000 MANUAL FOR INFORMATION
    */

void setup() {
// put your setup code here, to run once:
pinMode(t7, OUTPUT); //set pin as an output
pinMode(t8, OUTPUT); //set pin as an output
pinMode(t9, OUTPUT); //set pin as an output
Serial.begin(9600); //start serial communication
Serial.println("We are in VCO mode \n Guide to input \n a = CW motion \n b = CCW motion \n c = Stop"); //basic instruction prior to code start printed onto Serial Monitor
}

void loop() {
// put your main code here, to run repeatedly:
in = Serial.read(); //take input through Serial Monitor

if(isAlpha(in)) //check if there is an input and input is a char
{
Serial.println(in); //feedback on what you entered
Serial.println("START OF CODE"); //feedback on this is start of code
}

if(in == 'a') //check if input is a
{
Serial.println("running CW..."); //feedback; shown on Serial Monitor
digitalWrite(t9, LOW); //Set pin to ~0V; reason for setting this is to be double sure the pin is not set to HIGH
delay(1000);
digitalWrite(t7, HIGH); //Set pin to ~5V
delay(1000);
digitalWrite(t8, HIGH); //Set pin to ~5V
delay(1000);

}

if(in == 'b') //check if input is b
{
Serial.println("running CCW..."); //feedback; shown on Serial Monitor
digitalWrite(t8, LOW); //Set pin to ~0V
digitalWrite(t7, HIGH); //Set pin to ~5V
digitalWrite(t9, HIGH); //Set pin to ~5V
}

if(in == 'c') //check if input is c
{
Serial.println("stopping...");
digitalWrite(t7, LOW);
digitalWrite(t8, LOW);
digitalWrite(t9, LOW);
}

}

Once I type 'a' the motor rotates in counter clockwise, but I do not want them to run forever. I can stop the motor when I type 'c'. However, using the delay function, I do want to make them run only for few seconds. When I put delay functions in the above, it does not give me any error nor any changes in the motor.
Can someone please help me?
Thank you

You need to learn how to not use delay().
You probably want the read the Serial input basics topic too.

Take a look at blink without delay example in the IDE

Please remember to use code tags when posting code

1 Like

Please format your code with the "</>" button in the edit box.

I'd suggest setting a flag and timer when you start the motor, and then if the timer has gone on too long, turn the motor off.

1 Like

Hi Max7637,

welcome to the Arduino-Forum.
If you want to proceed faster in future postings there are a fe things to learn about how to use the forum

This is an too unprecise description of the functionality that you want.
You should describe it similar like this
the controllino receives a "a"
then IO-pin t9 shall switch to _____ after _____ seconds / or immidiately
then IO-pin t7 shall switch to _____ after _____ seconds / immidiately
then IO-pin t8 shall switch to _____ after _____ seconds / immidiately

after that
after ____ seconds in which state do you wish to have the IO-pins t7, t8, t9 ???

same for receiving "b"

And as a further advice you should give anoverview about your project. As soon as you give an overview very often much better suggestions how to realise it can be made

best regards Stefan

1 Like

Frequently the process of writing up an overview or explanation of a project for other people helps your own understanding and can inspire new solutions.

1 Like

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