Problem with values

Hi

i use a arduino mkr 1010 & kemo rain-detector m152, connecting on pin 4

i want to use the sensor to control the relay1 (pin 1)

my problem is now, that relay1 is switching on high at the start, and i dont know why

my goal is, to store the value 1/high, which should compared to pin4, to write the digitalpin1 (relay1) as high (check last point in the code)

can someone help?

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "TMPLrNGyjAuU"
#define BLYNK_DEVICE_NAME "ITSE21a"
#define RELAY1 = (V20)

#include <SPI.h>
#include <Arduino_MKRENV.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <math.h>
#include "RTClib.h"

RTC_Millis rtc;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XPCSpLcWsELvNRCeJL-dvt43-r4deHS8";


BlynkTimer timer;// this timer is required to send data from shield to blynk app in intervals 

char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
  char fmt[20];
  sprintf(fmt, "%%%d.%df", width, prec);
  sprintf(sout, fmt, val);
  return sout;
}

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Fitim";
char pass[] = "123456789";
int relay_1 = 1;
int val = 1;
 



// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1

BLYNK_WRITE(V1) // Switch between automatic and manual mode by changing the value of virtual pin  
{
  if(param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    digitalWrite(1,HIGH);  // Set digital pin 2 HIGH
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(1,LOW);  // Set digital pin 2 LOW    y
  }

}



BLYNK_WRITE(V20) // Executes when the value of virtual pin 1 changes
{
  if(param.asInt() == 1)
    {
    // execute this code if the switch widget is now ON                                      // Noch zu erfüllen Switch-Button muss nach abgelauferner Zeit wieder zurückfallen
    digitalWrite(1,HIGH);  // Set digital pin 2 HIGH
    Blynk.virtualWrite(V20,1);
    delay (9000);
    digitalWrite(1,LOW);
    Blynk.virtualWrite(V20,0);
    }

}
void setup()
{
  pinMode(1, OUTPUT); // Initialise digital pin 2 as an output pin
  pinMode(4, INPUT);
   digitalWrite(1, LOW);


  Serial.begin(9600);
  // this line is very immportant which i dont know why but it didnt work without it i got it from env arduino documentation 
  if (!ENV.begin()) 
  {
   Serial.println("Failed to initialize MKR ENV shield!");
    while (1);
  } 
  
  Blynk.begin(auth, ssid, pass);

  timer.setInterval(1000L, sendSensor);
  

    // following line sets the RTC to the date & time this sketch was compiled
     rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}


void loop()
{
  Blynk.run();
  timer.run();
}


void sendSensor()
{
  float h = ENV.readHumidity();
  float t = ENV.readTemperature(); 
  float lx = ENV.readIlluminance();
  
  // rounding all vairables to 2 decimal points
  char tt[16];
  dtostrf(t,1,2,tt);
char hh[16];
  dtostrf(h,1,2,hh);
  
  char lxx[16];
  dtostrf(lx,1,2,lxx);

  
  
  if (isnan(h) || isnan(t)) {
    Serial.println("Error");
    return;
  }
  
  Blynk.virtualWrite(V5, hh);
  Blynk.virtualWrite(V6, tt);
  Blynk.virtualWrite(V7, lxx);
  


    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    delay(1000);


   
  if (val == digitalRead(4))
  
    {
    digitalWrite(1,HIGH);
        
    }
    
    else 
    {
      digitalWrite(1,LOW);
      
    }

Please post a proper wiring diagram, schematics.
I don't know the maker board ant its pins. On an Arduino UNO pin 1 is used when downloading code or using serial monitor.

Is it a pure relay or a relay module? Please give a link to its datasheet.

image

its the internel relay form the MKR 1010 Board:

the sensor also responds to pin4

as i see it, it has something to do with my variable and value at this code section, if i take that away, everything works as it should.

but as soon as i use that, the ralay switches high from the beginning for no reason, and if i activate the sensor to send a high value, the relay gets LOW

normally the relay should be low, and only go high when the sensor reports high

int val = 0; 

 if (val == digitalRead(4))
  
    {
    digitalWrite(relay_1,HIGH);
        
    }
    
    else 
    {
      digitalWrite(relay_1,LOW);
      
    }

Okey, You use a relay shield.
The tutorial, "Learn how to control the relays", doesn't make it clear.

Powering things is often where mistakes are found. Please post schematics of Your build. Words are no good.

Line 1 raises a questions....
Line 2 makes schematics very interesting.

im really sorry, i dont have a shema of my build, its my first time im using arduino and c++

i think, maybe the main problem is this part?:

or is there another way how i can store the value HIGH as variable, so i can use it fr the relay

you know what is wrong here ? :

Google = versus ==

ok, I see the error

then how can I save the HIGH value of pin4 as a variable?

From where did You get the idea how to connect the circuits?
Make, draw that schematics now, and post it.

Good. Work on the theories You have. Not having Your circuitry set up I can't test it.....

You had that right. So now you are flailing, maybe doing as much harm as good.

Post the complete sketch you are dealing with, or a small complete example that supports your claim.

There is nothing obvsly wrong with the parts you've posted and it is unlikely that your theory is in any way worth pursuing.

a7

FLAILING. worth reading