Timer related/FSR sensor code question:

Hey all-

I’m having a sort of ‘Blynk best practices’ issue with a project I’m working on. To summarize it, I have a physical button that, when pressed (needs to be constantly detected for input), tells a piston to move, delays time until the piston is done moving, turns a filler machine on, and stops the filler when the weight needed (FSR analog input- needs to be constantly monitored/refreshed after the filler machine has been enabled) or a specific time interval has lapsed (in case the fsr fails)

I’m posting my code here- it… doesn’t work. I feel like I’m shooting in the dark a bit- if anyone can give some input / words of wisdom, that would be excellent.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>


BlynkTimer timer;

//Physical Connections:
//D1 on ESP to IN1 on RELAY Board
//D2 on ESP to IN2 on Relay Board


int pistonTimer = 0;
int augurTimer = 0;

//test environment
char auth[] = "1111";

char ssid[] = "blumpkin";
char pass[] = "blah";


WidgetLED pistonLED(V1);
WidgetLED augurLED(V2);

//Physical Buttons
const int buttonLeft = D5;
const int buttonRight = D6;

//TRACKING BUTTON STATE
int leftButtonState = 0;
int rightButtonState = 0;


//OUTPUTS TO RELAY BOARD
const int relayPiston = D1;
const int relayAugur = D2;

bool augurEnabled = false;

//Safe machine startup delays
int augurDelay = 1;
int pistonDelay = 1000;

float fsrSetting = 3.3;

float sensorValue;
float fsrValue = 0;
bool fsrWeight = false;

unsigned long currentMillis = millis();

unsigned long previousMillis = 0;
bool timerToggle = false;


//augurDelay from Blynk Slider
BLYNK_WRITE(3) {
  int setting = param.asInt();
  augurDelay = setting;
}

//piston delay from blynk slider
BLYNK_WRITE(4) {
  int setting = param.asInt();
  pistonDelay = setting;
}

//Voltage setting from blynk slider
BLYNK_WRITE(8) {
  float setting = param.asFloat();
  fsrSetting = setting;
}

void disableAugur() {
  timer.disable(augurTimer);
  digitalWrite(relayAugur, HIGH);
  augurLED.off();
  augurEnabled = false;
  Serial.println("E");

}


void enableAugur() {
  timer.disable(pistonTimer);
  digitalWrite(relayAugur, LOW);
  Serial.println("B");

  augurLED.on();
  if ((fsrWeight) || ((currentMillis - previousMillis) > augurDelay)) {
    Serial.println("D");

    disableAugur();

  } else {
    enableAugur();
    Serial.println("C");

  }
}



void setup()
{
  // Serial.begin(9600);

  pinMode(A0, INPUT);
  pinMode(buttonLeft, INPUT_PULLUP);
  pinMode(buttonRight, INPUT_PULLUP);
  pinMode(relayPiston, OUTPUT);
  digitalWrite(relayPiston, HIGH);
  pinMode(relayAugur, OUTPUT);
  digitalWrite(relayAugur, HIGH);
  augurLED.off();



  WiFi.mode(WIFI_STA);
  WiFi.begin("blumpkin", "blah");
  ArduinoOTA.setHostname("Augur Controller");
  ArduinoOTA.begin();
  delay(2000);

  //Enable for Farm
  //Blynk.begin(auth, ssid, pass, "192.168.1.50");

  //Test mode Blynk connection
  Blynk.begin(auth, ssid, pass);


}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V3, V4, V8);
}

void loop()
{
  ArduinoOTA.handle();

  Blynk.run();
  timer.run();
  currentMillis = millis();

  sensorValue = analogRead(A0);
  fsrValue = sensorValue * ( 3.3 / 1023.0);

  //Tests whether scale is at-weight or not
  if (fsrValue > fsrSetting) {
    fsrWeight = true;
  } else {
    fsrWeight = false;
  }

  leftButtonState = digitalRead(buttonLeft);
  rightButtonState = digitalRead(buttonRight);
  if (leftButtonState == 0 && augurEnabled == false) {
    augurEnabled = true;

    Serial.println("A");
    previousMillis = millis();

    digitalWrite(relayPiston, HIGH);
    pistonLED.on();

    //This gives piston a specific time to extend/retract before turning augur on
    pistonTimer = timer.setTimeout(pistonDelay, enableAugur);
  }

  if (rightButtonState == 0 && augurEnabled == false) {
    augurEnabled = true;

    Serial.println("A");
    previousMillis = millis();

    digitalWrite(relayPiston, HIGH);
    pistonLED.on();

    //This give piston a specific time to extend/retract before turning augur on
    pistonTimer = timer.setTimeout(pistonDelay, enableAugur);
  }
}
void dummyfunction() {

}

A Blynk best practices scenario has only these items in your void loop[()… everything else is dealt with timed routines and/or possibly interrupts for your button.

1 Like

You need to learn about interrupts. https://www.arduino.cc/en/Reference/AttachInterrupt

Here is a small example to help.

void setup(){
  // .. insert code
  attachInterrupt(buttonLeft, buttonLeftRoutine, FALLING); // wait for button to FALL from 1 to 0
  // .. 
}

// interrupts service routine runs when button FALLS to 0 
void buttonLeftRoutine(){
  // double check the button WAS pushed by checking the pin for LOW 
  if (!digitalRead(buttonLeft) && !augurEnabled) { 
    augurEnabled = true;

    Serial.println("A");
    previousMillis = millis();

    digitalWrite(relayPiston, HIGH);
    pistonLED.on();

    //This gives piston a specific time to extend/retract before turning augur on
    pistonTimer = timer.setTimeout(pistonDelay, enableAugur);
  }
}
1 Like

Awesome. This is almost exactly what I needed. Thank you!

1 Like