0

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

Breadboard schematic

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 in gumbKuhinja() when is D11 oscillating.. Do you also get any serial output when the pin toggles? – Maximilian Gerhardt Feb 26 '18 at 22:03
  • Let me upload a quick schematic so you could see better. – mike6715b Feb 26 '18 at 22:06
  • But yes it is a button. – mike6715b Feb 26 '18 at 22:07
  • Can you try changing all the pinMode(x, INPUT) lines to pinMode(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. – Maximilian Gerhardt Feb 26 '18 at 22:18
  • 1
    This comment section is getting rather messy. With your minimal test code, did you try to short the button pin, which is D8, to GND to trigger it? Work on getting the "toggle LED by button press" working (follow arduino.cc/en/Tutorial/InputPullupSerial) first, then re-integrate code. If you are unsure on what voltage level a pin is, use a multimeter. – Maximilian Gerhardt Feb 26 '18 at 23:13
2

You had the following general problem:

The code you were using was configuring a pin into INPUT mode and reading the value on it. On this pin you conencted a button without extern pullup or pulldown resistor. This means that if the button is not pressed, it is not connected to anything and thus a floating pin. You cannot realibly read the value of the floating pin, as its voltage level may be on pretty much anything. It picks up interference like an antenna.

To solve this the button pin must be pulled to known voltage level if it is not connected (aka button is not pressed). We achieve this by using a resistor which is on one end connected to the button pin and on the other end connected to either VCC (+5V) or GND (0V). This has the effect that when the button is not pressed, the resistor pulls the voltage level of this pin up (if connected to VCC), or down if connected to GND.

We can either attach a resistor (e.g. 10K Ohm) in the circuit or active the internal (weak) pull-up resistors in our microcontroller. In Arduino, this is achieved by using pinMode( buttonPin, INPUT_PULLUP).

enter image description here

In this circuit with INPUT_PULLUP on D2, we have:

  • if the button is not pressed down, no connection to GND is made. The activated pull-up resistor will pull the voltage level of D2 to HIGH
  • if the button is pressed down, it makes a connection to GND and overrides the pullup resistor. Pin D2 now reads LOW.

The logic can of course also be inverted -- you can use a PULLDOWN resistor and instead of connecting the button to GND on the other side connect it do 5V. This make the button read LOW when unpressed and HIGH pressed.

So the modified code must be changed from

  pinMode(gumbKuh, INPUT);
  pinMode(gumbKup, INPUT);
  pinMode(gumbSpav, INPUT);
  pinMode(gumbBor, INPUT);

to

  pinMode(gumbKuh, INPUT_PULLUP);
  pinMode(gumbKup, INPUT_PULLUP);
  pinMode(gumbSpav, INPUT_PULLUP);
  pinMode(gumbBor, INPUT_PULLUP);

and the logic which detects a button pressed must be changed to trigger an action when the pin is LOW (because a PULLUP is used, button reads LOW when pressed, HIGH else).

So from code of the form

void gumbBoravak()
{
  gumbStanjeBor = digitalRead(gumbBor);
  if (gumbStanjeBor == HIGH)
  {
    /* LED toggling and debouncing */
    delay(500);
  }
}

To

void gumbBoravak()
{
  gumbStanjeBor = digitalRead(gumbBor);
  if (gumbStanjeBor == LOW)
  {
    /* LED toggling and debouncing */
    delay(500);
  }
}

Also I recommend you to watch this youtube video to learn more about floating pins and pullup/pulldown resistors: Pull up Resistor Tutorial

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.