Im a student and working on a arduino project for IoT competition and me and my team have built a house and I made this code below. For some reason when I upload it to my Arduino MKR1000 it constantly turns D6 and D8 on and right after their both on it turns them off and so on for ever. I cant figure out why is it doing that because its only these 2 but not the rest of them.
Quick help would be appreciated.
Please note that I'm by any means a PRO at this but I do know my share about programming.
//Definiramo ispis na serial port
#define BLYNK_PRINT Serial
//Ucitavamo biblioteke
#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleMKR1000.h>
// Autorizacijski kod za aplikaciju Blynk
char auth[] = "Token";
// Podatci za WiFi
char ssid[] = "SSID";
char pass[] = "PASSWORD";
// ----- Inicializacija ledica -----
const int ledBorDim = 2;
const int ledKuh = 6;
const int ledTrp = 7;
const int ledKup = 8;
const int lediceUlaz = 9;
const int ledSpav = 10;
// ----- Inicializacija gumbova -----
const int gumbKuh = 11;
const int gumbKup = 12;
const int gumbSpav = 13;
const int gumbBor = 14;
// ----- Kontrolne varijable -----
int stanjeLedKuh = 0;
int stanjeLedKup = 0;
int stanjeLedBor = 0;
int stanjeLedSpav = 0;
int gumbStanjeKuh = 0;
int gumbStanjeSpav = 0;
int gumbStanjeBor = 0;
int gumbStanjeKup = 0;
//Led kuhinja
BLYNK_WRITE(V3)
{
int pinValue = param.asInt(); //Pinvalue postavljamo da bude vrijednost ono sto postavimo na aplikaciji
Serial.print("ledKuh = "); //Ispis na ekran trenutno stanje
Serial.println(pinValue);
if (pinValue == 1)
{
digitalWrite(ledKuh, HIGH);
stanjeLedKuh = 1;
} if (pinValue == 0)
{
digitalWrite(ledKuh, LOW);
stanjeLedKuh = 0;
}
}
//Ledica za kupaonicu
BLYNK_WRITE(V5)
{
int pinValue = param.asInt(); //Pinvalue postavljamo da bude vrijednost ono sto postavimo na aplikaciji
Serial.print("ledKup = "); //Ispis na ekran trenutno stanje
Serial.println(pinValue);
if (pinValue == 1)
{
digitalWrite(ledKup, HIGH); //Promjena stanja na izlazu arduina
stanjeLedKup = 1;
} if (pinValue == 0)
{
digitalWrite(ledKup, LOW); //Promjena stanja na izlazu arduina
stanjeLedKup = 0;
}
}
//Paralelni spoj ledica na ulazu
BLYNK_WRITE(V6)
{
int pinValue = param.asInt(); //Pinvalue postavljamo da bude vrijednost ono sto postavimo na aplikaciji
Serial.print("lediceUlaz = "); //Ispis na ekran trenutno stanje
Serial.println(pinValue);
if (pinValue == 1)
{
digitalWrite(lediceUlaz, HIGH); //Promjena stanja na izlazu arduina
} if (pinValue == 0)
{
digitalWrite(lediceUlaz, LOW); //Promjena stanja na izlazu arduina
}
}
//Dimmer za Boravak
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); //Ucitamo vrijednost
Serial.print("Dimmer vrijednost: "); //Ispisemo na serial
Serial.println(pinValue);
analogWrite(ledBorDim, pinValue); //Na izlaz postavljamo vrijednost
if (pinValue == 0)
{
Blynk.virtualWrite(V50, 1);
stanjeLedBor = 0;
} else if (pinValue > 0)
{
Blynk.virtualWrite(V50, 0);
stanjeLedBor = 1;
}
}
BLYNK_WRITE(V50)
{
int pinValue = param.asInt();
if (pinValue == 1)
{
analogWrite(ledBorDim, 255);
Blynk.virtualWrite(V1, 255);
stanjeLedBor = 1;
} if (pinValue == 0)
{
digitalWrite(ledBorDim, LOW);
Blynk.virtualWrite(V1, 0);
stanjeLedBor = 0;
}
}
void gumbKuhinja()
{
gumbStanjeKuh = digitalRead(gumbKuh);
if (gumbStanjeKuh == LOW)
{
if(stanjeLedKuh == 1)
{
digitalWrite(ledKuh, LOW);
Blynk.virtualWrite(V3, 0);
Serial.println("Ledica na V3/D6 je LOW");
stanjeLedKuh = 0;
} else if (stanjeLedKuh == 0)
{
digitalWrite(ledKuh, HIGH);
Blynk.virtualWrite(V3, 1);
Serial.println("Ledica na V3/D6 je HIGH");
stanjeLedKuh = 1;
}
delay(500);
}
}
void gumbKupaonica()
{
gumbStanjeKup = digitalRead(gumbKup);
if (gumbStanjeKup == LOW)
{
if(stanjeLedKup == 1)
{
digitalWrite(ledKup, LOW);
Blynk.virtualWrite(V5, 0);
Serial.println("Ledica na V5/D8 je LOW");
stanjeLedKup = 0;
} else if (stanjeLedKup == 0)
{
digitalWrite(ledKup, HIGH);
Blynk.virtualWrite(V5, 1);
Serial.println("Ledica na V5/D8 je HIGH");
stanjeLedKup = 1;
}
delay(500);
}
}
void gumbSpavaca()
{
gumbStanjeSpav = digitalRead(gumbSpav);
if (gumbStanjeSpav == LOW)
{
if(stanjeLedSpav == 1)
{
digitalWrite(ledSpav, LOW);
Blynk.virtualWrite(V6, 0);
Serial.println("Ledica na V6/D10 je LOW");
stanjeLedSpav = 0;
} else if (stanjeLedSpav == 0)
{
digitalWrite(ledSpav, HIGH);
Blynk.virtualWrite(V6, 1);
Serial.println("Ledica na V6/D10 je HIGH");
stanjeLedSpav = 1;
}
delay(500);
}
}
void gumbBoravak()
{
gumbStanjeBor = digitalRead(gumbBor);
if (gumbStanjeBor == LOW)
{
if(stanjeLedBor == 1)
{
analogWrite(ledBorDim, 0);
Blynk.virtualWrite(V1, 0);
Serial.println("Ledica na V1/D2 je 0");
stanjeLedBor = 0;
} else if (stanjeLedBor == 0)
{
analogWrite(ledBorDim, 255);
Blynk.virtualWrite(V1, 1);
Serial.println("Ledica na V1/D2 je 255");
stanjeLedBor = 1;
}
delay(500);
}
}
void setup()
{
pinMode(ledBorDim, OUTPUT);
pinMode(ledKuh, OUTPUT);
pinMode(ledKup, OUTPUT);
pinMode(lediceUlaz, OUTPUT);
pinMode(ledSpav, OUTPUT);
pinMode(gumbKuh, INPUT);
pinMode(gumbKup, INPUT);
pinMode(gumbSpav, INPUT);
pinMode(gumbBor, INPUT);
// Debug console
Serial.begin(9600);
//Povezivanje na WiFi i na aplikaciju Blynk
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
gumbKuhinja();
gumbKupaonica();
gumbSpavaca();
gumbBoravak();
}
Serial outpout:
Ledica na V6/D10 je HIGH
Ledica na V1/D2 je 255
Ledica na V6/D10 je LOW
Ledica na V1/D2 je 0
Ledica na V6/D10 je HIGH
Ledica na V1/D2 je 255
Ledica na V6/D10 je LOW
Ledica na V1/D2 je 0
Ledica na V6/D10 je HIGH
Ledica na V1/D2 je 255
Ledica na V6/D10 je LOW
Ledica na V1/D2 je 0
Ledica na V6/D10 je HIGH
Ledica na V1/D2 je 255
Little label reading D14 means that that pin isnt suppoast to go to D4 but to D14 of the MKR1000 but since i dont have that board in the program im stuck with arduino Uno.
Short explanation of the code...
We build a house out of styrofoam and placed 6 Led's around and almost all can be controlled from the Blynk app on my device. Also there are 4 buttons controlling 4 Led's. I have it setup so that when i press a button it changes the state of a particular LED and then sets the appropriate value on the app (If LED is on then set app value to 1). Pretty much that that about this code. Sorry if you cant understand but I had to write it in my native language since this is going to be a compettition and I was told that they wouldn't like if the code was in english for some reason.


pinMode(gumbKuh, INPUT);what's connected this pin, a button? with external pullup/pulldown? The repeadetly called function could flip the D6 pin ingumbKuhinja()when is D11 oscillating.. Do you also get any serial output when the pin toggles?pinMode(x, INPUT)lines topinMode(x, INPUT_PULLUP)and change your program logic to "if the pin is LOW, the button is pressed and we must do something" (if it isn't already)? Without a pullup or pulldown on the buttons they will randomly read noise (like an antenna) which might trigger your logic to toggle the pin.