MKRWAN1300 LED connected not lighting up

Hi all,

I am using the MKRWAN1300 board along with the Sharp GP2Y0A21YK0F Analog Distance Sensor, LDR sensor, and a push button for this project.

I am trying to send the sensor data to the TTN network as well as integrating that part with Adafruit IO to make graphs.

I have recently connected an LED and programmed it so when the button is pressed once, the LED will turn on, and when the sharp sensor senses something between 650 and 750 the LED will turn off.

I have used the same code without the communication to the TTN part and it seems to be working fine ( for the Sharp sensor and LED Part ). When I combine the code, I seem to be getting results from the TTN network however my LED does not seem to switch ON. It seems like it is completely ignoring the LED switch ON part.

As shown in the code, I have added the LED part to the \\buttonpart\\ section in the code

Apologies in advance, I am very new to this, any help would be much appreciated.

Attached below is the code I used. ( sorry about all the comments)

//BY VICHAI KONANGI

// Hardware:
//   Arduino MKRWAN 1300 https://store.arduino.cc/usa/mkr-wan-1300
//   DHT22 Temperature and Humidity Sensor
//
// Libraries:
//   MKRWAN https://github.com/arduino-libraries/MKRWAN
//   CayenneLPP https://github.com/sabas1080/CayenneLPP
//   Adafruit DHT https://github.com/adafruit/DHT-sensor-library

#include <MKRWAN.h>
#include <CayenneLPP.h>
#define LDR_PIN A2   // LDR sesnor
#define SHARP_PIN A1 // Sharp pin number
const int ledPin =  5;      // the number of the LED pin

//#define BUTTON_PIN 1 // Pushbutton pin number

const int buttonPin = 2;
//#define LDR_LIMIT 500
//#define TIME_LIMIT 20000
//#define DISTANCE 400
int buttonState = 0;

int buttonPressed = 0, LdrFlag = 0;

#include "arduino_secrets.h"
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;

LoRaModem modem;

//#define DHTPIN 2
//#define DHTTYPE DHT22
//DHT dht(DHTPIN, DHTTYPE);
//

void setup() {
  Serial.begin(9600);
  // Uncomment the next line to wait for a serial connection when debugging
  //while (!Serial);

  // change this to your regional band (eg. US915, AS923, ...)
  if (!modem.begin(AS923)) {
    Serial.println("Failed to start LoRa");
    while (1) {}
  };

  Serial.print("Your module version is: ");
  Serial.println(modem.version());
  Serial.print("Your device EUI is: ");
  Serial.println(modem.deviceEUI());
  // initialize the pushbutton pin as an input:
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.println("Attempting to join LoRaWAN network with OTAA");
  int connected = modem.joinOTAA(appEui, appKey);
  if (!connected) {
    Serial.println("Something went wrong; are you indoor? Move near a window and retry");
    while (1) {}
  }
  Serial.println("Join Successful");

  modem.minPollInterval(60);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  //////////////////////////////////////////////BUTTON PART///////////////////
  buttonState = digitalRead(buttonPin);
  float  distSharp = analogRead(SHARP_PIN);
  //      Serial.println(distSharp);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  if (buttonState == HIGH) {

    digitalWrite(ledPin, HIGH);
    Serial.println(buttonState);
    //    delay(3000);
    //    Serial.println(distSharp);

  }
  else if (buttonState == LOW && distSharp > 600 && distSharp < 700 ) {

    digitalWrite(ledPin, LOW);
    Serial.println(distSharp);
  }
  /////////////////////////////////////////////////////////////////////////////



  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
    Serial.println(buttonState);

  } else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
    Serial.println(buttonState);

  }
  float light = analogRead(LDR_PIN);
  float  light2 = light / 100;
  //  float distSharp = analogRead(SHARP_PIN);
  float distSharp2 = distSharp / 10;


  // read values from DHT
  //  float temperature = dht.readTemperature();
  //  float humidity = dht.readHumidity();

  // Check if any reads failed and exit early (to try again)
  if (isnan(light) || isnan(distSharp)) {
    Serial.println("Failed to read from LDR and Sharp sensor!");
    //    delay(1000);
    return;
  }
  //TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
  CayenneLPP lpp(51);                    // create a buffer of 51 bytes to store the payload
  // Prepare Cayenne LPP
  lpp.reset();
  lpp.addTemperature(1, light2);
  lpp.addRelativeHumidity(2, distSharp2);
  lpp.addRelativeHumidity(3, buttonState);

  Serial.println(light2);

  Serial.println(distSharp2);

  // Send the data
  modem.beginPacket();
  modem.write(lpp.getBuffer(), lpp.getSize());
  int err = modem.endPacket(true);
  if (err > 0) {
    Serial.println("Message sent.");
  } else {
    Serial.println("Error sending data.");
  }

  // Wait 2.5 minutes between transmissions
  //  delay(1000 * 60 * 3);

PLEASE HELP

Sorry guys,

Here is my modified code, it still does not work properly.

// Send Temperature and Humidity from DHT-22 to The Things Network
// Demo for Maker Faire NY 2018
// Don Coleman
//BY VICHAI KONANGI

// Hardware:
//   Arduino MKRWAN 1300 https://store.arduino.cc/usa/mkr-wan-1300
//   DHT22 Temperature and Humidity Sensor
//
// Libraries:
//   MKRWAN https://github.com/arduino-libraries/MKRWAN
//   CayenneLPP https://github.com/sabas1080/CayenneLPP
//   Adafruit DHT https://github.com/adafruit/DHT-sensor-library

#include <MKRWAN.h>
#include <CayenneLPP.h>
#define LDR_PIN A2   // LDR sesnor
#define SHARP_PIN A1 // Sharp pin number
const int ledPin =  5;      // the number of the LED pin

//#define BUTTON_PIN 1 // Pushbutton pin number
//#define LED_PIN 7    // LED pin number
const int buttonPin = 2;
//#define LDR_LIMIT 500
//#define TIME_LIMIT 20000
//#define DISTANCE 400
int buttonState = 0;

int buttonPressed = 0, LdrFlag = 0;

#include "arduino_secrets.h"
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;

LoRaModem modem;

//#define DHTPIN 2
//#define DHTTYPE DHT22
//DHT dht(DHTPIN, DHTTYPE);
//

void setup() {
  Serial.begin(9600);
  // Uncomment the next line to wait for a serial connection when debugging
  //while (!Serial);

  // change this to your regional band (eg. US915, AS923, ...)
  if (!modem.begin(AS923)) {
    Serial.println("Failed to start LoRa");
    while (1) {}
  };

  Serial.print("Your module version is: ");
  Serial.println(modem.version());
  Serial.print("Your device EUI is: ");
  Serial.println(modem.deviceEUI());
  // initialize the pushbutton pin as an input:
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.println("Attempting to join LoRaWAN network with OTAA");
  int connected = modem.joinOTAA(appEui, appKey);
  if (!connected) {
    Serial.println("Something went wrong; are you indoor? Move near a window and retry");
    while (1) {}
  }
  Serial.println("Join Successful");

  modem.minPollInterval(60);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  //////////////////////////////////////////////BUTTON PART///////////////////
  buttonState = digitalRead(buttonPin);
  float  distSharp = analogRead(SHARP_PIN);
  //      Serial.println(distSharp);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  if (buttonState == HIGH) {

    digitalWrite(ledPin, HIGH);
    Serial.println(buttonState);
    //    delay(3000);
    //    Serial.println(distSharp);

  }
  else if (buttonState == LOW && distSharp > 600 && distSharp < 700 ) {

    digitalWrite(ledPin, LOW);
    Serial.println(distSharp);
  }
  /////////////////////////////////////////////////////////////////////////////



//  // check if the pushbutton is pressed.
//  // if it is, the buttonState is HIGH:
//  if (buttonState == LOW) {
//    // turn LED on:
//    digitalWrite(ledPin, LOW);
//    Serial.println(buttonState);
//
//  } else {
//    // turn LED off:
//    digitalWrite(ledPin, HIGH);
//    Serial.println(buttonState);
//
//  }
  float light = analogRead(LDR_PIN);
  float  light2 = light / 100;
  //  float distSharp = analogRead(SHARP_PIN);
  float distSharp2 = distSharp / 10;


  // read values from DHT
  //  float temperature = dht.readTemperature();
  //  float humidity = dht.readHumidity();

  // Check if any reads failed and exit early (to try again)
  if (isnan(light) || isnan(distSharp)) {
    Serial.println("Failed to read from LDR and Sharp sensor!");
    //    delay(1000);
    return;
  }
  //TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
  CayenneLPP lpp(51);                    // create a buffer of 51 bytes to store the payload
  // Prepare Cayenne LPP
  lpp.reset();
  lpp.addTemperature(1, light2);
  lpp.addRelativeHumidity(2, distSharp2);
  lpp.addRelativeHumidity(3, buttonState);

  Serial.println(light2);

  Serial.println(distSharp2);

  // Send the data
  modem.beginPacket();
  modem.write(lpp.getBuffer(), lpp.getSize());
  int err = modem.endPacket(true);
  if (err > 0) {
    Serial.println("Message sent.");
  } else {
    Serial.println("Error sending data.");
  }

  // Wait 2.5 minutes between transmissions
  //  delay(1000 * 60 * 3);
  //  ldrDetection();
  //SharpSensorDetection();
  //ButtonV();



}

Please help

Hello again, Vichai.

It's still hard to help you. By the way, have you solved your previous problem posted here regarding the battery powering?

Moving on... Can you tell us what happens with your current code? Are you debugging on the USB Serial?
Can you tell if the button reading is working? And the distance sensor and LDR? And the LoRa connection?

If you can see the messages on the serial, please tell us what you got.

Trying to go a little deeper, I would suggest you to verify the LoRa connection issues with a simpler code, if you hadn't did yet.
If this is your first time, I can tell you that sending data over TTN isn't that easy, right out of the box...

You better solve one problem at a time.

Good luck.

mjunior-fitec:
It's still hard to help you.

+1.

By the way, in this code there is no delay at all in the loop, so likely there is no bounce control for the button, and also it attempts to send packets continuously... too much to help helpers :slight_smile:

MITEL:
By the way, in this code there is no delay at all in the loop, so likely there is no bounce control for the button,

Exactly. That's why my first question was regarding the button reading ...
Way too many things to be tested at once!