Can you check if this code will work

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.

#include <Wire.h>
#include <LiquidCrystal.h>
#define Slave_Add 9
int push1=A1;
int PushVal;
int push2=A2;
int PushVal2;
int push3=A3;
int PushVal3;
LiquidCrystal lcd(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3);  
void setup() {
  Wire.begin(Slave_Add);
  lcd.begin(16,2);    
  lcd.clear();  
  pinMode(push1, INPUT);
  pinMode(push2, INPUT);
  pinMode(push3, INPUT);
  Wire.beginTransmission(Slave_Add);
  }
void readSensors() {
Wire.available();
byte Info=Wire.read();
Serial.println(Info);
}
void sendSignal(){
  PushVal=analogRead(push1);
  if(PushVal==LOW){
    Wire.write(1);
  }
  PushVal2=analogRead(push2);
  if(PushVal2 == LOW){
    Wire.write(1);
  }
  PushVal3=analogRead(push3);
  if(PushVal3 == LOW){
    Wire.write(1);
  }
}
void loop() {
sendSignal();
readSensors();
}

Did it "work" when you tried it?

I couldn't test it right now because I don't have my Arduino on me, but I wanna know does it logically makes sense.

  • Suggest you get your Arduino and try the sketch yourself, after all you wrote the sketch didn’t you ?

BTW

Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components.

1 Like

Nope

1 Like
2 Likes

why

  • Hint, are you reading a digital or analog pin ?
    :thinking:

what will the value of PushVal likely be? Strongly suggest you explore Wokwi, by the way, if you insist on leaving your Arduino at school/home/work.

1 Like

What does that do in the context of an analog pin?

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.

pushVal is supposed to either read high or low and depending on that it sends a signal to the uno which communicates with the mega

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?

2 Likes

Say what? If HIGH or LOW? Why not while(1)?

if (pushVal || !pushVal)
  mySerial.print("stuff"); // to Uno

external pull up can you show a code piece for that

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");
}
}

Mega(Master)

#include <Wire.h>
#include <LiquidCrystal.h>
#define Slave_Add 9
int push1=22;
int PushVal;
int push2=24;
int PushVal2;
int push3=26;
int PushVal3;
LiquidCrystal lcd(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3); 
byte Info; 
void setup() {
  Wire.begin(Slave_Add);
  lcd.begin(16,2);    
  lcd.clear();  
  pinMode(push1, INPUT);
  pinMode(push2, INPUT);
  pinMode(push3, INPUT);
  Wire.beginTransmission(Slave_Add);
  Serial.begin(9600);
  }
 

void readSensors() {
Wire.requestFrom(1,9);
while(Wire.available())
Info=Wire.read();
lcd.clear();
lcd.setCursor(0,0);
lcd.write("Sensor Value");
lcd.write(Info);
}
void sendSignal(){
  PushVal=digitalRead(push1);
  if(PushVal==HIGH){
    Wire.write(1);
    Serial.println("Working2");
  }
  PushVal2=digitalRead(push2);
  if(PushVal2 == HIGH){
    Wire.write(2);
    Serial.println("Working");
  }
  PushVal3=digitalRead(push3);
  if(PushVal3 == HIGH){
    Wire.write(3);
    Serial.println("Working3");
  }

}
void loop() {
sendSignal();
readSensors();
}

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.

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