Hello,
My first post and question in this forum. ![]()
My current project is to make a weather station that reads values from different sensors, and sends the data wireless to the my house. Most of the data will contain decimals so I guess float is the best data type to use when receiving the data from the sensors.
For now I have a sensor (DHT11) that i can read humidity and temperature with. I leave the humidity for now and just reading the temperature.
What I want is to take the temperature (for example float t = 24.00 Celsius) as a float and loop trough every character to send each character as morse code to my house. I can print the temperature value as it is to the serial monitor but I can't loop the value character by character. I guess I have to convert the "float t" to a string or something so that the value can be looped.
Am I right or can I loop through the temperature value as it is some how?
In case I have to convert the float in to a string or char, what is the best way to do that?
Changing the loop and putting the temperature instead of the hardcoded string stringToMorseCode ("whatever") as below doesn't seem to work.
// Read temperature as Celsius
float t = dht.readTemperature(false);
for (int i = 0; i < sizeof(t) - 1; i++)
{
// Get the character in the current position
char tmpChar = t*;*
// Print the character to serial monitor
Serial.print(tmpChar);
// Send the character as morse code
GetChar(tmpChar);
}
Entire code:
```
/
Morse Code Project
This code will loop through a string of characters and convert these to morse code.
It will blink two LED lights and play audio on a speaker.
*/
#include<stdlib.h>
#include <DHT.h>
#define DHTPIN 7 // the sensors pinnumber
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// Create variable to define the output pins
int led = 9; // light up a led on transmission
int audio = 8; // output audio
int note = 1200; // music note/pitch
/*
Set the speed of your morse code
Adjust the 'dotlen' length to speed up or slow down your morse code
(all of the other lengths are based on the dotlen)
Here are the ratios code elements:
Dash length = Dot length x 3
Pause between elements = Dot length
(pause between dots and dashes within the character)
Pause between characters = Dot length x 3
Pause between words = Dot length x 7
http://www.nu-ware.com/NuCode%20Help/index.html?m...
*/
int dotLen = 40; // length of the morse code 'dot'
int dashLen = dotLen * 3; // length of the morse code 'dash'
int elemPause = dotLen; // length of the pause between elements of a character
int Spaces = dotLen * 3; // length of the spaces between characters
int wordPause = dotLen * 7; // length of the pause between words
void setup() {
Serial.begin(9600);
// Start reading the sensor
dht.begin();
}
void loop()
{
// Read temperature as Celsius
float t = dht.readTemperature(false);
char stringToMorseCode[] = "whatever ";
// Loop through the string and get each character one at a time until the end is reached
for (int i = 0; i < sizeof(stringToMorseCode) - 1; i++)
{
// Get the character in the current position
char tmpChar = stringToMorseCode[i];
// Print the character to serial monitor
Serial.print(tmpChar);
// Send the character as morse code
GetChar(tmpChar);
}
// Print out the temperature
Serial.print(t);
// Break line
Serial.println();
// Wait a while between measurements.
// using this delay while testing
delay(2000);
// Aprox every five minutes
//delay(299750);
}
// DOT
void MorseDot()
{
tone(audio, note, dotLen); // start playing a tone
delay(dotLen); // hold in this position
}
// DASH
void MorseDash()
{
tone(audio, note, dashLen); // start playing a tone
delay(dashLen); // hold in this position
}
// Turn Off
void LightsOff(int delayTime)
{
noTone(audio); // stop playing a tone
delay(delayTime); // hold in this position
}
// *** Characters to Morse Code Conversion *** //
void GetChar(char tmpChar)
{
// Take the passed character and use a switch case to find the morse code for that character
switch (tmpChar) {
case '1':
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case '2':
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case '3':
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case '4':
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
case '5':
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case '6':
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case '7':
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case '8':
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case '9':
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDot();
LightsOff(elemPause);
break;
case '0':
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
MorseDash();
LightsOff(elemPause);
break;
default:
// If a matching character was not found it will default to a blank space
LightsOff(Spaces);
}
}
_```*_