delay with millis doesnt work after a certain number

Hi everyone, having a hard time with this code.

I'm trying to use the delay with millis approach for delays and everything is working fine....

except it only works with numbers under 30000.

At first, I thought it was the fact that I was just using a normal int instead of an unsigned int. But I've tried everything and it just isnt working... up to 30000 its a okay, everything works perfect. I just dont understand please help

/*
  
//Motor Pin Setup
const int motor1Pin = 8; // the pin that the LED is attached to
const int motor2Pin = 9; // the pin that the LED is attached to
const int motor3Pin = 10; // the pin that the LED is attached to

unsigned long shotTime = 0;        // will store last time LED was updated

bool isActive = false;
bool setDelay = false;
bool canSetRelay = true;

String delaySet = "0";

// constants won't change:
const long interval = 25000;  

//Constants
int shotTimer = 5000;


unsigned int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(motor3Pin, OUTPUT);
  Serial.println("hello");
}

void loop() {
  unsigned long currentMillis = millis();
  
  
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:

                if(isDigit(incomingByte) && setDelay == true){
   int bytetoInt = incomingByte - 48 ;
    delaySet = delaySet + bytetoInt;

    Serial.println(delaySet);

    }

        if (incomingByte == 'D') {
      if(setDelay == true){
        Serial.println("Delay Set");
        shotTimer = delaySet.toInt();
       setDelay = false;
      canSetRelay = true;
      delaySet = "";
      }else{
      Serial.println("Set Delay");
      setDelay = true;
      canSetRelay = false;
      
      Serial.println(delaySet);
    }
    


        }


    
    

    if(canSetRelay == true){
    if (incomingByte == '1') {
      digitalWrite(motor1Pin, HIGH);
      //delay(shotTimer);
    }

    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(motor1Pin, LOW);
      digitalWrite(motor2Pin, LOW);
      digitalWrite(motor3Pin,LOW);
    }
        if (incomingByte == '2') {
          digitalWrite(motor2Pin,HIGH);
          isActive = true;
          shotTime = currentMillis;
          //Serial.println("Shot Time:");
        //  Serial.println(shotTime);
          
          

    }
            if (incomingByte == '3') {
      digitalWrite(motor3Pin, HIGH);
      //delay(shotTimer);
    }


    }

  }

        if(isActive == true){

  //Serial.println("ACTIVE");
 // Serial.println(shotTime);
  Serial.println(currentMillis - shotTime);
    
  if (currentMillis - shotTime >= shotTimer) {
    // save the last time you blinked the LED
   
  Serial.println("Deactive");
  
  isActive = false;

  digitalWrite(motor2Pin,LOW);
 // shotTime = 0;
  
  }
    }
}

Use CTRL T to format your code.
Weird white space in the code makes things difficult to read.

Sounds like an ‘integer’ vs ‘unsigned long’ problem.

String delaySet = "0";
I never ever use ‘String’

@TheGreyfox - I assume that you modified the code in your original post without mentioning that you had done so

"String" doesn't make a good 'unsigned long'. It looks like .toInt() returns an 'int' which is limited to 32767.

Try using an unsigned long:

//Motor Pin Setup
const int motor1Pin = 8; // the pin that the LED is attached to
const int motor2Pin = 9; // the pin that the LED is attached to
const int motor3Pin = 10; // the pin that the LED is attached to


unsigned long shotTime = 0;        // will store last time LED was updated


bool isActive = false;
bool setDelay = false;
bool canSetRelay = true;


unsigned long delaySet;  // Use 'unsigned long' for delay times
unsigned long shotTimer = 5000;


// constants won't change:
const unsigned long interval = 25000;

unsigned int incomingByte;      // a variable to read incoming serial data into


void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  pinMode(motor3Pin, OUTPUT);
  Serial.println("hello");
}


void loop() {
  unsigned long currentMillis = millis();


  // see if there's incoming serial data:
  if (Serial.available() > 0)
  {
    // read the oldest byte in the serial buffer:
    int incomingByte = Serial.read();


    // if it's a capital H (ASCII 72), turn on the LED:


    if (isDigit(incomingByte) && setDelay == true)
    {
      int bytetoInt = incomingByte - '0' ;
      delaySet = (delaySet * 10) + bytetoInt;


      Serial.println(delaySet);
    }


    if (incomingByte == 'D') {
      if (setDelay == true) {
        Serial.print("Delay Set ");
        Serial.println(delaySet);
        shotTimer = delaySet;
        setDelay = false;
        canSetRelay = true;
        delaySet = 0;
      } else {
        Serial.println("Set Delay");
        delaySet = 0;
        setDelay = true;
        canSetRelay = false;
      }


    }


    if (canSetRelay == true) {
      if (incomingByte == '1') {
        digitalWrite(motor1Pin, HIGH);
        //delay(shotTimer);
      }


      // if it's an L (ASCII 76) turn off the LED:
      if (incomingByte == 'L') {
        digitalWrite(motor1Pin, LOW);
        digitalWrite(motor2Pin, LOW);
        digitalWrite(motor3Pin, LOW);
      }
      
      if (incomingByte == '2') {
        digitalWrite(motor2Pin, HIGH);
        isActive = true;
        shotTime = currentMillis;
        //Serial.println("Shot Time:");
        //  Serial.println(shotTime);
      }
      
      if (incomingByte == '3') {
        digitalWrite(motor3Pin, HIGH);
        //delay(shotTimer);
      }




    }


  }


  if (isActive == true) {


    //Serial.println("ACTIVE");
    // Serial.println(shotTime);
    Serial.println(currentMillis - shotTime);


    if (currentMillis - shotTime >= shotTimer) {
      // save the last time you blinked the LED


      Serial.println("Deactive");


      isActive = false;


      digitalWrite(motor2Pin, LOW);
      // shotTime = 0;


    }
  }
}

larryd:
Use CTRL T to format your code.
Weird white space in the code makes things difficult to read.

Sounds like an ‘integer’ vs ‘unsigned long’ problem.

String delaySet = "0";
I never ever use ‘String’

@larryd
Sorry! I will from now on. TIL Ctrl-T is the greatest short cut ever created. Just wow.

I will give that shot when I'm back at my arduino.

The string was just for testing purpose, I wasn't sure how to add a value to the end of an Integer so I just added it to a string. Hopefully all the delay values will be hard coded.

Is there a better way to convert a String to long?

UKHeliBob:
@TheGreyfox - I assume that you modified the code in your original post without mentioning that you had done so

@UKHeliBob
The top part of it is the default comments and credits that are found at the top of PhysicalPixel.ino. I removed them for readability.

"Is there a better way to convert a String to long?"

I've seen the below in code before, but haven't used it.

String myData = "12345678";
long myNum = atol(myData);
String myData = "12345678";
long myNum = atol(myData);  //only works with strings (lowercase s)
cannot convert 'String' to 'const char*' for argument '1' to 'long int atol(const char*)'

Either don't use Strings and use strings instead or if you feel that you must use Strings

  String myData = "12345678";
  long myNum = myData.toInt();

this will give some more stuff to play with

http://www.cplusplus.com/reference/cctype/