Hi to all!
I always have this luck to escalate from problems ![]()
Im doing a thermostat for start and stop a resistor. After a very frustrating attempt to do so by two pots and analog read.
I switch to a menu code... MENWIZ... make the menu set the variables all good to go, but then the code uses a normal screen to display sensors and stuff like in a char type...
1.-Problem; convert float number to a string or chart.[SOLVED... SORT OF]
After investigate, the c++ is capable to do so by using itoa, but for space reasons this was left
behind the arduino compiler.
So I search the web and I found a perfect function to do this; dtostrf...
Thanks to Nick Gammon on this post
So I implement on the sketch and works !
I tested using; //Code snip
dtostrf(tempC, 10, 2, stempC); // Pass the float value to char
Serial.print("String C"); Serial.println( stempC); //prints on serial
dtostrf(tempF, 10, 2, stempF);
Serial.print("String F"); Serial.println (stempF);
Serial.println("---------------------------------"); //space bar.
PROBLEM TO SOLVE;
The arduino stops after some iteration, I count 36.
After the problem strikes me... I seek to search a litte bit more and found the way to save on flash
static comments or strings like so;
dtostrf(tempC, 10, 2, stempC);
Serial.print(F("String C")); Serial.println( stempC);
dtostrf(tempF, 10, 2, stempF);
Serial.print(F("String F")); Serial.println (stempF);
Serial.print("freeMemory()=");Serial.println(freeMemory());
I was thinking, it might solve the problem but dont...
After that point Im kind of lost... I don't have an specific problem to deal width... just symptoms and I would thank you for some hints on this.
THINGS ALREADY DO;
1.- Install memoryfree library to see if there was any free memory... it marks 516 steady.
2.- Try to uncomment the static screen to see if the chars was overloading it.
3.- Clean the chars by mychar[0] = 0; At the begining of the loop to see if there was a rollover problem.
4.- Uninstall all libraries dont requiered for the program to work (include memoryfree);
5.- Try to count the numbers of iterations on each reaset by adding a variable like so;
global variable byte myVariable = 0;
void loop(){
myVariable = myVariable++
//myVariable++
//myVariable +1
Serial.print("My Variable") Serial.println(myVariable);
In all cases above the serial output was 1.
So I cant count precisely the iterations.
ALTERNATIVES TO SOLUTION
1.- Some sort of menu implementation to up and down two float values.
I already seek phi-menu, M2klib but there are too big for this.
2.- Find a problem on the sketch and fix it.
And this is were I ask for help to delimitate the problem since I dont know what else to do!!!!
I leave the complete code here:
//=====================================LIBRERIAS==================================================//
#include <MemoryFree.h>
#include <OneWire.h>
#include <dht.h> //Libreria para DTH
#include <DallasTemperature.h> //Libreria Ds1820
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> //Libreria IC2
#include <buttons.h>
#include <MENWIZ.h>
#include <EEPROM.h>
//=====================================COSNTRUCTORES =============================================//
// DEFINE ARDUINO PINS FOR THE NAVIGATION BUTTONS
#define UP_BUTTON_PIN 9
#define DOWN_BUTTON_PIN 10
#define LEFT_BUTTON_PIN 7
#define RIGHT_BUTTON_PIN 8
#define CONFIRM_BUTTON_PIN 12
#define ESCAPE_BUTTON_PIN 11
dht DHT; //constructor del objeto
#define DHT11_PIN 5 // Pin sensor DTH
#define ONE_WIRE_BUS 2 //Pin Sensor temperatura
OneWire oneWire(ONE_WIRE_BUS); //define un bus para comunicarse con cualquier sensor
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
DeviceAddress insideThermometer; // arrays to hold device address
menwiz menu;
//Constructor objeto LCD
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//=====================================GLOBAL VARIABLES==============================================//
float ti = 19.00;
float tf = 25.00;
const byte led =13; //led to chek if the heating element has staring
const byte rele = 4; //pin to start or stop heating element
int humidity = 0; //read the humidity sensor
float tempC = 0; //read the sensor temperature
float tempF = 0; //farenheit
char stempC [10]; //Char to hold the temperature on C
char stempF [10]; //Char to hold the temperature on F
int iteracion = 0;
//=====================================VOID SET UP ==================================================//
void setup(void)
{
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(led, OUTPUT);
//-----------------------------------MENWIZ MENU-------------------------------------------------//
_menu *r,*s1,*s2;
_var *v;
int mem;
// inizialize the menu object (16 colums x 2 rows)
menu.begin(&lcd,16,2);
//create the menu tree
r=menu.addMenu(MW_ROOT,NULL,F("MENU")); //create a root menu at first (required)
s1=menu.addMenu(MW_SUBMENU,r,F("AJUSTE TEMPER"));
s2=menu.addMenu(MW_VAR,s1,F("TEMP INCIO")); //add a terminal node (that is "variable");
s2->addVar(MW_AUTO_FLOAT,&ti,20.00,50.00,0.5); //create a variable of type "float number"...
s2=menu.addMenu(MW_VAR,s1,F("TEMP FINAL")); //add a terminal node (that is "variable");
s2->addVar(MW_AUTO_FLOAT,&tf,20.00,50.00,0.5); //create a variable of type "float number"...
menu.addUsrScreen(msc,10000);
menu.navButtons(UP_BUTTON_PIN,DOWN_BUTTON_PIN,ESCAPE_BUTTON_PIN,CONFIRM_BUTTON_PIN);
//-----------------------------------SENSOR DS1820-------------------------------------------------//
// locate devices on the bus
sensors.begin();
Serial.print(sensors.getDeviceCount(), DEC);
//Linea para debug
if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
Serial.print("Device 0 Address: ");
//Funcion para imprimir la direccion del dispositivo --printAddress(insideThermometer);
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(insideThermometer, 10);
}
//=====================================VOID LOOP ==================================================//
void loop(void){
iteracion = iteracion+1;
menu.draw();
limpia_char();
if (tempC < tf){ //Chek if the temperature is below the off heating element
digitalWrite(rele, LOW);
digitalWrite( led, ON);
}
if (tempC > ti){
digitalWrite(rele, HIGH); //chek if the temperature is above the start temperature
digitalWrite (led, OFF);
}
sensors.requestTemperatures(); // Comando para obtener temperatura
// It responds almost immediately. Let's print out the data
printTemperature(insideThermometer); // Use a simple function to print out the data
dtostrf(tempC, 10, 2, stempC);
Serial.print(F("String C")); Serial.println( stempC);
dtostrf(tempF, 10, 2, stempF);
Serial.print(F("String F")); Serial.println (stempF);
Serial.print(F("freeMemory()="));Serial.println(freeMemory());
Serial.print(F("Numero de iteracion = ")); Serial.println(iteracion);
Serial.println(F("---------------------------------"));
}
void msc(){
static char buf[7];
strcat(menu.sbuf, stempC); strcat(menu.sbuf,"C/n"); //1st lcd line
strcat(menu.sbuf, stempF); strcat(menu.sbuf,"F/n"); //2nd lcd line
menu.drawUsrScreen(menu.sbuf);
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress){
tempC = sensors.getTempC(deviceAddress);
tempF = DallasTemperature::toFahrenheit(tempC); // Converts tempC to Fahrenheit
Serial.print(F("Temp C: "));
Serial.print(tempC);
Serial.print(F(" Temp F: "));
Serial.println(tempF);
}
void limpia_char(){
stempC[10] = 0 ;
stempF[10] = 0 ;
}
Have some words on Spanish but I believe is quite understandable, if is not, my apologies.
Thank you very much for you help.
-Alex. ![]()

