From INT to STRING

Hi everyone!

I'm new to arduino and I'm trying to figure out how to transform an input value (via serial monitor) from int to string.

This is because I have to connect Arduino to VVVV.

So that you can send data to Arduino in an easier way.

In this case the value that must change is the one that is read by 3 different sensors. I set a threshold and wrote this sketch

void handleLight() {

int LightThr = 700;

  Light = analogRead(LDR_PIN);
  Serial.println(Light);
  pixels.begin();
  lcd.init();
  lcd.backlight();

     if (Serial.available())  
  {
    int value = Serial.parseInt(); 
    
    if (value > 1 && value <= 1024)
      LightThr = value;
  }

VVVV can only send values ​​as a string and so I was wondering if going to change

Serial.parseInt

with

Serial.parseString

could work

You are thinking backwards. In that sketch, the input value is already a string.

ah ok great then!

But then I can still define the variable like this

int LightThr = X;

or I have to define it with string, like this:

string LightThr = X;

First, show me how you intend to use the variable, 'LightThr'... also what 'X' is...

Why do you want to initialize that variable? You assign its value from an input...

Also, enquiring minds would want to know, why you are restricting the input range from 2 to 1024? What is special about those magic numbers?

Your function has no closing brace. Is that just a typo, or have you omitted some code that comes after that?

Here's another problem you made. This variable is local to the function, your main code won't be able to access it.

What does your function "do"? 'handleLight' only says that it "handles" it. If the purpose of the function is to obtain a value from serial, you should allow it to return the int value, by declaring the function 'int' and returning the value with 'return LightThr' instead of declaring it 'void'.

The project is as follows:

based on the threshold considered "optimal" of the data recorded by 3 sensors

  • photoresistor
  • DHT11
  • soil moisture sensor

a Neopixel led strip will light up in different colors.

LightThr is a value in Input because in this way I can go to modify the threshold in an easier way and I would like to be able to use VVVV to do it.

The values ​​are from 2 to 1024 because in another post I was told to ignore the 0 otherwise it would time out

Here is the complete sketch:

 #include <Adafruit_NeoPixel.h>
#include <RTClib.h>
#include <Wire.h>
#include <DHT.h>
#include <DHT_U.h>
#include <easyRun.h> 

int LightThr = 700;
int UmiThr = 600;
int TempThr = 23;

 // includere possibilità cambiare soglia vvvv
 
#include <LiquidCrystal_I2C.h>

DHT dht(11, DHT11);
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27,16,2);

const byte modePin = 3; 
button modeButton(modePin);
const uint32_t autoChangePeriod = 5000ul; // ogni 10s

enum : byte {LIGHT, SOIL, TEMPERATURE} mode = LIGHT;

#define LDR_PIN A0

#define USOIL A2

#define PIN 2   // input pin Neopixel 

#define NUMPIXELS    24// numero di pixel nell'annelo led

#define secondsSinceMidnight(h, m,s) (3600ul * (h) + 60ul * (m) + (s))

int light = 0;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

uint32_t dayStart = secondsSinceMidnight(6, 0, 0);  // 6:00:00 am
uint32_t dayEnd   = secondsSinceMidnight(22, 0, 0); // 10:00:00 pm

void dieHere(const char * errorMessage) {
  
  Serial.println(__FUNCTION__);
  Serial.println(errorMessage);
  while (true) yield();
}

void selectMode() {

  Serial.println(__FUNCTION__);
  static unsigned long lastChange = 0;
  easyRun();
  if (modeButton || (millis() - lastChange >= autoChangePeriod)) {
    
    // passare ad un altra funzione
    
   for (int i = 0; i <NUMPIXELS; i ++) pixels.setPixelColor (i, pixels.Color (0,0,0));      // turn LEDs off
    pixels.show ();
    
    lastChange = millis ();
    switch (mode) {
      case LIGHT: mode = SOIL; break;
      case SOIL: mode = TEMPERATURE; break;
      case TEMPERATURE: mode = LIGHT; break;
    }
  }
}

void handleLight() {

  Serial.println(__FUNCTION__);
  
  light = analogRead(LDR_PIN);
  Serial.println(light);
  pixels.begin();
  lcd.init();
  lcd.backlight();

     if (Serial.available())  // comunicazione seriale in input
  {
    int value = Serial.parseInt(); // trasforma ASCII in INT
    // Zero means timeout so ignore that value
    if (value > 1 && value <= 1024)
      LightThr = value;
  }
  
    if (LightThr < 500) { 
      
      for(int i=0;i<NUMPIXELS;i++)
         pixels.setPixelColor(i, pixels.Color(0,255,0)); 
         pixels.show();
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("Luce OK");
         delay (100);
     
     }  else { for(int i=0;i<NUMPIXELS;i++)
      
          pixels.setPixelColor(i, pixels.Color(255,0,0)); 
          pixels.show();
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Luce no OK");
          delay (100);
}
}

void handleSoil() {

  
  u = analogRead(USOIL); 

  Serial.println(__FUNCTION__);
  Serial.println(u);

     if (Serial.available())  
  {
    int value = Serial.parseInt(); // trasforma ASCII in INT
    // Zero means timeout so ignore that value
    if (value > 1 && value <= 1024)
      UmiThr = value;
  }


  if (UmiThr <= 600) { 
      
      for(int i=0;i<NUMPIXELS;i++)
         pixels.setPixelColor(i, pixels.Color(51,153,255)); 
         pixels.show();
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("Acqua OK");
         delay(100);
         
     
     }  else { for(int i=0;i<NUMPIXELS;i++)
      
          pixels.setPixelColor(i, pixels.Color(255,0,0)); 
          pixels.show();
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Acqua no OK");
          delay (100);

     }

}

void handleTemperature() {
  
 dht.begin();
 
 t = dht.readTemperature();

    if (Serial.available()) 
  {
    int value = Serial.parseInt(); // trasforma ASCII in INT
    // Zero means timeout so ignore that value
    if (value > 1 && value <= 1024)
      TempThr = value;
  }

  Serial.println(t);
 
  pixels.begin();
  
 if (TempThr >= 23  ) { 
      
      for(int i=0;i<NUMPIXELS;i++)
         pixels.setPixelColor(i, pixels.Color(153,51,255));  // violet
         pixels.show();
         lcd.clear();
         lcd.setCursor(0,0);
         lcd.print("Temp OK");
         delay (100);
     
     }  else { for(int i=0;i<NUMPIXELS;i++)
      
          pixels.setPixelColor(i, pixels.Color(255,0,0)); 
          pixels.show();
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Temp no OK");
          delay (100);
}
}


void setup() {
  Serial.begin(115200); Serial.println();
  Serial.println(__FUNCTION__);

  if (! rtc.begin()) dieHere("Couldn't find RTC");
  if (! rtc.isrunning()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // careful will be the time when you compiled the sketch
}

void loop () {
  Serial.println(__FUNCTION__); 
  selectMode ();
  DateTime now = rtc.now ();
  uint32_t sNow = secondsSinceMidnight (now.hour (), now.minute (), now.second ());
  if ((sNow >= dayStart) && (sNow <= dayEnd)) {
    switch (mode) {
      case LIGHT: handleLight (); break;
      case SOIL : handleSoil (); break;
      case TEMPERATURE: handleTemperature (); break;
    }
  }

}

Why ignore the 1? I see no code at all that would create any problem with 0. All you ever do with it, is compare with the value 500.

In fact you are right, I am still a beginner and had not considered the fact that it is > of 0 and not >=

The 'for' is missing a starting brace {

Have you tried auto-formatting or compiling this program?

How do you auto-format it?

However the sketch is compiled correctly and works

I just checked all the for loops and there are opening and closing brackets

Here is a section formatted to show you that the for loop only acts on one line. The rest of the lines do not iterate over 'i'. If that was intentional, you should change your formatting to clarify that, by documenting it or adding specific {} braces:

  if (TempThr >= 23  ) {

    for (int i = 0; i < NUMPIXELS; i++)
      pixels.setPixelColor(i, pixels.Color(153, 51, 255)); // violet
    pixels.show();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temp OK");
    delay (100);

What you really have there, is:

  if (TempThr >= 23  ) {

    for (int i = 0; i < NUMPIXELS; i++)
    {
      pixels.setPixelColor(i, pixels.Color(153, 51, 255)); // violet
    }
    pixels.show();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temp OK");
    delay (100);

  }

After some thought, I realize that it's intentional. So it's only the formatting that is deceptive. Did you write this code?

The code works the way I want it, I'm sure.

I wrote this code, putting together several sketches with the help of many users on this forum

ctrl-T or Tools->Auto Format in the IDE

Thank you!

Your non-standard formatting makes this code really hard to work on. Please restate your problem in clearer terms. What does it do, that it should not do? What does it not do, that it should do?

You are right and I apologize, I have now used auto format as you suggested.

What I would like to be able to do is to be able to change the variables:

LightThr
UmiThr
TempThr

Using VVVV instead of Arduino's serial monitor

How does VVVV transmit values? In what format? Can you give an example? Are they received on the Arduino's serial input? I'm not familiar with VVVV.

If they are text values, it should make no difference whether they originate with VVVV or the serial monitor. It would look the same to the Arduino.

Have you tried it with VVVV? If so, what was the result? If not, why?

Eh good question, I'm not very familiar with VVVV either for what I was asking here.

The only thing I know is that it passes values ​​as a string.

Anyway, back to the code, I'll ask you another question.

As you can see from the code, I have inserted a blocking function so that when the button is pressed, it changes function.

However this works for handleSoil and handleTemperature but not for handleLight.

In fact, as soon as the handleLight function starts, even if I press the button, nothing changes.

The same happens when the handleTemp or handleSoil function starts.

I can switch the latter two with the button only at the beginning, while handleLight even pressing the button never stops even at the beginning.

#include <Adafruit_NeoPixel.h>
#include <RTClib.h>
#include <Wire.h>
#include <DHT.h>
#include <DHT_U.h>
#include <easyRun.h>

int sogliaLuce = 700;
int sogliaUmi = 600;
int sogliaTemp = 23;

// includere possibilità cambiare soglia vvvv

#include <LiquidCrystal_I2C.h>

DHT dht(11, DHT11);
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);

const byte modePin = 3;
button modeButton(modePin);
const uint32_t autoChangePeriod = 5000ul; // ogni 10s

enum : byte {LIGHT, SOIL, TEMPERATURE} mode = LIGHT;

#define LDR_PIN A0

#define USOIL A2

#define PIN 2   // input pin Neopixel 

#define NUMPIXELS    24// numero di pixel nell'annelo led

#define secondsSinceMidnight(h, m,s) (3600ul * (h) + 60ul * (m) + (s))

int light = 0;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

uint32_t dayStart = secondsSinceMidnight(6, 0, 0);  // 6:00:00 am
uint32_t dayEnd   = secondsSinceMidnight(22, 0, 0); // 10:00:00 pm

void dieHere(const char * errorMessage) {

  Serial.println(__FUNCTION__);
  Serial.println(errorMessage);
  while (true) yield();
}

void selectMode() {

  Serial.println(__FUNCTION__);
  static unsigned long lastChange = 0;
  easyRun();
  if (modeButton || (millis() - lastChange >= autoChangePeriod)) {

    // passare ad un altra funzione

    for (int i = 0; i < NUMPIXELS; i ++) pixels.setPixelColor (i, pixels.Color (0, 0, 0));   // turn LEDs off
    pixels.show ();

    lastChange = millis ();
    switch (mode) {
      case LIGHT: mode = SOIL; break;
      case SOIL: mode = TEMPERATURE; break;
      case TEMPERATURE: mode = LIGHT; break;
    }
  }
}

void handleLight() {

  Serial.println(__FUNCTION__);

  light = analogRead(LDR_PIN);
  Serial.println(light);
  pixels.begin();
  lcd.init();
  lcd.backlight();

  if (Serial.available())  // comunicazione seriale in input
  {
    int value = Serial.parseInt(); // trasforma ASCII in INT
    // Zero means timeout so ignore that value
    if (value > 0 && value <= 1024)
      sogliaLuce = value;
  }

  if (sogliaLuce < 500) {

    for (int i = 0; i < NUMPIXELS; i++)
      pixels.setPixelColor(i, pixels.Color(0, 255, 0));
    pixels.show();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Luce OK");
    delay (100);

  }  else {
    for (int i = 0; i < NUMPIXELS; i++)

      pixels.setPixelColor(i, pixels.Color(255, 0, 0));
    pixels.show();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Luce no OK");
    delay (100);
  }
}

void handleSoil() {


int  u = analogRead(USOIL); //sensore umidità

  Serial.println(__FUNCTION__);
  Serial.println(u);

  if (Serial.available())  // comunicazione seriale in input
  {
    int value = Serial.parseInt(); // trasforma ASCII in INT
    // Zero means timeout so ignore that value
    if (value > 0 && value <= 1024)
      sogliaUmi = value;
  }


  if (sogliaUmi <= 600) { //condizione per far partire neopixel con umidità suolo

    for (int i = 0; i < NUMPIXELS; i++)
      pixels.setPixelColor(i, pixels.Color(51, 153, 255));
    pixels.show();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Acqua OK");
    delay(100);


  }  else {
    for (int i = 0; i < NUMPIXELS; i++)

      pixels.setPixelColor(i, pixels.Color(255, 0, 0));
    pixels.show();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Acqua no OK");
    delay (100);

  }

}

void handleTemperature() {

  dht.begin();

  int t = dht.readTemperature();

  if (Serial.available())  // comunicazione seriale in input
  {
    int value = Serial.parseInt(); // trasforma ASCII in INT
    // Zero means timeout so ignore that value
    if (value > 0 && value <= 1024)
      sogliaTemp = value;
  }

  Serial.println(__FUNCTION__);
  Serial.println(t);

  pixels.begin();

  if (sogliaTemp >= 23  ) {

    for (int i = 0; i < NUMPIXELS; i++)
      pixels.setPixelColor(i, pixels.Color(153, 51, 255)); // violet
    pixels.show();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temp OK");
    delay (100);

  }  else {
    for (int i = 0; i < NUMPIXELS; i++)

      pixels.setPixelColor(i, pixels.Color(255, 0, 0));
    pixels.show();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Temp no OK");
    delay (100);
  }
}


void setup() {
  Serial.begin(115200); Serial.println();
  Serial.println(__FUNCTION__);

  if (! rtc.begin()) dieHere("Couldn't find RTC");
  if (! rtc.isrunning()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // careful will be the time when you compiled the sketch
}

void loop () {
  Serial.println(__FUNCTION__);
  selectMode ();
  DateTime now = rtc.now ();
  uint32_t sNow = secondsSinceMidnight (now.hour (), now.minute (), now.second ());
  if ((sNow >= dayStart) && (sNow <= dayEnd)) {
    switch (mode) {
      case LIGHT: handleLight (); break;
      case SOIL : handleSoil (); break;
      case TEMPERATURE: handleTemperature (); break;
    }
  }

}

Now you have two questions. Which one do you want help with?

Did you research it?