I have this transmit code, for my Arduino Uno. I want to make sure that this code will work. Can you check if it works or not? I think there might be an error in the transmit or receive void functions.
I used it to define the analogpin. I have a uno and I don't have any extra digital pins left because the LCD display connected to the uno takes most of those port up.
Clearly, you don't have time or inclination to read Arduino Reference.
Look, analogRead returns a value between 0 and 1023. Only one of those values even remotely resembles "LOW".
Analog pins can be used as digitals, but you must then use digitalRead(). Maybe, try that.
Oh, and if your button is low active, did you forget to INPUT_PULLUP? Or is there an external pullup?
I took everyone consideration into account and came up with this final code. It still doesn't work. The whole purpose is that when I hit one button that buttons signal will go to other arduino. From there the arduino will decide which sensor it has to involve with. Then it goes to that void function, and starts it up. After it is read the sensor returns the data back to master for the lcd to display. It's a fairly simple ish code. And before anyone asks this is supposed to be a wired remote control car which sense the environment.
Uno(Slave)
#include <Wire.h>
#include "DHT.h"
#define CO2 A0
int gas, co2lvl;
int sensor;
const int powerpin = 8;
const int delaytime = 5000;
const int tresh = 520;
int q;
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
#define Slave_Add 9
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(sensor,INPUT);
pinMode(CO2, INPUT);
Serial.begin(9600);
delay(1000);
Wire.begin(Slave_Add);
Wire.beginTransmission(Slave_Add);
}
void DHTtester(){
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
int y = int(h);
int z = int(t);
// Serial.print(F("Humidity: "));
Serial.print(h);
// Serial.print(F("% Temperature: "));
Serial.print(t);
// Serial.print(F("°C "));
// Serial.print(f);
Wire.write(y);
Wire.write(z);
}
void CO2tester(){
gas = analogRead(CO2);
co2lvl = gas - 120;
co2lvl = map(co2lvl, 0, 1024, 400, 5000);
Serial.println(gas);
Serial.print(co2lvl);
if ((co2lvl >= 350) && (co2lvl <= 1400))
{
Wire.write(byte(0));
}
else if ((co2lvl >= 1400) && (co2lvl <= 2000))
{
Wire.write(byte(1));
}
}
void Soiltester(){
digitalWrite(powerpin, HIGH);
delay(10);
sensor = analogRead(A2);
Wire.write(sensor);
}
void loop() {
Wire.requestFrom(1,9);
while(Wire.available())
q = Wire.read();
if(q>2){
DHTtester();
Serial.println("hello");
}
else if(q == 2){
CO2tester();
Serial.println("hello");
}
else if(q<2){
Soiltester();
Serial.println("hello");
}
}
So you've moved your transmit code to a Mega, to avoid the (nonexistent) I/O count limitations. I suppose, rather than understanding what could be done, that works.
It still doesn't work.
But your code still "doesn't work". We're going to do this one question at a time, so settle in for a long day.
First, how are your buttons wired - see image in post #16, please identify which wiring arrangement - #1, #2, #3?
hey i got it to work i moved it all to one arduino and coded it. I know this is a stupid question but can someone tell me how to code a servo motor to move 90 degrees stay there for 5 seconds and come back to 0.