Why is my code only doing my void setup?


#include <Arduino.h>
#include "DFRobot_LCD.h"
#include <Wire.h>
#include "WiFi.h"
#include "ThingSpeak.h"

// ThingSpeak----------------------------------------------------------------------/
WiFiClient client;
char ssid[] = "ASUS nanosun-123";
char pass[] = "201312279c"; 
int status = WL_IDLE_STATUS;
unsigned long myChannelNumber = 1967185;
const char * myWriteAPIKey = "A3Y9LSEOI6ETE19T";

// DO SENSOR-----------------------------------------------------------------/
#define DO_PIN A1
#define VREF 5000 // VREF(mv) 
#define ADC_RES 1024 //ADC Resolution/

#define SINGLE_POINT_CALIBRATION 0
#define READ_TEMP (25)

#define CAL1_V (8354) //mv  CHANGE AFTER CALIBRATION
#define CAL1_T (25)   //℃


const uint16_t DO_Table[41] = {
  14460, 14220, 13820, 13440, 13090, 12740, 12420, 12110, 11810, 11530,
  11260, 11010, 10770, 10530, 10300, 10080, 9860, 9660, 9460, 9270,
  9080, 8900, 8730, 8570, 8410, 8250, 8110, 7960, 7820, 7690,
  7560, 7430, 7300, 7180, 7070, 6950, 6840, 6730, 6630, 6530, 6410};

uint8_t Temperaturet;
uint16_t ADC_Raw;
uint16_t ADC_Voltage;
uint16_t DO;

int16_t readDO(uint32_t voltage_mv, uint8_t temperature_c)
{
  #if SINGLE_POINT_CALIBRATION == 00
    uint16_t V_saturation = (uint32_t)CAL1_V + (uint32_t)35 * temperature_c - (uint32_t)CAL1_T * 35;
    return (voltage_mv * DO_Table[temperature_c] / V_saturation);
  #else
    uint16_t V_saturation = (int16_t)((int8_t)temperature_c - CAL2_T) * ((uint16_t)CAL1_V - CAL2_V) / ((uint8_t)CAL1_T - CAL2_T) + CAL2_V;
    return (voltage_mv * DO_Table[temperature_c] / V_saturation);
  #endif
}

// pH SENSOR---------------------------------------/
DFRobot_LCD lcd(16, 2); //16 characters and 2 lines of show

#define PH_PIN 25 
float voltage,phvalue,temperature = 25;

float acidVoltage = 2000; //buffer solution at 4.o 
float neutralVoltage = 1390;   //buffer solution at 7.o 

const int Sensor_MINvalue1 = 6.5; // +-0.5 from ph 4.0
const int Sensor_MAXvalue1 = 7.5;



void setup()
{
  Serial.begin(115200);
  pinMode(D1, OUTPUT);  

    // LCD Begin //
  lcd.init();
  lcd.setCursor(4,0);
  lcd.print("Welcome");
  delay(1000);
  lcd.clear();

  lcd.setCursor(3,0);
  lcd.print("Reading ");
  lcd.setCursor(3,1);
  lcd.print("pH & DO");
  delay(1000);
  lcd.clear();

  lcd.setCursor(3,0);
  lcd.print("Please Wait");
  delay(1000);
  lcd.clear();
}



void loop()
{
    
  //ThingSpeak----------------------------------------------------------//
    float P = phvalue;
    float D = (readDO(ADC_Voltage, Temperaturet)/1000);

    ThingSpeak.setField(1,P);
    ThingSpeak.setField(2,D); 
    ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); 

    //-----------------------------------------------------------------------------------//
  //pH SENSOR
    static unsigned long timepoint = millis();

    if(millis()-timepoint>1000U){
      timepoint = millis();// timepoint = read temperature 

      voltage = analogRead (PH_PIN)/1024.0*5000; 

      float slope = (7.0-4.0)/((neutralVoltage-1500)/30 - (acidVoltage-1500)/3.0);
      float intercept = 7.0 - slope*(neutralVoltage-1500)/3.0;

      phvalue = slope*(voltage-1500)/3.0 + intercept;   // y=k*x + b [formula]

      if( phvalue < Sensor_MAXvalue1 && phvalue > Sensor_MINvalue1 ){
         Serial.print("Voltage:");
         Serial.print(voltage,1);
         Serial.print("PH:");
         Serial.println(phvalue,2);

          //LCD setting 16x2
         lcd.setCursor(0,0);
         lcd.print("PH:");
        
         lcd.print(phvalue,2);

         lcd.setCursor(0,1);
         lcd.print("Volt:");
         lcd.print(voltage/1000,2);} 
      else {
        lcd.init();
        lcd.setCursor(0,0);
        lcd.print("DOSING REQUIRED");
        digitalWrite(D1, LOW);// turn relay OFF
        delay(5000);
        digitalWrite(D1, HIGH);// turn relay OFF
        delay(3000);
        lcd.clear(); }   
    }

    Temperaturet = (uint8_t)READ_TEMP;
    ADC_Raw = analogRead(DO_PIN);
    ADC_Voltage = uint32_t(VREF) * ADC_Raw / ADC_RES;

    Serial.print("Temperaturet:\t" + String(Temperaturet) + "\t");
    Serial.print("ADC RAW:\t" + String(ADC_Raw) + "\t");
    Serial.print("ADC Voltage:\t" + String(ADC_Voltage) + "\t");
    Serial.println("DO:\t" + String(readDO(ADC_Voltage, Temperaturet)) + "\t"); 

    lcd.setCursor(9,0);
    lcd.print((readDO(ADC_Voltage, Temperaturet))/1000);
    lcd.print("mg/L");
    delay(5000);
    lcd.clear();   
    return; 
}

before adding thingSpeak the code was working normally however upon adding it, it begun to not read my void loop and only read my void setup again and again.
did I do something wrong?

Could be a power supply problem. The Arduino is resetting because of a voltage drop. Perhaps at the point where communications with thingspeak begins, the extra power/current required causes a voltage drop which resets the Arduino.

Insert some extra Serial.println("beginning/end of setup/loop") to figure out if that is true. Then insert more Serial.println("doing XYZ") at other points to narrow down where the reset is occurring.

Read the forum guide to find out what else you need to post so that forum members can help you diagnose the problem.

If the passwords and the likes are the real ones, I suggest that you edit your post and replace them by dummies (e.g. ***) when posting the code on the forum.

Include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Post a schematic.

Post an image of your project.

Which Micro Controller are you using?

Is this simulator code?

Make a new tab in your project. Call the tab MyThingsToKeepSeceret.h. Put your ssid and password into MyThingsToKeepSeceret.h, save. Add to the project an #include MyThingsToKeepSeceret.h, now we will not see your stuff.

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