Using 2 arduinos and TMP36 to send message

Hi I'm working on two arduinos that communicate to each other by I2C method. One board acts like a master in monitoring room temp and sending the message to the receiver with the following condition: normal (below 30), alert( 30-70) and danger( above70). The receiver will use the input message to blink the led lights with the following condition: green for normal, yellow for alert, red for danger.
This is what I did so far for the master code
#include <Wire.h>
int allowableTemp=0;
byte I2C_temperaturesensor;
int temp= 0;
int tempC=0;
void setup()
{
pinMode(A0, INPUT);
Wire.begin(1);
}
void loop()
{
allowableTemp=30;
temp = analogRead(A0);
temp = temp / 1024; //* find percentage of input reading
temp = temp * 5; //* multiply by 5V to get voltage
temp = temp - 0.5; //* Subtract the offset
tempC = temp * 100; //* Convert to degrees

if (tempC < allowableTemp) //temp below 30
{
Serial.println("normal"); // blink normal
Wire.beginTransmission(1);
Wire.write(I2C_temperaturesensor);
Wire.endTransmission();
}
if (tempC >= allowableTemp && tempC < allowableTemp + 40) //temp between 30 and 70
{
Serial.println("alert"); //blink alert
Wire.beginTransmission(1);
Wire.write(I2C_temperaturesensor);
Wire.endTransmission();
}

if (tempC > allowableTemp + 40) //temp above 70
{
Serial.println("danger");//blink danger
Wire.beginTransmission(1);
Wire.write(I2C_temperaturesensor);
Wire.endTransmission();
}
delay(1000); // Wait for 1000 millisecond(s)

}
what should I do with the receiver

Read the forum guidelines.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Have you seen the master write, slave receive example in the Wire library examples?

yes I was able to get this code right here but for some reason it said
slave:9:18: error: 'dataRcv' was not declared in this scope

Wire.onReceive(dataRcv);

              ^~~~~~~

slave:10:18: error: 'dataRqst' was not declared in this scope

Wire.onRequest(dataRqst);

              ^~~~~~~~

slave:18:19: error: expected ';' before 'temp'

               temp = analogRead(A0);

               ^~~~

slave:32:26: error: expected ';' before ':' token

 digitalWrite(3, HIGH):

                      ^

slave:34:27: error: expected ';' before ':' token

   digitalWrite(4, LOW):

                       ^

slave:44:30: error: a function-definition is not allowed here before '{' token

void dataRcv(int numBytes) {

                          ^

slave:49:19: error: a function-definition is not allowed here before '{' token

void dataRqst() {

               ^

exit status 1

'dataRcv' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Post the code that generated those errors.

Look at the line before this line. Does the line end with ;? It should.

This and the next error is because you use a : (colon) to end the line. Should be a ; (semi-colon).

You have mismatched { and } so the compiler thinks that you are trying to define a function inside of another function. Hence the error.

Using the autoformat tool will point out some of those errors. If you post the code I can help with the others. Please post code in code tags and use the autoformat tool.

1 Like

Hi, Thank you. I was able to fix my code however, there was an issue with my slaver code the LED light couldn't change to different colors. Here is my codes
Master code

char chArray[7];
int tmp36=A0;
int allowableTemp = 0;
double temp;
double tempC;
void setup()
{
Serial.begin(9600);
pinMode(tmp36, INPUT);
}
void loop()
{
allowableTemp = 30;
temp = analogRead(tmp36);
temp = temp / 1024; //* find percentage of input reading
temp = temp * 5; //* multiply by 5V to get voltage
temp = temp - 0.5; //* Subtract the offset
tempC = temp * 100; //* Convert to degree

if (tempC < allowableTemp) //temp below 30
{
strcpy(chArray,"normal"); // blink normal
}
else if (tempC >= allowableTemp && tempC < allowableTemp + 40) //temp between 30 and 70
{
strcpy(chArray,"alert"); //blink alert
}

else //temp above 70
{
strcpy(chArray,"danger");//blink danger
}
}

//slaver code
char roomtemp [7];
int LED1=4;
int LED2=3;
int LED3=2;
void setup ()
{
Serial.begin(9600);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
}
void loop ()
{
Serial.readBytes(roomtemp,7);
Serial.println(roomtemp);
if ("normal"){
digitalWrite(LED1,HIGH);
delay(1000);
digitalWrite(LED1,LOW);
delay(1000);
}
else if("alert!"){
digitalWrite(LED2,HIGH);
delay(1000);
digitalWrite(LED2,LOW);
delay(1000);
}
else
{
digitalWrite(LED3,HIGH);
delay(1000);
digitalWrite(LED3,LOW);
delay(1000);
}
}

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