Cumulative Timer triggered by chat won't write pin low

I have an ESP8266 that is connected to my Twitch chat. It uses to digital outputs to trigger functions on a Leonardo. I wanted to set up a cumulative timer, so that when a specific user types a specific word in Twitch chat, the timer increments 10 seconds each time and then outputs LOW at the end of the timer. Everything is function, except that the cumulative timer only switches to low once a new command is sent via Twitch chat. I have a feeling this is some sort of syntax error but I'm not sure.

*Edit: I should add this is a kind of joint project and I'm not as good with code as my other team member, so I apologize for a slow uptake on suggestions.

  /*******************************************************************
    Connect to Twtich Chat with a Bot
   Created with code from TheOtherLoneStar (https://www.twitch.tv/theotherlonestar)
   Hackaday IO: https://hackaday.io/otherlonestar
   By Brian Lough (https://www.twitch.tv/brianlough)
   YouTube: https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA
Created with code from noycebru www.twitch.tv/noycebru
 *******************************************************************/
 
#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <IRCClient.h>


//define your default values here, if there are different values in config.json, they are overwritten.
#define secret_ssid "my ssid" 
#define IRC_SERVER   "irc.chat.twitch.tv"
#define IRC_PORT     6667
#define ACC_TIME_ADDITION     10000UL
#define CLUTCH_TIME_ADDITION  10000UL

//------- Replace the following! ------
char ssid[] = "null";       // your network SSID (name)
char password[] = "null";  // your network key
 
//The name of the channel that you want the bot to join
const String twitchChannelName = "null"; //this is case sensitive!
 
//The name that you want the bot to have
#define TWITCH_BOT_NAME "null"
 
//OAuth Key for your twitch bot
// https://twitchapps.com/tmi/
#define TWITCH_OAUTH_TOKEN "null"
 
 
//------------------------------ 

bool acc_twitch_input = false;                //Boolean variable to hold twitch input value for accelerator
int acc_sig_out = 13;                         //This pin will be used to send the accelerator switch signal to the arduino leonardo. This pin should connect to Arduino Leonardo pin 7

bool clutch_twitch_input = false;             //Boolean variable to hold twitch input value for clutch
int clutch_sig_out = 14;                      //This pin will be used to sent the cluch switch signal to the arduino leonardo. This pin should connect to Arduino Leonardo pin 8

long clutch_total_on_time = 0;                //Variable to hold the accumulated ON time for the clutch
long clutch_interval_remainder = 0;           //Variable to hold the remaining time of the current interval
unsigned long clutch_interval_on_time = 0;    //Variable to hold the interval on time
unsigned long clutch_interval_start = 0;      //Variable to hold the interval start time

long acc_total_on_time = 0;                   //Variable to hold the accumulated ON time for the accelerator
long acc_interval_remainder = 0;              //Variable to hold the remaining time of the current interval
unsigned long acc_interval_on_time = 0;       //Variable to hold the interval on time
unsigned long acc_interval_start = 0;         //Variable to hold the interval start time

//int loop_count = 0;
String ircChannel = "";
 
WiFiClient wiFiClient;
IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);
 
void setup() {
  
  pinMode(acc_sig_out, OUTPUT);
  pinMode(clutch_sig_out, OUTPUT);

  delay(2000);
  Serial.begin(115200);
  Serial.println();
 
  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);
 
  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);
 
  ircChannel = "#" + twitchChannelName;
 
  client.setCallback(callback);
  

  digitalWrite(acc_sig_out,LOW);                  //Write the acceleration trigger output pin low on startup
  digitalWrite(clutch_sig_out,LOW);               //Write the clutch trigger output pin low on startup
}
 
void loop() {
 
  // Try to connect to chat. If it loses connection try again
  if (!client.connected()) {
    Serial.println("Attempting to connect to " + ircChannel );
    // Attempt to connect
    // Second param is not needed by Twtich
    if (client.connect(TWITCH_BOT_NAME, "", TWITCH_OAUTH_TOKEN)) {
      client.sendRaw("JOIN " + ircChannel);
      Serial.println("connected and ready to rock");
      sendTwitchMessage("We're here to test!");
    } else {
      Serial.println("failed... try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
    return;
  }
  client.loop();
}
 
void sendTwitchMessage(String message) {
  client.sendMessage(ircChannel, message);
}
 
 
void callback(IRCMessage ircMessage) {
  //Serial.println("In CallBack");
 
  if (ircMessage.command == "PRIVMSG" && ircMessage.text[0] != '\001') {
    //Serial.println("Passed private message.");
   
    ircMessage.nick.toUpperCase();
 
    String message("<" + ircMessage.nick + "> " + ircMessage.text);
 
    //prints chat to serial
    Serial.println(message);

//this is where you would replace these elements to match your streaming configureation. 

if(ircMessage.text.indexOf("test1") > -1 && ircMessage.nick == "user")         //Put the IF statement input condition for acc switch from Twitch here
        {
          acc_interval_start = millis();                                              //Mark the start time of the current interval
          acc_total_on_time += ACC_TIME_ADDITION;                                     //Add the prescribed time addition to the total time each time the input switch is triggered
        }
        
        acc_interval_on_time = millis() - acc_interval_start;                          //Calculate the current interval by subtracting "now" time from the interval start time
        acc_interval_remainder = ACC_TIME_ADDITION - acc_interval_on_time;             //Calculate the interval remainder from the difference between the prescribed time addition and the current interval time

        
        if(acc_interval_remainder < 0)                                                 //Keep the interval remainder from rolling negative
        {
          acc_interval_remainder = 0;
        }
        
        if(acc_total_on_time <= ACC_TIME_ADDITION)
        {
          acc_total_on_time = acc_interval_remainder;                                   //If the total time is less than the current interval remainder, the total remaining time is the current interval remainder     
        }
        else
        {
          acc_total_on_time = acc_total_on_time - acc_interval_on_time;                 //Get the total on time from the difference between total and current interval
        }
              
        if(acc_total_on_time < 0)                                                       //Keep total time from rolling negative
        {
          acc_total_on_time = 0;
        }       
         
        String acc_print_string = "Acc interval time: " + (String)acc_interval_on_time;
        acc_print_string += " Acc interval remaining: " + (String)acc_interval_remainder;
        acc_print_string += " Acc total time: " + (String)acc_total_on_time;
        Serial.println(acc_print_string);
        
        if(acc_total_on_time > 0)
        {
          digitalWrite(acc_sig_out,HIGH);                                               //Write the acceleration trigger output pin high- put these writes where it is appropriate for you Twitch Program
        }
        else
        {
          digitalWrite(acc_sig_out,LOW);                                                //Write the acceleration trigger output pin low-  put these writes where it is appropriate for you Twitch Program
        }

     
if(ircMessage.text.indexOf("test2") > -1 && ircMessage.nick == "user")             //Put the IF statement input condition for clutch switch from Twitch here

        {
          clutch_interval_start = millis();                                              //Mark the start time of the current interval
          clutch_total_on_time += CLUTCH_TIME_ADDITION;                                  //Add the prescribed time addition to the total time each time the input switch is triggered
        }
        
        clutch_interval_on_time = millis() - clutch_interval_start;                       //Calculate the current interval by subtracting "now" time from the interval start time
        clutch_interval_remainder = CLUTCH_TIME_ADDITION - clutch_interval_on_time;       //Calculate the interval remainder from the difference between the prescribed time addition and the current interval time
        
        if(clutch_interval_remainder < 0)                                                 //Keep the interval remainder from rolling negative
        {
          clutch_interval_remainder = 0;
        }
        
        if(clutch_total_on_time <= CLUTCH_TIME_ADDITION)
        {
          clutch_total_on_time = clutch_interval_remainder;                                //If the total time is less than the current interval remainder, the total remaining time is the current interval remainder     
        }
        else
        {
          clutch_total_on_time = clutch_total_on_time - clutch_interval_on_time;           //Get the total on time from the difference between total and current interval
        }
              
        if(clutch_total_on_time < 0)                                                       //Keep total time from rolling negative
        {
          clutch_total_on_time = 0;
        }       
         
        String clutch_print_string = "Clutch interval ON time: " + (String)clutch_interval_on_time;
        clutch_print_string += " Clutch interval remaining: " + (String)clutch_interval_remainder;
        clutch_print_string += " Clutch total time: " + (String)clutch_total_on_time;
        Serial.println(clutch_print_string);                                             //Print string for clutch values
        
        if(clutch_total_on_time > 0)
        {
          digitalWrite(clutch_sig_out,HIGH);                                                //Write the clutch trigger output pin high- put these writes where it is appropriate for you Twitch Program
        }
        else
        {
          digitalWrite(clutch_sig_out,LOW);                                                   //Write the clutch trigger output pin low-  put these writes where it is appropriate for you Twitch Program
        }


    delay(1000);

    return;
  }
}

The timer is checked in a callback function, which I presume is only called on incoming data.

You probably want to do this check independently, e.g., make a separate function and call this from loop().

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