delay and millis() is way way to fast

hey guys,

I am new here this is my first post, and I am technically new to arduino. I got one a couple months back and have been tinkering around with different adafruit stuff. transistors, lcd's, oled's, relays, that sort of thing. however I have hit my first wall. My sketch is relatively small, 1630 bytes. and I believe there is supposed to be 27k. I have tried on multiple boards, all authentic, 2 different nano's and 2 different uno's. all with the same result which makes me think its my code, duh. but why, and how is my issue because if I pull the code and run everything individually everything runs fine. delay() is correct enough. but when I put it all together, delay goes by way way way to quick. so 420000 goes by in 4 seconds. if I add 4 zeros, it adds another 4 seconds. this is a guesstimate as I didn't have a stop watch but definitely not 7 minutes, or even 1. I tried blink without delay and that didn't work. I know that if I find the issue in this code I will find it in the blink without delay code as well as the problem is identical. I just cannot get delay or blink without delay to work as millis() is going by way to quick but only with a bigger code, something with 10 lines, everything is fine. I also reinstalled the arduino IDE thinking that a library was corrupt or something. I say millis() is to fast because blink without delay has the same issue.

Issue: entire code runs delay way way to quick. but bits of the code copied and pasted are treated correctly.

int soilPin = A1, soilPin2 = A2;
int soilPower = 10, soilPower2 = 11;
int pumpPin = 2, pumpPin2 = 3;
int ledGR = 12, ledRED = 13;
int val = 0, val2 = 0;
int pumpStatus = LOW;
int pumpStatus2 = LOW;
int pumpTime = 0;
int pumpTime2 = 0;

void setup() {
  pinMode (pumpPin, OUTPUT);
  pinMode (pumpPin2, OUTPUT);
  pinMode (soilPin, INPUT);
  pinMode (soilPin2, INPUT);
  pinMode (soilPower, OUTPUT);
  pinMode (soilPower2, OUTPUT);
  pinMode (ledRED, OUTPUT);
  pinMode (ledGR, OUTPUT);
  delay(1000);
}

void loop() {
  //check moisture
  checkSoil(soilPower, soilPin, ledGR, val);
  delay(50);
  percent(val);
  checkSoil(soilPower2, soilPin2, ledRED, val2);
  delay(50);
  percent(val2);
  //run pump if moisture is under a certain threshold
  if (val <= 60) {
    pumpCycle(pumpPin, pumpStatus, pumpTime, ledGR);
  }
  if (val2 <= 60) {
    pumpCycle(pumpPin2, pumpStatus2, pumpTime2, ledRED);
  }
}


unsigned long checkSoil(int sPower, int APin, int led, int moist) {
  //check moisture
  digitalWrite (sPower, HIGH);
  digitalWrite (led, HIGH);
  delay(100);
  moist = analogRead(APin);
  delay(100);
  digitalWrite (sPower, LOW);
  digitalWrite (led, LOW);
  return moist;
}

unsigned long percent (int value) {
  //change analog reading to percentage number
  value = map(value, 260, 1025, 0, 100);
  value = map(value,  0, 100, 100, 0);
  return value;
}

void pumpCycle(int pumpP, int pStatus, int pumpT, int led) {
  //run pumps in intervals to fight heat
  for (int x = 0; x < 4; x++) {
    if (pStatus = LOW) {
      pStatus = HIGH;
      pumpT = 420000;
    }
    else {
      pStatus = LOW;
      pumpT = 210000;
    }
    digitalWrite (pumpP, pStatus);
    digitalWrite (led, pStatus);
    delay(pumpT);
  }
}

please forgive me if I forgot some information. I want this on a 328p and I am using the arduino IDE 1.8.5

Thank you for even reading!
edit = I am positive this is a space issue due to the irrational behavior. but I am unsure of a solution.

int is a signed 16 bit datatype. The maximum possible decimal value that can be stored in such a datatype is...

https://www.google.com/search?q=(2^15)-1

What is the largest decimal value you are trying to store?

Your checkSoil function is not working as you think it does. Apparently you think you're passing the val and val2 variables to the function, which modifies the value. Instead what happens is that you are just passing a copy of the values of those variables to checkSoil(), which assigns the values to a local variable. The value of that local variable is returned but you don't do anything with the return value. It is possible to make this function work as you designed it by adding a reference operator to the moist parameter but for this application it's much more sensible to just set val or val2 according to the return value:

val = checkSoil(soilPower, soilPin, ledGR);

Since val and val2 were always 0 no matter what, pumpCycle() was never ran.

0 is still under 60. so it should have ran everytime. however I was having an issue with the percentages as they were spotty, sometimes would show sometimes wouldn't. I have an LCD screen and have seen the percentages. but I am positive this was an issue sometimes.

However, I figured out my issue because of your statement. because percent was not working as you described the issue but with another piece of code. The percentage was always 1025, which is 0 if percent would work. so I fixed the percent issue.

however my code was running the pumps before but only for 4 seconds, they would not turn on if this was the issue. The issue comes with the if else statement in the pumpcycle in only what I can believe is the same issue you described but with the "pStatus" variable

for (int x = 0; x < 4; x++) {
    if (pStatus = LOW) {
      pStatus = HIGH;
      pumpT = 420000;
    }
    else {
      pStatus = LOW;
      pumpT = 210000;
    }
    digitalWrite (pumpP, pStatus);
    digitalWrite (led, pStatus);
    delay(pumpT);
  }

you see the pStatus was never writing HIGH or LOW to the pumpPin. If I remove this piece and directly put it in the loop with the correct variables everything works as should.

I figured it out because after I fixed my 1025 issue the board never looped again. so it was doing the if else statement. but the variable was not actually writing any HIGH or LOW in the digitalWrite.

any thoughts as you were one of the only ones that took time to read the code. (apologies if you actually did and are and just could not help.)

even so, thank you very very much for pointing me in some direction here.

 if (pStatus = LOW) {

Oops

pumpT = 420000;Let's hope pumpT is at least a long