Exploring code techniques

The relay should arrive by Monday, fingers crossed. It will go where the green LED is.

In the meantime I continually have to keep an eye on the solar system, during which time I while away the hours reading, feeding the cat, browsing the web, and, my project for today, exploring ways of writing code more efficiently.

This is the code I showed in an earlier post:

void setup() {
Serial.begin(230400);
pinMode(D1,OUTPUT);
const int analogInPin=A0;
float ratioFactor=0.8;
float Voltage=0.0;
float sensorValue=0;
float sensorValueSample=0;
for(int i=0;i<10;i++){
sensorValueSample=analogRead(analogInPin);
sensorValue=sensorValue+sensorValueSample;
Serial.println(sensorValueSample);
delay(50);
}
sensorValue=(float)sensorValue/10.0;
Voltage=(float)(sensorValue*20)/1023.0;
Voltage=(float)(Voltage*ratioFactor);
if(Voltage<=12.4){
digitalWrite(D1,LOW);
Serial.println("LOW");
}else{
if(Voltage>=13.0){
digitalWrite(D1,HIGH);
Serial.println("HIGH");
}else{
Serial.println("MID");
}
}
Serial.print("Average A0 input from 10 samples over 0.5s = ");
Serial.print(Voltage);
Serial.println("V");
Serial.print("Average digital output from 10 samples over 0.5s = ");
Serial.println(sensorValue);
Serial.println("Going into deep sleep for 19.5s");
ESP.deepSleep(19.5e6);
}
void loop() {
}

It can be cleaned up by taking out all of the print code. All the Serial.print stuff is for debugging and calibration:

void setup() {
Serial.begin(230400);
pinMode(D1,OUTPUT);
const int analogInPin=A0;
float ratioFactor=0.8;
float Voltage=0.0;
float sensorValue=0;
float sensorValueSample=0;
for(int i=0;i<10;i++){
sensorValueSample=analogRead(analogInPin);
sensorValue=sensorValue+sensorValueSample;
delay(50);
}
sensorValue=(float)sensorValue/10.0;
Voltage=(float)(sensorValue*20)/1023.0;
Voltage=(float)(Voltage*ratioFactor);
if(Voltage<=12.4){
digitalWrite(D1,LOW);
}else{
if(Voltage>=13.0){
digitalWrite(D1,HIGH);
}
}
ESP.deepSleep(19.5e6);
}
void loop() {
}

This:

if(Voltage<=12.4){
digitalWrite(D1,LOW);
}else{
if(Voltage>=13.0){
digitalWrite(D1,HIGH);
}
}

Can be shortened using a ternary expression like this:

digitalWrite(D1,Voltage <=12.4 ? LOW : Voltage >=13.0 ? HIGH : 0);

Leaving me with:

void setup() {
Serial.begin(230400);
pinMode(D1,OUTPUT);
const int analogInPin=A0;
float ratioFactor=0.8;
float Voltage=0.0;
float sensorValue=0;
float sensorValueSample=0;
for(int i=0;i<10;i++){
sensorValueSample=analogRead(analogInPin);
sensorValue=sensorValue+sensorValueSample;
delay(50);
}
sensorValue=(float)sensorValue/10.0;
Voltage=(float)(sensorValue*20)/1023.0;
Voltage=(float)(Voltage*ratioFactor);
digitalWrite(D1,Voltage <=12.4 ? LOW : Voltage >=13.0 ? HIGH : 0);
ESP.deepSleep(19.5e6);
}
void loop() {
}

And I can shorten it further by replacing the constants with single characters, merging some calculations, and get rid of void loop() which is not being used:

void setup() {
Serial.begin(230400);
pinMode(D1,OUTPUT);
const int a=A0; // This is the value read at pin A0
float b=0.8; // This is the ratio factor to provide a correct value for voltage. It's not necessary but makes it a bit easier to change parameters later on
float c=0.0; // This is the voltage value
float d=0; // This is the average A0 value
float e=0; // This is for each instance of the value sampled at A0
for(int i=0;i<10;i++){
e=analogRead(a);
d=d+e;
delay(50);
}
c=(float)(d*2*b)/1023.0; // I've merged three lines of code here
digitalWrite(D1,c <=12.4 ? LOW : c >=13.0 ? HIGH : 0);
ESP.deepSleep(19.5e6);
}

I haven’t tested this yet. Might do that tomorrow.*

I also have a suspicion that I am not using float declarations correctly.**

* I did test it, and compiling failed. It appears that void loop() is required even if it’s not used. After putting that back in and adding Serial.print for debugging it ran correctly.

** float was not needed for d and e as they are integers ranging from 0 to 1023, so I replaced them with int. I also removed float for the final calculation of c because that was already declared.

One comment

Comments are closed.