0

Activating and Deactivating a Process without killing loop
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Activating and Deactivating a Process without killing loop

by P0tux on Tue Jul 23, 2019 10:28 am

Bellow is a small snip of the code I am using under the void loop(){}. My goal is to be able to stop and start (as it stands I only have stop) certain processes by sending a command through the serial port. As it stands the "K01" command works fine until the loop restarts which is expected. I have tried to use a while command however when the while command triggers the rest of the process is unable to continue or loop.

I hope this problem is easy to solve for those of you with more experience. Would greatly appreciate some help!


Code: Select all | TOGGLE FULL SIZE
float t01 = dht.readTemperature();
 Serial.print("Temp: ");
 myFile.print("Temp: ");

 Serial.println(t01);
 myFile.println(t01);

   if (t01 < 30){
       digitalWrite(5, HIGH);
       Serial.println("TempActive");
       myFile.println("TempActive");
    }
    if (t01 > 31){
       digitalWrite(5, LOW);
      Serial.println("TempInactive");
      myFile.println("TempInactive");
   
    }
    if(t01<=30 && t01>=31){
      Serial.println("TempIdle");
      myFile.println("TempIdle");
    }
    if(Serial.available()){
      input = Serial.read();
      if (input = "K01"){
        digitalWrite(5, LOW);
      }
    }

P0tux
 
Posts: 11
Joined: Tue Jul 09, 2019 6:26 pm

Re: Activating and Deactivating a Process without killing lo

by adafruit_support_bill on Tue Jul 23, 2019 11:00 am

Not enough to go on there without seeing your loop code.

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: Activating and Deactivating a Process without killing lo

by P0tux on Tue Jul 23, 2019 11:07 am

Ive attached the sketch. Hope this helps!

Code: Select all | TOGGLE FULL SIZE
//Ethernet
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
//the IP address for the shield:
byte ip[] = { 10, 1, 15, 243 };
unsigned int localPort = 5000;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
String dataReq; //string for data
int packetSize; //size of packet
EthernetUDP Udp; //Create a udp object

//Temp Sensor [DHT22]
#include "DHT.h"
#define DHTPIN 12   
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);


// Independent Temp Sensors
int SensorA = 0;
int SensorB = 1;
int SensorC = 2;

//SD
#include <SD.h>
const int chipSelect = 4;
File myFile;

//Absolute Orientation
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
Adafruit_BNO055 bno = Adafruit_BNO055(55);

//Time Keeping (internal)
int a = 0;
int b = 0;
int c = 0;

//Command and Control
char input;

void setup() {
Serial.begin(9600);
 
  SD.begin(chipSelect);
  bno.begin();
  bno.setExtCrystalUse(true);
  dht.begin(); 
  Ethernet.begin(mac, ip);
  Udp.begin(localPort); //Initialize Udp
  pinMode(5, OUTPUT);
  delay(100);
}

 void displayCalStatus(void)
{
  /* Get the four calibration values (0..3) */
  /* Any sensor data reporting 0 should be ignored, */
  /* 3 means 'fully calibrated" */
  uint8_t system, gyro, accel, mag;
  system = gyro = accel = mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);

  /* The data should be ignored until the system calibration is > 0 */
  Serial.print("\t");
  if (!system)
  {
    Serial.print("! ");
  }

  /* Display the individual values */
  Serial.print("Sys:");
  Serial.print(system, DEC);
  Serial.print(" G:");
  Serial.print(gyro, DEC);
  Serial.print(" A:");
  Serial.print(accel, DEC);
  Serial.print(" M:");
  Serial.print(mag, DEC);
}

void loop() {
Serial.println("");

float t01 = dht.readTemperature();

sensors_event_t event;
bno.getEvent(&event);               
File myFile = SD.open("datalog3.txt", FILE_WRITE);
  if (myFile) {
    a = a + 1;
    if (a > 59){
      a = 0;
      b = b + 1;
    }
    if (b > 59){
      b = 0;
      c = c + 1;
   }
   if( c > 11){
      b = 0;
      c = 0;
    }
  Serial.print("Time: ");
  myFile.print("Time: ");
  Serial.print(c);
  myFile.print(c);
  Serial.print(":");
  myFile.print(":");
  Serial.print(b);
  myFile.print(b);
  Serial.print(":");
  myFile.print(":");
  Serial.println(a);
  myFile.println(a); 
 
    auto link = Ethernet.linkStatus(); /* not currently in use*/
    Serial.print("Linkstatus: ");
    myFile.print("Linkstatus: ");
    switch (link) {
      case Unknown:
        Serial.println("LinkERROR");
        myFile.println("LinkERROR");
        break;
      case LinkON:
        Serial.println("LinkACTIVE");
        myFile.println("LinkACTIVE");
        break;
      case LinkOFF:
        Serial.println("LinkOFF");
        myFile.println("LinkOFF");
        break;
        }
       
 Serial.print("Temp: ");
 myFile.print("Temp: ");

 Serial.println(t01);
 myFile.println(t01);

   if (t01 < 30){
       digitalWrite(5, HIGH);
       Serial.println("TempActive");
       myFile.println("TempActive");
    }
    if (t01 > 31){
       digitalWrite(5, LOW);
      Serial.println("TempInactive");
      myFile.println("TempInactive");
   
    }
    if(t01<=30 && t01>=31){
      Serial.println("TempIdle");
      myFile.println("TempIdle");
    }
    if(Serial.available()){
      input = Serial.read();
      if (input = "K01"){
        digitalWrite(5, LOW);
      }
    }
 
  Serial.print("X: ");
  myFile.print("X: ");
  Serial.print(event.orientation.x, 4);
  myFile.print(event.orientation.x, 4);
  Serial.print("\tY: ");
  myFile.print("\tY: ");
  Serial.print(event.orientation.y, 4);
  myFile.print(event.orientation.y, 4);
  Serial.print("\tZ: ");
  myFile.print("\tZ: ");
  Serial.print(event.orientation.z, 4);
  myFile.print(event.orientation.z, 4);


  /* Optional: Display calibration status: useful in determining if there is ever a fault or error */
  displayCalStatus();
  Serial.println("");
  myFile.println("");
}

//Files over ethernet for live updates
  packetSize = Udp.parsePacket();
  if(packetSize>0){
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    String datReq(packetBuffer);

    if (datReq == "Temp"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(t01);
      Udp.endPacket();
    }
     if (datReq == "IMUx"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.x);
      Udp.endPacket();
    }
    if (datReq == "IMUy"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.y);
      Udp.endPacket();
    }
    if (datReq == "IMUz"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.z);
      Udp.endPacket();
    }
  }
   memset (packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE); //clear packet buffer array
   delay(1000);
   myFile.close();     }
Attachments
LINKYFINAL.ino
(5.08 KiB) Downloaded 14 times
Last edited by adafruit_support_bill on Tue Jul 23, 2019 11:12 am, edited 1 time in total.
Reason: posted code in-line

P0tux
 
Posts: 11
Joined: Tue Jul 09, 2019 6:26 pm

Re: Activating and Deactivating a Process without killing lo

by adafruit_support_bill on Tue Jul 23, 2019 11:21 am

You need to maintain your process state outside of the loop. For a simple on/off kind of state, a boolean variable defined at global scope should be sufficient:

Code: Select all | TOGGLE FULL SIZE
boolean processRun = false;  // default to not running


Then in your command handler, you can do something like:
Code: Select all | TOGGLE FULL SIZE
    if(Serial.available()){
      input = Serial.read();
      if (input = "K01"){
   processRun = false;
        digitalWrite(5, LOW);
      }
     if (input = "K02"){
       processRun = true;
      }
    }


Then you can wrap your other process code in an 'if'

Code: Select all | TOGGLE FULL SIZE
if (processRun)
{
   /// do process stuff
}

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: Activating and Deactivating a Process without killing lo

by P0tux on Tue Jul 23, 2019 5:02 pm

Thanks for all the help. While the process makes sense, when putting it into practice my pin 5 is writing as high by default even with the boolean defaulted to "false" which should write low. I have attached the new code below. It seems as though the sketch is choosing to go through with the processes under the if(process = true){} section without the process being "true".

I have thrown the Boolean value "process" at the top of the sketch prior to all loops.

Code: Select all | TOGGLE FULL SIZE
//Ethernet
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
//the IP address for the shield:
byte ip[] = { 10, 1, 15, 243 };
unsigned int localPort = 5000;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
String dataReq; //string for data
int packetSize; //size of packet
EthernetUDP Udp; //Create a udp object

//Temp Sensor [DHT22]
#include "DHT.h"
#define DHTPIN 12   
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);


// Independent Temp Sensors
int SensorA = 0;
int SensorB = 1;
int SensorC = 2;
boolean process = false; //default to not running

//SD
#include <SD.h>
const int chipSelect = 4;
File myFile;

//Absolute Orientation
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
Adafruit_BNO055 bno = Adafruit_BNO055(55);

//Time Keeping (internal)
int a = 0;
int b = 0;
int c = 0;

//Command and Control
char input;

void setup() {
Serial.begin(9600);
 
  SD.begin(chipSelect);
  bno.begin();
  bno.setExtCrystalUse(true);
  dht.begin(); 
  Ethernet.begin(mac, ip);
  Udp.begin(localPort); //Initialize Udp 
  pinMode(5, OUTPUT);
  delay(100);
}

 void displayCalStatus(void)
{
  /* Get the four calibration values (0..3) */
  /* Any sensor data reporting 0 should be ignored, */
  /* 3 means 'fully calibrated" */
  uint8_t system, gyro, accel, mag;
  system = gyro = accel = mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);

  /* The data should be ignored until the system calibration is > 0 */
  Serial.print("\t");
  if (!system)
  {
    Serial.print("! ");
  }

  /* Display the individual values */
  Serial.print("Sys:");
  Serial.print(system, DEC);
  Serial.print(" G:");
  Serial.print(gyro, DEC);
  Serial.print(" A:");
  Serial.print(accel, DEC);
  Serial.print(" M:");
  Serial.print(mag, DEC);
}

void loop() {
Serial.println("");

float t01 = dht.readTemperature();
sensors_event_t event;
bno.getEvent(&event);               
File myFile = SD.open("datalog3.txt", FILE_WRITE);
  if (myFile) {
    a = a + 1;
    if (a > 59){
      a = 0;
      b = b + 1;
    }
    if (b > 59){
      b = 0;
      c = c + 1;
   }
   if( c > 11){
      b = 0;
      c = 0;
    }
  Serial.print("Time: ");
  myFile.print("Time: ");
  Serial.print(c);
  myFile.print(c);
  Serial.print(":");
  myFile.print(":");
  Serial.print(b);
  myFile.print(b);
  Serial.print(":");
  myFile.print(":");
  Serial.println(a);
  myFile.println(a); 
 
    auto link = Ethernet.linkStatus(); /* not currently in use*/
    Serial.print("Linkstatus: ");
    myFile.print("Linkstatus: ");
    switch (link) {
      case Unknown:
        Serial.println("LinkERROR");
        myFile.println("LinkERROR");
        break;
      case LinkON:
        Serial.println("LinkACTIVE");
        myFile.println("LinkACTIVE");
        break;
      case LinkOFF:
        Serial.println("LinkOFF");
        myFile.println("LinkOFF");
        break;
        }
 Serial.print("Temp01: ");
 myFile.print("Temp01: ");

 Serial.println(t01);
 myFile.println(t01);
       
 if(Serial.available()){
      input = Serial.read();
      if (input = "A"){
        process = false;
        digitalWrite(5, LOW); //should be off by default and not trigger heater
        Serial.println("Temp01OFF");
      }
      if (input = "B"){ //used to switch process to active
        process = true;
      }
    }
         
 
if (process = true){
   if (t01 < 30){
       digitalWrite(5, HIGH);
       Serial.println("TempActive");
       myFile.println("TempActive");
    }
    if (t01 > 31){
       digitalWrite(5, LOW);
      Serial.println("TempInactive");
      myFile.println("TempInactive");
   
    }
    if(t01<=30 && t01>=31){
      Serial.println("TempIdle");
      myFile.println("TempIdle");
    }
}
   
  Serial.print("X: ");
  myFile.print("X: ");
  Serial.print(event.orientation.x, 4);
  myFile.print(event.orientation.x, 4);
  Serial.print("\tY: ");
  myFile.print("\tY: ");
  Serial.print(event.orientation.y, 4);
  myFile.print(event.orientation.y, 4);
  Serial.print("\tZ: ");
  myFile.print("\tZ: ");
  Serial.print(event.orientation.z, 4);
  myFile.print(event.orientation.z, 4);


  /* Optional: Display calibration status: useful in determining if there is ever a fault or error */
  displayCalStatus();
  Serial.println("");
  myFile.println("");
}

//Files over ethernet for live updates
  packetSize = Udp.parsePacket();
  if(packetSize>0){
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    String datReq(packetBuffer);

    if (datReq == "Temp"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(t01);
      Udp.endPacket();
    }
     if (datReq == "IMUx"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.x);
      Udp.endPacket();
    }
    if (datReq == "IMUy"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.y);
      Udp.endPacket();
    }
    if (datReq == "IMUz"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.z);
      Udp.endPacket();
    }
  }
   memset (packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE); //clear packet buffer array
   delay(1000);
   myFile.close();     }
Attachments
test.ino
(5.37 KiB) Downloaded 15 times
Last edited by adafruit_support_bill on Tue Jul 23, 2019 5:05 pm, edited 1 time in total.
Reason: posted code in-line

P0tux
 
Posts: 11
Joined: Tue Jul 09, 2019 6:26 pm

Re: Activating and Deactivating a Process without killing lo

by adafruit_support_bill on Tue Jul 23, 2019 5:10 pm

Please use the [/code] button when submitting code to the forums. Just paste your code between the [/code] tags.

This line will always evaluate to 'true' since the '=' operator is the assignment operator.

Code: Select all | TOGGLE FULL SIZE
if (process = true){


Instead you should use the equality operator '==':

Code: Select all | TOGGLE FULL SIZE
if (process == true){


Or even just:

Code: Select all | TOGGLE FULL SIZE
if (process){

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: Activating and Deactivating a Process without killing lo

by P0tux on Wed Jul 24, 2019 10:15 am

That update worked fantastic. The main issue now is while the process starts as off and I can send the activate process command perfectly, once activated I cannot send the opposite command.

So when sending the A01 command over serial the if (process == true){} portion begins however when attempting to send the K01 command the A01 process continues.

Any help would be super appreciated!

Code: Select all | TOGGLE FULL SIZE
//Ethernet
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
//the IP address for the shield:
byte ip[] = { 10, 1, 15, 243 };
unsigned int localPort = 5000;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
String dataReq; //string for data
int packetSize; //size of packet
EthernetUDP Udp; //Create a udp object

//Temp Sensor [DHT22]
#include "DHT.h"
#define DHTPIN 12   
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);


// Independent Temp Sensors
int SensorA = 0;
int SensorB = 1;
int SensorC = 2;
boolean process = false; //default to not running

//SD
#include <SD.h>
const int chipSelect = 4;
File myFile;

//Absolute Orientation
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
Adafruit_BNO055 bno = Adafruit_BNO055(55);

//Time Keeping (internal)
int a = 0;
int b = 0;
int c = 0;

//Command and Control
char input;

void setup() {
Serial.begin(9600);
 
  SD.begin(chipSelect);
  bno.begin();
  bno.setExtCrystalUse(true);
  dht.begin(); 
  Ethernet.begin(mac, ip);
  Udp.begin(localPort); //Initialize Udp 
  pinMode(5, OUTPUT);
  delay(100);
}

 void displayCalStatus(void)
{
  /* Get the four calibration values (0..3) */
  /* Any sensor data reporting 0 should be ignored, */
  /* 3 means 'fully calibrated" */
  uint8_t system, gyro, accel, mag;
  system = gyro = accel = mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);

  /* The data should be ignored until the system calibration is > 0 */
  Serial.print("\t");
  if (!system)
  {
    Serial.print("! ");
  }

  /* Display the individual values */
  Serial.print("Sys:");
  Serial.print(system, DEC);
  Serial.print(" G:");
  Serial.print(gyro, DEC);
  Serial.print(" A:");
  Serial.print(accel, DEC);
  Serial.print(" M:");
  Serial.print(mag, DEC);
}

void loop() {
Serial.println("");

float t01 = dht.readTemperature();
sensors_event_t event;
bno.getEvent(&event);               
File myFile = SD.open("datalog3.txt", FILE_WRITE);
  if (myFile) {
    a = a + 1;
    if (a > 59){
      a = 0;
      b = b + 1;
    }
    if (b > 59){
      b = 0;
      c = c + 1;
   }
   if( c > 11){
      b = 0;
      c = 0;
    }
  Serial.print("Time: ");
  myFile.print("Time: ");
  Serial.print(c);
  myFile.print(c);
  Serial.print(":");
  myFile.print(":");
  Serial.print(b);
  myFile.print(b);
  Serial.print(":");
  myFile.print(":");
  Serial.println(a);
  myFile.println(a); 
 
    auto link = Ethernet.linkStatus(); /* not currently in use*/
    Serial.print("Linkstatus: ");
    myFile.print("Linkstatus: ");
    switch (link) {
      case Unknown:
        Serial.println("LinkERROR");
        myFile.println("LinkERROR");
        break;
      case LinkON:
        Serial.println("LinkACTIVE");
        myFile.println("LinkACTIVE");
        break;
      case LinkOFF:
        Serial.println("LinkOFF");
        myFile.println("LinkOFF");
        break;
        }
 Serial.print("Temp01: ");
 myFile.print("Temp01: ");

 Serial.println(t01);
 myFile.println(t01);
       
 if(Serial.available()){
      input = Serial.read();
      if (input = "K01"){
        process = false;                //should be off by default and not trigger heater
      }
      if (input = "A01"){               //used to switch process to active
        process = true;
      }
    }
if (process == false){
  Serial.println("Temp01OFF");
  digitalWrite(5, LOW);           
}
 
if (process == true){
   if (t01 < 31){
       digitalWrite(5, HIGH);
       Serial.println("TempActive");
       myFile.println("TempActive");
    }
    if (t01 > 32){
       digitalWrite(5, LOW);
      Serial.println("TempInactive");
      myFile.println("TempInactive");
   
    }
    if(t01<=31 && t01>=32){
      Serial.println("TempIdle");
      myFile.println("TempIdle");
    }
}
   
  Serial.print("X: ");
  myFile.print("X: ");
  Serial.print(event.orientation.x, 4);
  myFile.print(event.orientation.x, 4);
  Serial.print("\tY: ");
  myFile.print("\tY: ");
  Serial.print(event.orientation.y, 4);
  myFile.print(event.orientation.y, 4);
  Serial.print("\tZ: ");
  myFile.print("\tZ: ");
  Serial.print(event.orientation.z, 4);
  myFile.print(event.orientation.z, 4);


  /* Optional: Display calibration status: useful in determining if there is ever a fault or error */
  displayCalStatus();
  Serial.println("");
  myFile.println("");
}

//Files over ethernet for live updates
  packetSize = Udp.parsePacket();
  if(packetSize>0){
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    String datReq(packetBuffer);

    if (datReq == "Temp"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(t01);
      Udp.endPacket();
    }
     if (datReq == "IMUx"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.x);
      Udp.endPacket();
    }
    if (datReq == "IMUy"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.y);
      Udp.endPacket();
    }
    if (datReq == "IMUz"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.z);
      Udp.endPacket();
    }
  }
   memset (packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE); //clear packet buffer array
   delay(1000);
   myFile.close();     }

P0tux
 
Posts: 11
Joined: Tue Jul 09, 2019 6:26 pm

Re: Activating and Deactivating a Process without killing lo

by adafruit_support_bill on Wed Jul 24, 2019 11:00 am

when attempting to send the K01 command the A01 process continues.

What do you mean by the "A01 process" ? The only "A01" in your code is the serial command. If there is more of your code that you want to suppress after a "K01" command, it needs to go into the "if (process == true)" conditional statement.

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: Activating and Deactivating a Process without killing lo

by P0tux on Wed Jul 24, 2019 11:21 am

Sorry for the confusing explanation, ill try to clean it up here.

When system turns on the process is set to false.
Code: Select all | TOGGLE FULL SIZE
boolean process = false; //default to not running


The initial command sent in order to start the process is A01. Once this command is sent, process is set to true and the process begins. This portion works perfectly.
Code: Select all | TOGGLE FULL SIZE
if (process == true){
   if (t01 < 31){
       digitalWrite(5, HIGH);
       Serial.println("TempActive");
       myFile.println("TempActive");
    }
    if (t01 > 32){
       digitalWrite(5, LOW);
      Serial.println("TempInactive");
      myFile.println("TempInactive");
   
    }
    if(t01<=31 && t01>=32){
      Serial.println("TempIdle");
      myFile.println("TempIdle");
    }
}


When I attempt to stop the process by re-setting the boolean value to false with K01 (shown below), nothing changes and the process continues to run as if it were set to true. The goal is to be able to turn this process on and off, currently once activated it just stays on.

Code: Select all | TOGGLE FULL SIZE
 if(Serial.available()){
      input = Serial.read();
      if (input = "K01"){
        process = false;                //should be off by default and not trigger heater
      }
      if (input = "A01"){               //used to switch process to active
        process = true;
      }
    }


Full Code Below.

Code: Select all | TOGGLE FULL SIZE
//Ethernet
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
//the IP address for the shield:
byte ip[] = { 10, 1, 15, 243 };
unsigned int localPort = 5000;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
String dataReq; //string for data
int packetSize; //size of packet
EthernetUDP Udp; //Create a udp object

//Temp Sensor [DHT22]
#include "DHT.h"
#define DHTPIN 12   
#define DHTTYPE DHT22   
DHT dht(DHTPIN, DHTTYPE);


// Independent Temp Sensors
int SensorA = 0;
int SensorB = 1;
int SensorC = 2;
boolean process = false; //default to not running

//SD
#include <SD.h>
const int chipSelect = 4;
File myFile;

//Absolute Orientation
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
Adafruit_BNO055 bno = Adafruit_BNO055(55);

//Time Keeping (internal)
int a = 0;
int b = 0;
int c = 0;

//Command and Control
char input;

void setup() {
Serial.begin(9600);
 
  SD.begin(chipSelect);
  bno.begin();
  bno.setExtCrystalUse(true);
  dht.begin(); 
  Ethernet.begin(mac, ip);
  Udp.begin(localPort); //Initialize Udp 
  pinMode(5, OUTPUT);
  delay(100);
}

 void displayCalStatus(void)
{
  /* Get the four calibration values (0..3) */
  /* Any sensor data reporting 0 should be ignored, */
  /* 3 means 'fully calibrated" */
  uint8_t system, gyro, accel, mag;
  system = gyro = accel = mag = 0;
  bno.getCalibration(&system, &gyro, &accel, &mag);

  /* The data should be ignored until the system calibration is > 0 */
  Serial.print("\t");
  if (!system)
  {
    Serial.print("! ");
  }

  /* Display the individual values */
  Serial.print("Sys:");
  Serial.print(system, DEC);
  Serial.print(" G:");
  Serial.print(gyro, DEC);
  Serial.print(" A:");
  Serial.print(accel, DEC);
  Serial.print(" M:");
  Serial.print(mag, DEC);
}

void loop() {
Serial.println("");

float t01 = dht.readTemperature();
sensors_event_t event;
bno.getEvent(&event);               
File myFile = SD.open("datalog3.txt", FILE_WRITE);
  if (myFile) {
    a = a + 1;
    if (a > 59){
      a = 0;
      b = b + 1;
    }
    if (b > 59){
      b = 0;
      c = c + 1;
   }
   if( c > 11){
      b = 0;
      c = 0;
    }
  Serial.print("Time: ");
  myFile.print("Time: ");
  Serial.print(c);
  myFile.print(c);
  Serial.print(":");
  myFile.print(":");
  Serial.print(b);
  myFile.print(b);
  Serial.print(":");
  myFile.print(":");
  Serial.println(a);
  myFile.println(a); 
 
    auto link = Ethernet.linkStatus(); /* not currently in use*/
    Serial.print("Linkstatus: ");
    myFile.print("Linkstatus: ");
    switch (link) {
      case Unknown:
        Serial.println("LinkERROR");
        myFile.println("LinkERROR");
        break;
      case LinkON:
        Serial.println("LinkACTIVE");
        myFile.println("LinkACTIVE");
        break;
      case LinkOFF:
        Serial.println("LinkOFF");
        myFile.println("LinkOFF");
        break;
        }
 Serial.print("Temp01: ");
 myFile.print("Temp01: ");

 Serial.println(t01);
 myFile.println(t01);
       
 if(Serial.available()){
      input = Serial.read();
      if (input = "K01"){
        process = false;                //should be off by default and not trigger heater
      }
      if (input = "A01"){               //used to switch process to active
        process = true;
      }
    }
if (process == false){
  Serial.println("Temp01OFF");
  digitalWrite(5, LOW);           
}
 
if (process == true){
   if (t01 < 31){
       digitalWrite(5, HIGH);
       Serial.println("TempActive");
       myFile.println("TempActive");
    }
    if (t01 > 32){
       digitalWrite(5, LOW);
      Serial.println("TempInactive");
      myFile.println("TempInactive");
   
    }
    if(t01<=31 && t01>=32){
      Serial.println("TempIdle");
      myFile.println("TempIdle");
    }
}
   
  Serial.print("X: ");
  myFile.print("X: ");
  Serial.print(event.orientation.x, 4);
  myFile.print(event.orientation.x, 4);
  Serial.print("\tY: ");
  myFile.print("\tY: ");
  Serial.print(event.orientation.y, 4);
  myFile.print(event.orientation.y, 4);
  Serial.print("\tZ: ");
  myFile.print("\tZ: ");
  Serial.print(event.orientation.z, 4);
  myFile.print(event.orientation.z, 4);


  /* Optional: Display calibration status: useful in determining if there is ever a fault or error */
  displayCalStatus();
  Serial.println("");
  myFile.println("");
}

//Files over ethernet for live updates
  packetSize = Udp.parsePacket();
  if(packetSize>0){
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    String datReq(packetBuffer);

    if (datReq == "A01"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print("Command Recieved");
      Udp.endPacket();
      process = true;
    }

    if (datReq == "Temp"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(t01);
      Udp.endPacket();
    }
     if (datReq == "IMUx"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.x);
      Udp.endPacket();
    }
    if (datReq == "IMUy"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.y);
      Udp.endPacket();
    }
    if (datReq == "IMUz"){
      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
      Udp.print(event.orientation.z);
      Udp.endPacket();
    }
  }
   memset (packetBuffer, 0, UDP_TX_PACKET_MAX_SIZE); //clear packet buffer array
   delay(1000);
   myFile.close();     }

P0tux
 
Posts: 11
Joined: Tue Jul 09, 2019 6:26 pm

Re: Activating and Deactivating a Process without killing lo

by kcl1s on Wed Jul 24, 2019 12:47 pm

Code: Select all | TOGGLE FULL SIZE
 if(Serial.available()){
      input = Serial.read();
      if (input = "K01"){    //put double == here to test for equivalency
        process = false;                //should be off by default and not trigger heater
      }
      if (input = "A01"){               //used to switch process to active     ---- put double == here to test for equivalency
        process = true;
      }
    }


As support Bill said above. When you are testing for equivalency such as in any if statement you need to use the double equal ( == ). If you use the single = you are assigning a value.

I teach coding to teens and this is always something that comes up. As I tell my students you need to understand the changes we suggest here not just blindly follow.

Fellow hobbyist
Keith

kcl1s
 
Posts: 1295
Joined: Tue Aug 30, 2016 12:06 pm

Re: Activating and Deactivating a Process without killing lo

by adafruit_support_bill on Wed Jul 24, 2019 12:55 pm

The problem is in your parsing of the input:
Code: Select all | TOGGLE FULL SIZE
 if(Serial.available()){
      input = Serial.read();

The first problem is that Serial.read() returns a single character - not a string.
https://www.arduino.cc/reference/en/lan ... rial/read/
Your code just reads a single character.

Code: Select all | TOGGLE FULL SIZE
      if (input = "K01"){
        process = false;                //should be off by default and not trigger heater
      }
      if (input = "A01"){               //used to switch process to active
        process = true;
      }
    }

The second pair of problems is that you are using the assignment operator '=' instead of the comparison operator '==' in your 'if' statements.

Since any value other that 0 is interpreted as 'true', both of the 'if' statements evaluate to true. And process is first assigned 'false' and then immediately re-assigned to 'true'.

There are lots of approaches to parsing serial input on the Arduino. Here are a few libraries:
https://github.com/WestfW/parser
https://jeelabs.org/2010/10/24/parsing-input-commands/
https://www.arduinolibraries.info/libraries/cmd-parser

adafruit_support_bill
 
Posts: 74687
Joined: Sat Feb 07, 2009 10:11 am

Re: Activating and Deactivating a Process without killing lo

by P0tux on Wed Jul 24, 2019 1:46 pm

Thanks for all your help! I actually took the easy way and decided to remove all serial command capabilities and rely solely on the Udp packets to control the arduino.

Vote: adafruit bill as best support

P0tux
 
Posts: 11
Joined: Tue Jul 09, 2019 6:26 pm

Please be positive and constructive with your questions and comments.