I'm pretty sure I know what scope is. I could use the Variable to print in serial but I can't use it to give sdata1 a value. Here's my code:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
SoftwareSerial nodemcu(2,3);
#define SensorPin A0 //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00 //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40 //times of collection
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int pHArray[ArrayLenth]; //Store the average value of the sensor feedback
int pHArrayIndex=0;
float Celsius = 0;
float sdata1 = 0;
float sdata2 = 0;
String cdata;
void setup(void)
{
Serial.begin(9600);
nodemcu.begin(9600);
Serial.println("Water pH and Temp!");//Test the serial monitor
lcd.begin(16,2);
lcd.clear();
}
void loop(void)
{
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static float pHValue,voltage;
sensors.requestTemperatures();
Celsius = sensors.getTempCByIndex(0);
if(millis()-samplingTime > samplingInterval)
{
pHArray[pHArrayIndex++]=analogRead(SensorPin);
if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
pHValue = 3.5*voltage+Offset;
samplingTime=millis();
}
if(millis() - printTime > printInterval) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
Serial.print("Temp:");
Serial.print(Celsius);
lcd.setCursor ( 0, 0 );
lcd.print("Temp:");
lcd.print(Celsius);
Serial.print(" pH value: ");
Serial.println(pHValue,2);
lcd.setCursor ( 0, 1 );
lcd.print("pH value:");
lcd.println(pHValue,2);
sdata1 = Celcius;
sdata2 = pHValue;
cdata = cdata + sdata1+","+sdata2;
Serial.println(cdata);
nodemcu.println(cdata);
delay(2000);
cdata = "";
digitalWrite(LED,digitalRead(LED)^1);
printTime=millis();
}
}
double avergearray(int* arr, int number){
int i;
int max,min;
double avg;
long amount=0;
if(number<=0){
Serial.println("Error number for the array to avraging!/n");
return 0;
}
if(number<5){ //less than 5, calculated directly statistics
for(i=0;i<number;i++){
amount+=arr[i];
}
avg = amount/number;
return avg;
}else{
if(arr[0]<arr[1]){
min = arr[0];max=arr[1];
}
else{
min=arr[1];max=arr[0];
}
for(i=2;i<number;i++){
if(arr[i]<min){
amount+=min; //arr<min
min=arr[i];
}else {
if(arr[i]>max){
amount+=max; //arr>max
max=arr[i];
}else{
amount+=arr[i]; //min<=arr<=max
}
}//if
}//for
avg = (double)amount/(number-2);
}//if
return avg;
}
In the loop function, the error occurs when I want to give sdata1 the value of Celcius but I could print Celcius in Serial and lcd. Error message was "'Celcius' was not declared in this scope".Any idea how to fix this? Kinda in a pinch ![]()