Go Down

Topic: Using Millis with a RGB led (Read 58 times) previous topic - next topic

Franktastic

In this code I want to have it so when it says connecting the light is yellow and when it couldnt connect it is red and when it successfully sent I want it to be green but I dont know about millis when having different timings was wondering if someone could help me



NB: this is for sending temprature but so it wont be connected to the pc thats why i want the leds so you know if it works


Code: [Select]
/***********************************************************************

  Telenor NB-IoT Hello World

  Configures NB-IoT, connects and sends a message every 15 minutes.

  See our guide on how to connect the EE-NBIOT-01 to Arduino:
  https://docs.nbiot.engineering/tutorials/arduino-basic.html

  This example is in the public domain.

  Read more on the Exploratory Engineering team at
  https://exploratory.engineering/

***********************************************************************/
// Include the libraries we need
    int redPin= 5;
    int greenPin = 4;
    int bluePin = 3;
#include <OneWire.h>
#include <DallasTemperature.h>

#include <Udp.h>
#include <TelenorNBIoT.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// arrays to hold device address
DeviceAddress insideThermometer;

#ifdef SERIAL_PORT_HARDWARE_OPEN
/*
 * For Arduino boards with a hardware serial port separate from USB serial.
 * This is usually mapped to Serial1. Check which pins are used for Serial1 on
 * the board you're using.
 */
#define ublox SERIAL_PORT_HARDWARE_OPEN
#else
/*
 * For Arduino boards with only one hardware serial port (like Arduino UNO). It
 * is mapped to USB, so we use SoftwareSerial on pin 10 and 11 instead.
 */
#include <SoftwareSerial.h>
SoftwareSerial ublox(10, 11);
#endif

/*
 * Create an nbiot instance using default settings, which is the Telenor NB-IoT
 * Development Platform's APN (mda.ee) and 0 for both mobile country code and
 * mobile operator code (auto mode).
 *
 * It's also possible to specify a different APN, mobile country code and
 * mobile network code. This should speed up attaching to the network.
 *
 * Example:
 * TelenorNBIoT nbiot("mda.ee", 242, 01); // Telenor Norway
 *
 * See list of codes here: https://www.mcc-mnc.com/
 */
TelenorNBIoT nbiot;

// The remote IP address to send data packets to
// u-blox SARA N2 does not support DNS
IPAddress remoteIP(172, 16, 15, 14);
int REMOTE_PORT = 1234;

// How often we want to send a message, specified in milliseconds
// 15 minutes = 15 (min) * 60 (sec in min) * 1000 (ms in sec)
unsigned long INTERVAL_MS = (unsigned long) 5 * 60 * 1000;

void setup() {
      pinMode(redPin, OUTPUT);
      pinMode(greenPin, OUTPUT);
      pinMode(bluePin, OUTPUT);
  // Open a serial connection over USB for Serial Monitor
  Serial.begin(9600);

  Serial.println("Dallas Temperature IC Control Library Demo");

  // locate devices on the bus
  Serial.print("Locating devices...");
  sensors.begin();
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
 
  // Assign address manually. The addresses below will beed to be changed
  // to valid device addresses on your bus. Device address can be retrieved
  // by using either oneWire.search(deviceAddress) or individually via
  // sensors.getAddress(deviceAddress, index)
  // Note that you will need to use your specific address here
  //insideThermometer = { 0x28, 0x1D, 0x39, 0x31, 0x2, 0x0, 0x0, 0xF0 };

  // Method 1:
  // Search for devices on the bus and assign based on an index. Ideally,
  // you would do this to initially discover addresses on the bus and then
  // use those addresses and manually assign them (see above) once you know
  // the devices on your bus (and assuming they don't change).
  if (!sensors.getAddress(insideThermometer, 0)) Serial.println("Unable to find address for Device 0");
 
  // method 2: search()
  // search() looks for the next device. Returns 1 if a new address has been
  // returned. A zero might mean that the bus is shorted, there are no devices,
  // or you have already retrieved all of them. It might be a good idea to
  // check the CRC to make sure you didn't get garbage. The order is
  // deterministic. You will always get the same devices in the same order
  //
  // Must be called before search()
  //oneWire.reset_search();
  // assigns the first address found to insideThermometer
  //if (!oneWire.search(insideThermometer)) Serial.println("Unable to find address for insideThermometer");

  // show the addresses we found on the bus
  Serial.print("Device 0 Address: ");
  printAddress(insideThermometer);
  Serial.println();

  // set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
  sensors.setResolution(insideThermometer, 9);
 
  Serial.print("Device 0 Resolution: ");
  Serial.print(sensors.getResolution(insideThermometer), DEC);
  Serial.println();

  // Useful for some Arduino boards to wait for the Serial Monitor to open
  while (!Serial);

  // Open the serial connection between the Arduino board and the NB-IoT module
  ublox.begin(9600);
 
  // Try to initialize the NB-IoT module until it succeeds
  Serial.print("Connecting to NB-IoT module...\n");
  while (!nbiot.begin(ublox)) {
    Serial.println("Begin failed. Retrying...");
    delay(1000);
  }
 
  /*
   * You neeed the IMEI and IMSI when setting up a device in our developer
   * platform: https://nbiot.engineering
   *
   * See guide for more details on how to get started:
   * https://docs.nbiot.engineering/tutorials/getting-started.html
   */
  Serial.print("IMSI: ");
  Serial.println(nbiot.imsi());
  Serial.print("IMEI: ");
  Serial.println(nbiot.imei());

  // Try to create a socket until it succeeds
  while (!nbiot.createSocket()) {
    Serial.print("Error creating socket. Error code: ");
    Serial.println(nbiot.errorCode(), DEC);
    delay(100);
  }
}


void loop() {
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
 
  String dataToSend = "";
  float temp = sensors.getTempC(insideThermometer);
  dataToSend += "Tempraturen er: ";
  dataToSend += temp;
  dataToSend += "C";
 
  if (nbiot.isConnected()) {
    // Successfully connected to the network

    // Send message to remote server
    if (true == nbiot.sendString(remoteIP, REMOTE_PORT, dataToSend)) {
      Serial.println("Successfully sent data");
     
    } else {
      Serial.println("Failed sending data");
     
    }

    // Wait specified time before sending again (see definition above)
    delay(INTERVAL_MS);
  } else {
    // Not connected yet. Wait 5 seconds before retrying.
    Serial.println("Connecting...");
    delay(5000);
  }
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
 
}
  void setColor(int redValue, int greenValue, int blueValue) {
      analogWrite(redPin, redValue);
      analogWrite(greenPin, greenValue);
      analogWrite(bluePin, blueValue);
    }

UKHeliBob

Save the value of millis() when the action that you want to indicate happens, turn on the associated LED and set a boolean to true to act as a flag for the event.  Use a different boolean with a meaningful name for each event.

Later in loop() test whether the current time minus the start time is equal to or greater than the required period.  If so then test each boolean flag.  If you find one set to true then turn off the associated LED and set the boolean to false
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up