Code only works when I use Serial.print(...)

As the title says, the code is supposed to turn some ERMs on and off when it gets messages from the client. That only works when I print "ch1 off" ( and the other ones). If I don't print these strings the ERMs do not turn off.
I guess the prints work as a delay that makes sure the code works, but I don't understand why.

Does anybody know how to fix this without using an extra delay or these prints?

#include <WiFi.h>


#define ch1 18       // INDEX
#define ch2 16      // THUMB
#define ch3 5      // PALM
#define ch4 17       // MIDDLE


const char *ssid = "SenseGlove_wifi";
const char *password = "1248163264";
const int serverPort = 1234;


int onTimeCh1;
int onTimeCh2;
int onTimeCh3;
int onTimeCh4;

const int longTime = 500;   // ms
const int middleTime = 100; // ms
const int shortTime = 50;


double oldTimeCh1 = 0;
double oldTimeCh2 = 0;
double oldTimeCh3 = 0;
double oldTimeCh4 = 0;

bool partyMode = false;

WiFiServer server(serverPort);
WiFiClient client;

void quickBuzz(){
    analogWrite(ch1, 255);
    delay(shortTime);
    analogWrite(ch1, 0);
  }

void setup()
{
   onTimeCh1 = longTime;
   onTimeCh2 = longTime;
   onTimeCh3 = longTime;
   onTimeCh4 = longTime;

  pinMode(ch1, OUTPUT);
  pinMode(ch2, OUTPUT);
  pinMode(ch3, OUTPUT);
  pinMode(ch4, OUTPUT);
 
  analogWrite(ch1, 0);
  analogWrite(ch2, 0);
  analogWrite(ch3, 0);
  analogWrite(ch4, 0);

  Serial.begin(115200);
  client.stop();
  WiFi.disconnect();
  

  WiFi.begin(ssid, password);
  
  

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi");
  quickBuzz();
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
}

void loop()
{
  // Check for a new client
  client = server.available();
  if (client)
  {
    quickBuzz();
    quickBuzz();
    Serial.println("New client connected");

    // Read data from the client
    while (client.connected())
    {
      double millisTijd = millis();
      if((millisTijd - oldTimeCh1) >= onTimeCh1){
        analogWrite(ch1, 0);
        // Serial.print("ch1 off");
      }
      if((millisTijd - oldTimeCh2) >= onTimeCh2){
        analogWrite(ch2, 0);
        // Serial.print("ch1 off");
      }
      if((millisTijd - oldTimeCh3) >= onTimeCh3){
        analogWrite(ch3, 0);
        // Serial.print("ch1 off");
      }
      if((millisTijd - oldTimeCh4) >= onTimeCh4){
        analogWrite(ch4, 0);
        // Serial.print("ch1 off");
      }
      
      
      if (client.available())
      {
        String data = client.readStringUntil('^');
        
        if(data == "0"){
          analogWrite(ch1, 255);
          oldTimeCh1 = millisTijd;
          onTimeCh1 = longTime;
        }
        else if(data == "1"){
          analogWrite(ch2, 255);
          oldTimeCh2 = millisTijd;
          onTimeCh2 = longTime;
        }
        else if(data == "2"){
          analogWrite(ch3, 255);
          oldTimeCh3 = millisTijd;
          onTimeCh3 = longTime;
        }
        else if(data == "3"){
          analogWrite(ch4, 255);
          oldTimeCh4 = millisTijd;
          onTimeCh4 = longTime;
        }
        else if(data == "4"){     // sliderVR
          analogWrite(ch1, 255);
          analogWrite(ch2, 255);
          oldTimeCh1 = millisTijd;
          oldTimeCh2 = millisTijd;
          onTimeCh1 = shortTime;
          onTimeCh2 = shortTime;
        }
        else if(data == "5"){
          analogWrite(ch1, 255);
          analogWrite(ch2, 255);
          analogWrite(ch3, 255);
          analogWrite(ch4, 255);
          oldTimeCh1 = millisTijd;
          oldTimeCh2 = millisTijd;
          oldTimeCh3 = millisTijd;
          oldTimeCh4 = millisTijd;
        }
        else if(data == "6"){ //keyboard presses thumb
          analogWrite(ch1, 255);
          oldTimeCh1 = millisTijd;
          onTimeCh1 = middleTime;
        }
        else if(data == "7"){ //keyboard presses index
          analogWrite(ch2, 255);
          oldTimeCh2 = millisTijd;
          onTimeCh2 = middleTime;
        }
        
        else if(data == "8"){ // pencil hold
          analogWrite(ch1, 60);
          analogWrite(ch2, 60);
          oldTimeCh1 = millisTijd;
          oldTimeCh2 = millisTijd;
          onTimeCh1 = middleTime;
          onTimeCh2 = middleTime;
        }
        else if(data == "9"){ //pencil draw
          analogWrite(ch1, 120);
          analogWrite(ch2, 120);
          oldTimeCh1 = millisTijd;
          oldTimeCh2 = millisTijd;
          onTimeCh1 = middleTime;
          onTimeCh2 = middleTime;
        }

        Serial.println("Received data: " + data);
        // client.print("Data received: " + data); // Respond to the client if needed
      }
      

      
    }
  }
}
  • why do you check if client.available() within the while (client.connected) loop? sorry, thought was server.available()

  • why delay -- if((millisTijd - oldTimeCh1) >= onTimeCh1) without resetting oldTimeCh1 which repeated performs the analogWrite() once the time is exceeded?

  • should there be something that disables the timer/operation once performed -- setting oldTimeCh1 to zero and making the timer contingent on it -- if(oldTimeCh1 && (millisTijd - oldTimeCh1) >= onTimeCh1), setting it to zero when the operation executes

It looks like implementing these changes fixed it, thank you!
~edit -> did not work

It did not work:/

what exactly doesn't work?

what are ERMs?

I am sorry it works now. I implemented what you said, but did it in the wrong way with some wrong parentheses.
ERM are the Eccentric rotating mass vibration motors that I am using. Thank you so much for the help again!

another thing is the millis() values are unsigned long, not double

your onTimeChN and oldTimeChN variables should also be unsigned long

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