Hello everyone.
I've created an sketch to send the number "1" when a water level sensor detects theres no water and send the number "2" when the contrary using an arduino nano with those RF433MHz cards. Something happens thah, if I connect my project to the USB of my laptop, it works fine. If, for example, I try to connect using a USB 5V power supply, it won't work. So, I changed the nano for a Mega and it is exactly the same situation: works fine with the computer USB, it doesn't with a 9V power supply or a 5V USB power supply. here I let you the code:
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_sistema(2000); //for the radio device
int sensor; //the variables were write in spanish, sorry.
int sumatorio;
int contador;
int nivel;
void setup(){
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
rf_sistema.init();
Serial.println("initializing system");
sumatorio = 0;
contador = 0;
nivel = 0;
}
void loop(){
do{
sensor = analogRead(A1);
sumatorio += sensor;
contador += 1;
delay(500);
}while(contador <= 15);
nivel = sumatorio / 15;
Serial.println("First reading");
Serial.print("average level ");
Serial.println(nivel);
if(nivel <= 200){
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Low level of water detected ❌");
Serial.println("⛔ Starting re-filling process... ⛔");
sumatorio = 0;
contador = 0;
for(int i = 0; i < 100; i++){
const char *msg = "A";
rf_sistema.send((uint8_t *)msg, strlen(msg));
rf_sistema.waitPacketSent();
Serial.print(".");
}
Serial.println();
Serial.println("End of transmittion");
do{
sensor = analogRead(A1);
Serial.print("Current level: ");
Serial.println(sensor);
delay(1000);
}while(sensor <= 200);
}
else{
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Right level of water detected ✅");
Serial.println("⛔ Re-filling process finished. ⛔");
for(int j = 0; j < 100; j++){
const char *msg = "B";
rf_sistema.send((uint8_t *)msg, strlen(msg));
rf_sistema.waitPacketSent();
Serial.print(".");
}
Serial.println();
Serial.println("End of transmittion");
do{
sensor = analogRead(A1);
Serial.print("Current level: ");
Serial.println(sensor);
delay(1000);
}while(sensor > 200);
}
sumatorio = 0;
contador = 0;
nivel = 0;
}
I thank you in advance for your help.