Error on my Processing GUI code for Arduino , I need some help

Arduino code :


#include <Wire.h> 
#include "SparkFunMPL3115A2.h" 
#include "SparkFun_Si7021_Breakout_Library.h" 

MPL3115A2 myPressure; 
Weather myHumidity;



const byte WSPEED = 3;
const byte RAIN = 2;
const byte STAT1 = 7;
const byte STAT2 = 8;

const byte REFERENCE_3V3 = A3;
const byte LIGHT = A1;
const byte BATT = A2;
const byte WDIR = A0;

long lastSecond; 
byte seconds; 
byte seconds_2m; 
byte minutes; 
byte minutes_10m; 

long lastWindCheck = 0;
volatile long lastWindIRQ = 0;
volatile byte windClicks = 0;


byte windspdavg[120]; 

#define WIND_DIR_AVG_SIZE 120
int winddiravg[WIND_DIR_AVG_SIZE]; 
float windgust_10m[10]; 
int windgustdirection_10m[10]; 
volatile float rainHour[60]; 


int winddir = 0; 
float windspeedmph = 0; 
float windgustmph = 0; 
int windgustdir = 0; 
float windspdmph_avg2m = 0; 
int winddir_avg2m = 0; 
float windgustmph_10m = 0; 
int windgustdir_10m = 0; 
float humidity = 0; 
float temperature = 0; 
float rainin = 0; 
volatile float dailyrainin = 0; 
float pressure = 0;
float batt_lvl = 11.8; 
float light_lvl = 455; 


volatile unsigned long raintime, rainlast, raininterval, rain;


void rainIRQ()

{
  raintime = millis(); 
  raininterval = raintime - rainlast; 

  if (raininterval > 10) 
  {
    dailyrainin += 0.011; 
    rainHour[minutes] += 0.011; 

    rainlast = raintime; 
  }
}

void wspeedIRQ()

{
  if (millis() - lastWindIRQ > 10) 
  {
    lastWindIRQ = millis(); 
    windClicks++; 
  }
}


void setup()
{
  Serial.begin(9600);
  Serial.println("Weather Shield Example");

  pinMode(STAT1, OUTPUT); 
  pinMode(STAT2, OUTPUT); 

  pinMode(WSPEED, INPUT_PULLUP); 
  pinMode(RAIN, INPUT_PULLUP); 

  pinMode(REFERENCE_3V3, INPUT);
  pinMode(LIGHT, INPUT);

 
  myPressure.begin(); 
  myPressure.setModeBarometer(); 
  myPressure.setOversampleRate(7); 
  myPressure.enableEventFlags(); 

  
  myHumidity.begin();

  seconds = 0;
  lastSecond = millis();

  // attach external interrupt pins to IRQ functions
  attachInterrupt(0, rainIRQ, FALLING);
  attachInterrupt(1, wspeedIRQ, FALLING);

  // turn on interrupts
  interrupts();

  Serial.println("Weather Shield online!");

}

void loop()
{
  
  if (millis() - lastSecond >= 1000)
  {
    digitalWrite(STAT1, HIGH); //Blink stat LED

    lastSecond += 1000;

   
    if (++seconds_2m > 119) seconds_2m = 0;

   
    float currentSpeed = get_wind_speed();
    windspeedmph = currentSpeed;
    
    int currentDirection = get_wind_direction();
    windspdavg[seconds_2m] = (int)currentSpeed;
    winddiravg[seconds_2m] = currentDirection;
    

    
    if (currentSpeed > windgust_10m[minutes_10m])
    {
      windgust_10m[minutes_10m] = currentSpeed;
      windgustdirection_10m[minutes_10m] = currentDirection;
    }

    
    if (currentSpeed > windgustmph)
    {
      windgustmph = currentSpeed;
      windgustdir = currentDirection;
    }

    if (++seconds > 59)
    {
      seconds = 0;

      if (++minutes > 59) minutes = 0;
      if (++minutes_10m > 9) minutes_10m = 0;

      rainHour[minutes] = 0; 
      windgust_10m[minutes_10m] = 0; 
    }

  
    printWeather();

    digitalWrite(STAT1, LOW); //Turn off stat LED
  }

  delay(100);
}


void calcWeather()
{
  
  winddir = get_wind_direction();

 
  float temp = 0;
  for (int i = 0 ; i < 120 ; i++)
    temp += windspdavg[i];
  temp /= 120.0;
  windspdmph_avg2m = temp;

  
  long sum = winddiravg[0];
  int D = winddiravg[0];
  for (int i = 1 ; i < WIND_DIR_AVG_SIZE ; i++)
  {
    int delta = winddiravg[i] - D;

    if (delta < -180)
      D += delta + 360;
    else if (delta > 180)
      D += delta - 360;
    else
      D += delta;

    sum += D;
  }
  winddir_avg2m = sum / WIND_DIR_AVG_SIZE;
  if (winddir_avg2m >= 360) winddir_avg2m -= 360;
  if (winddir_avg2m < 0) winddir_avg2m += 360;

 
  windgustmph_10m = 0;
  windgustdir_10m = 0;

  for (int i = 0; i < 10 ; i++)
  {
    if (windgust_10m[i] > windgustmph_10m)
    {
      windgustmph_10m = windgust_10m[i];
      windgustdir_10m = windgustdirection_10m[i];
    }
  }

  
  humidity = myHumidity.getRH();
 

  
  temperature = myPressure.readTemp();
  

  
  rainin = 0;
  for (int i = 0 ; i < 60 ; i++)
    rainin += rainHour[i];

  
  pressure = myPressure.readPressure();


  light_lvl = get_light_level();

  
  batt_lvl = get_battery_level();
}


float get_light_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float lightSensor = analogRead(LIGHT);

  operatingVoltage = 3.3 / operatingVoltage; 

  lightSensor = operatingVoltage * lightSensor;

  return (lightSensor);
}


float get_battery_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float rawVoltage = analogRead(BATT);

  operatingVoltage = 3.30 / operatingVoltage; 

  rawVoltage = operatingVoltage * rawVoltage; 

  rawVoltage *= 4.90; 

  return (rawVoltage);
}


float get_wind_speed()
{
  float deltaTime = millis() - lastWindCheck; 

  deltaTime /= 1000.0; 

  float windSpeed = (float)windClicks / deltaTime; 

  windClicks = 0; 
  lastWindCheck = millis();

  windSpeed *= 1.492; 

  

  return (windSpeed);
}


int get_wind_direction()
{
  unsigned int adc;

  adc = analogRead(WDIR); 

 
  if (adc < 380) return (113);
  if (adc < 393) return (68);
  if (adc < 414) return (90);
  if (adc < 456) return (158);
  if (adc < 508) return (135);
  if (adc < 551) return (203);
  if (adc < 615) return (180);
  if (adc < 680) return (23);
  if (adc < 746) return (45);
  if (adc < 801) return (248);
  if (adc < 833) return (225);
  if (adc < 878) return (338);
  if (adc < 913) return (0);
  if (adc < 940) return (293);
  if (adc < 967) return (315);
  if (adc < 990) return (270);
  return (-1); 
}



void printWeather()
{
  calcWeather(); 

  Serial.println();
  Serial.print("winddir=");
  Serial.print(winddir);
  Serial.println();
  Serial.print("windspeedmph=");
  Serial.print(windspeedmph, 1);
  Serial.println();
  Serial.print("windgustmph=");
  Serial.print(windgustmph, 1);
  Serial.println();
  Serial.print("windgustdir=");
  Serial.print(windgustdir);
  Serial.println();
  Serial.print("windspdmph_avg2m=");
  Serial.print(windspdmph_avg2m, 1);
  Serial.println();
  Serial.print("winddir_avg2m=");
  Serial.print(winddir_avg2m);
  Serial.println();
  Serial.print("windgustmph_10m=");
  Serial.print(windgustmph_10m, 1);
  Serial.println();
  Serial.print("windgustdir_10m=");
  Serial.print(windgustdir_10m);
  Serial.println();
  Serial.print("humidity=");
  Serial.print(humidity, 1);
  Serial.println();
  Serial.print("tempf=");
  Serial.print(temperature, 1);
  Serial.println();
  Serial.print("rainin=");
  Serial.print(rainin, 2);
  Serial.println();
  Serial.print("dailyrainin=");
  Serial.print(dailyrainin, 2);
  Serial.println();
  Serial.print("pressure=");
  Serial.print(pressure, 2);
  Serial.println();
  Serial.print("batt_lvl=");
  Serial.print(batt_lvl, 2);
  Serial.println();
  Serial.print("light_lvl=");
  Serial.print(light_lvl, 2);
  Serial.println();
  Serial.print("");
  Serial.println();
  

delay(3000);
}

Processing code:


import processing.serial.*;


float winddir;
float windspeedmph;
float windgustmph;
float windgustdir;
float windspdmph_avg2m;
float winddir_avg2m;
float windgustmph_10m;
float windgustdir_10m;
float humidity;
float tempf;
float rainin;
float dailyrainin;
float pressure;
float batt_lvl;
float light_lvl;

String myString = null;
Serial myPort;





void setup()
{
size(700,700);
String portName = Serial.list()[5]; 
myPort = new Serial(this, portName, 9600);
myPort.clear();
myString = myPort.readStringUntil('\n');
myString = null;
}
void draw()
{
  while ( myPort.available() > 0) 
  {  
  myString = myPort.readStringUntil('\n'); 
  if (myString != null) {
    println(myString);
    
    String myString = myPort.readString();
    
      String[] q = splitTokens(myString);
      
    
    
    
    
      winddir = parseFloat(q[0]);
      windspeedmph = parseFloat(q[1]);
      windgustmph = parseFloat(q[2]);
      windgustdir = parseFloat(q[3]);
      windspdmph_avg2m = parseFloat(q[4]);
      winddir_avg2m = parseFloat(q[5]);
      windgustmph_10m = parseFloat(q[6]);
      windgustdir_10m = parseFloat(q[7]);
      humidity = parseFloat(q[8]);
      tempf = parseFloat(q[9]);
      rainin = parseFloat(q[10]);
      dailyrainin = parseFloat(q[11]);
      pressure = parseFloat(q[12]);
      batt_lvl = parseFloat(q[13]);
      light_lvl = parseFloat(q[14]);

      
background(0);
textSize(20);
text("Wind direction:",50,50);
text(winddir,250,50);
text("Wind speed:" ,50,100);
text(windspeedmph,250,100);
text("Wind gust:",50,150);
text(windgustmph,250,150);
text("Wind gust direction:",50,200);
text(windgustdir,250,200);
text("Humidity:",50,250);
text(humidity,250,250);
text("Temperature",50,300);
text(tempf,250,300);
text("Rainfall:",50,350);
text(rainin,250,350);
text("Daily rain:",50,400);
text(dailyrainin,250,400);
text("Pressure",50,450);
text(pressure,250,450);
text("Battery level:",50,500);
text(batt_lvl,250,500);
text("Light level:",50,550);
text(light_lvl,250,550);

     
   }
  } 
}

1 Like

Hello,

Take a look at this example:

Arduino code:

  • sends comma delimited string of data with Serial.print(',');
  • the end of the data string is terminated with a '\r' and a '\n' from Serial.println();
Arduino Code
void setup() 
  {
  Serial.begin(9600);
  }

void loop() 
  {  
  for(int x = 0; x < 15; x++) 
    {  
    Serial.print((float) x + random(0, 1000)/1000.0 , 3);   
    Serial.print(',');
    }
  Serial.println();  
  delay(1000);
  }

Processing code:

  • receives characters up to and including '\n'
  • uses trim() to trim the data of whitespace including '\r' and a '\n'
  • splits the data separated by a ',' into an array
Processing Code
import processing.serial.*;

String myString = null;
Serial myPort;

void setup()
  {
  size(700, 700);
  printArray(Serial.list());
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myString = myPort.readStringUntil('\n');
  myString = null;
  }

void draw()
  {
  while (myPort.available() > 0)
    { // If data is available,
    String myString = myPort.readStringUntil('\n');
    //println(myString);
    if (myString != null) 
      {
      //println(myString);
      myString = myString.trim();
      //println(myString);

      //String[] q = split(myString, ','); 
      String[] q = splitTokens(myString, ",");
      printArray(q);
      
      if(q.length == 16)
        {
        printArray(q);
        }
      }
    }
  }

In my Arduino example…

The Arduino Serial.println(); sends a '\r' and a '\n':
https://www.arduino.cc/reference/en/language/functions/communication/serial/println/

The Processing trim() removes the '\r' and '\n'.

You can terminate the data sent with the Arduino with any character you want as long as it is not a character that is in your data.
Remember to check for this character in Processing in readStringUntil().

Take a good look at a working example and then scrutinize your code.

:)

If you can be more specified ,where I should edit the code and how , I am a beginner in this kind of stuff :))

You will learn by doing it yourself.

In the end it will be worth the effort.

Please provide a link to the source of the Arduino code out of respect to the original authors and content.

:)

I made the Processing code , but I have troubles :))

Hi @fdinumario, Looking at your original code I want to suggest something that will make shrink the code and make it easier to adjust the comms scheme.

Where you have items like this repeated

  Serial.print("rainin=");
  Serial.print(rainin, 2);
  Serial.println();

Simplify by writing a new function:

void prntValue(char label[], float value, int  nofDec)
{
   Serial.print(label);
   Serial.print(value, nofDec);
   Serial.println();
}

Now your list of sending becomes:

  prntValue("rainin="     , rainin     , 2);
  prntValue("dailyrainin=", dailyrainin, 2);

Your original code is meant to print labels and values on the serial monitor, but if you want to convert to @glv suggestion you have to alter every printing line. With the function you only have to make the changes once. You could use a function in your Processing to replace the pairs of lines

text("Rainfall:",50,350);
text(rainin,250,350);

This pays off here as well, because I think you need textAlign(LEFT) for the label and RIGHT for the value.

Serial: You need a simple definition for what the message is, and in your original the labels make things complicated. Suggest you try glv’s example and my example. It will help you to see serial comms simply working.

Please try all that and describe the remaining troubles.

Can you write a full example of new function with only one parameter (label) because I still got an error with char label

I’ll do as you ask:

void prntL(char label[])
{Serial.print(label);}

void setup(){}

void loop() 
{prntL("rainin=");}

but I’m surprised you get an error because I compiled what I posted, and it’s very similar to what I have in use. I’ve compiled that one as well. Would you like to post the error, just the few lines from the console where it starts to go wrong? Maybe some of the code?

I still got an error
I will attach the new code


#include <Wire.h> 
#include "SparkFunMPL3115A2.h" 
#include "SparkFun_Si7021_Breakout_Library.h" 

MPL3115A2 myPressure; 
Weather myHumidity;



const byte WSPEED = 3;
const byte RAIN = 2;
const byte STAT1 = 7;
const byte STAT2 = 8;

const byte REFERENCE_3V3 = A3;
const byte LIGHT = A1;
const byte BATT = A2;
const byte WDIR = A0;

long lastSecond; 
byte seconds; 
byte seconds_2m; 
byte minutes; 
byte minutes_10m; 

long lastWindCheck = 0;
volatile long lastWindIRQ = 0;
volatile byte windClicks = 0;


byte windspdavg[120]; 

#define WIND_DIR_AVG_SIZE 120
int winddiravg[WIND_DIR_AVG_SIZE]; 
float windgust_10m[10]; 
int windgustdirection_10m[10]; 
volatile float rainHour[60]; 


int winddir = 0; 
float windspeedmph = 0; 
float windgustmph = 0; 
int windgustdir = 0; 
float windspdmph_avg2m = 0; 
int winddir_avg2m = 0; 
float windgustmph_10m = 0; 
int windgustdir_10m = 0; 
float humidity = 0; 
float temperature = 0; 
float rainin = 0; 
volatile float dailyrainin = 0; 
float pressure = 0;
float batt_lvl = 11.8; 
float light_lvl = 455; 


volatile unsigned long raintime, rainlast, raininterval, rain;


void rainIRQ()

{
  raintime = millis(); 
  raininterval = raintime - rainlast; 

  if (raininterval > 10) 
  {
    dailyrainin += 0.011; 
    rainHour[minutes] += 0.011; 

    rainlast = raintime; 
  }
}

void wspeedIRQ()

{
  if (millis() - lastWindIRQ > 10) 
  {
    lastWindIRQ = millis(); 
    windClicks++; 
  }
}


void setup()
{
  Serial.begin(9600);
  Serial.println("Weather Shield Example");

  pinMode(STAT1, OUTPUT); 
  pinMode(STAT2, OUTPUT); 

  pinMode(WSPEED, INPUT_PULLUP); 
  pinMode(RAIN, INPUT_PULLUP); 

  pinMode(REFERENCE_3V3, INPUT);
  pinMode(LIGHT, INPUT);

 
  myPressure.begin(); 
  myPressure.setModeBarometer(); 
  myPressure.setOversampleRate(7); 
  myPressure.enableEventFlags(); 

  
  myHumidity.begin();

  seconds = 0;
  lastSecond = millis();

  // attach external interrupt pins to IRQ functions
  attachInterrupt(0, rainIRQ, FALLING);
  attachInterrupt(1, wspeedIRQ, FALLING);

  // turn on interrupts
  interrupts();

  Serial.println("Weather Shield online!");

}

void loop()
{
  
  if (millis() - lastSecond >= 1000)
  {
    digitalWrite(STAT1, HIGH); //Blink stat LED

    lastSecond += 1000;

   
    if (++seconds_2m > 119) seconds_2m = 0;

   
    float currentSpeed = get_wind_speed();
    windspeedmph = currentSpeed;
    
    int currentDirection = get_wind_direction();
    windspdavg[seconds_2m] = (int)currentSpeed;
    winddiravg[seconds_2m] = currentDirection;
    

    
    if (currentSpeed > windgust_10m[minutes_10m])
    {
      windgust_10m[minutes_10m] = currentSpeed;
      windgustdirection_10m[minutes_10m] = currentDirection;
    }

    
    if (currentSpeed > windgustmph)
    {
      windgustmph = currentSpeed;
      windgustdir = currentDirection;
    }

    if (++seconds > 59)
    {
      seconds = 0;

      if (++minutes > 59) minutes = 0;
      if (++minutes_10m > 9) minutes_10m = 0;

      rainHour[minutes] = 0; 
      windgust_10m[minutes_10m] = 0; 
    }

  
    printWeather();

    digitalWrite(STAT1, LOW); //Turn off stat LED
  }

  delay(100);
}


void calcWeather()
{
  
  winddir = get_wind_direction();

 
  float temp = 0;
  for (int i = 0 ; i < 120 ; i++)
    temp += windspdavg[i];
  temp /= 120.0;
  windspdmph_avg2m = temp;

  
  long sum = winddiravg[0];
  int D = winddiravg[0];
  for (int i = 1 ; i < WIND_DIR_AVG_SIZE ; i++)
  {
    int delta = winddiravg[i] - D;

    if (delta < -180)
      D += delta + 360;
    else if (delta > 180)
      D += delta - 360;
    else
      D += delta;

    sum += D;
  }
  winddir_avg2m = sum / WIND_DIR_AVG_SIZE;
  if (winddir_avg2m >= 360) winddir_avg2m -= 360;
  if (winddir_avg2m < 0) winddir_avg2m += 360;

 
  windgustmph_10m = 0;
  windgustdir_10m = 0;

  for (int i = 0; i < 10 ; i++)
  {
    if (windgust_10m[i] > windgustmph_10m)
    {
      windgustmph_10m = windgust_10m[i];
      windgustdir_10m = windgustdirection_10m[i];
    }
  }

  
  humidity = myHumidity.getRH();
 

  
  temperature = myPressure.readTemp();
  

  
  rainin = 0;
  for (int i = 0 ; i < 60 ; i++)
    rainin += rainHour[i];

  
  pressure = myPressure.readPressure();


  light_lvl = get_light_level();

  
  batt_lvl = get_battery_level();
}


float get_light_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float lightSensor = analogRead(LIGHT);

  operatingVoltage = 3.3 / operatingVoltage; 

  lightSensor = operatingVoltage * lightSensor;

  return (lightSensor);
}


float get_battery_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float rawVoltage = analogRead(BATT);

  operatingVoltage = 3.30 / operatingVoltage; 

  rawVoltage = operatingVoltage * rawVoltage; 

  rawVoltage *= 4.90; 

  return (rawVoltage);
}


float get_wind_speed()
{
  float deltaTime = millis() - lastWindCheck; 

  deltaTime /= 1000.0; 

  float windSpeed = (float)windClicks / deltaTime; 

  windClicks = 0; 
  lastWindCheck = millis();

  windSpeed *= 1.492; 

  

  return (windSpeed);
}


int get_wind_direction()
{
  unsigned int adc;

  adc = analogRead(WDIR); 

 
  if (adc < 380) return (113);
  if (adc < 393) return (68);
  if (adc < 414) return (90);
  if (adc < 456) return (158);
  if (adc < 508) return (135);
  if (adc < 551) return (203);
  if (adc < 615) return (180);
  if (adc < 680) return (23);
  if (adc < 746) return (45);
  if (adc < 801) return (248);
  if (adc < 833) return (225);
  if (adc < 878) return (338);
  if (adc < 913) return (0);
  if (adc < 940) return (293);
  if (adc < 967) return (315);
  if (adc < 990) return (270);
  return (-1); 
}


void printWeather(char label[])
{Serial.print(label);}

void setup(){}



void loop() 
{
printWeather("winddir=" , winddir);
printWeather("windspeedmph=" , windspeedmph , 1);
printWeather("windgustmph=" , windgustmph , 1);
printWeather("windgusdir=" , windgustdir);
printWeather("windspdmph_avg2m=" , winddir_avg2m);
printWeather("winddir_avg2m=" , rainin , 2);
printWeather("windgustmph_10m=" , windgustmph_10m, 1);
printWeather("windgustdir_10m=" , windgustdir_10m);
printWeather("humidity=" , humidity, 1);
printWeather("tempf=" , temperature, 1);
printWeather("rainin=" , rainin , 2);
printWeather("dailyrainin=" , dailyrainin, 2);
printWeather("pressure=" , pressure, 2);
printWeather("batt_lvl=" , batt_lvl, 2);
printWeather("light_lvl=" , light_lvl, 2);

}




the error is "too few arguments to function “void printWeather(char*)” - line 171

Try this:

void printWeather(char label[], float value, int decPlaces = 0)
{
  Serial.print(label);
  Serial.print(value, decPlaces);
  Serial.println();
}

void loop() 
{
  printWeather("winddir=" , winddir);
  printWeather("windspeedmph=" , windspeedmph , 1);
  printWeather("windgustmph=" , windgustmph , 1);
...

I haven’t run it, just compiled, but with the Sparkfun libraries etc. commented out. The function has to have the same number of parameters in the definition and every time you call it. Except when the last (or more) parameters is optional with a default value. There are other things that you need to fix, more than 1 setup, and loop, and a printWeather with no parameters.

now on the same line (171) this error: “printWeather” was not declared in this scope

In post 8 (think) on line 170 you have “printWeather()” with no parameters. Think you didn’t mean to have this here.

so what should I do ? I didn’t get you (also thanks for your support )

Somehow we are not making progress. Please do all of:

  • tell me the exact names of the 2 libraries, so I can include them and compile the exact code you are using.
  • when you post code, simply click the </> and past your code. You are doing something that looks like a quote section. Your last one doesn’t have the ‘copy’ icon at top right. If you mean to refer to code, don’t quote it or repost, just say ‘as in post N’.
  • In general it’s often useful to put aside your main project, create a small Ard sketch and just test the problem until you understand it.
  • what is line 172 for? “printWeather();” - I think you should delete that line.
    What errors remain?
#include <Wire.h> 
#include "SparkFunMPL3115A2.h" 
#include "SparkFun_Si7021_Breakout_Library.h" 

MPL3115A2 myPressure; 
Weather myHumidity;



const byte WSPEED = 3;
const byte RAIN = 2;
const byte STAT1 = 7;
const byte STAT2 = 8;

const byte REFERENCE_3V3 = A3;
const byte LIGHT = A1;
const byte BATT = A2;
const byte WDIR = A0;

long lastSecond; 
byte seconds; 
byte seconds_2m; 
byte minutes; 
byte minutes_10m; 

long lastWindCheck = 0;
volatile long lastWindIRQ = 0;
volatile byte windClicks = 0;


byte windspdavg[120]; 

#define WIND_DIR_AVG_SIZE 120
int winddiravg[WIND_DIR_AVG_SIZE]; 
float windgust_10m[10]; 
int windgustdirection_10m[10]; 
volatile float rainHour[60]; 


int winddir = 0; 
float windspeedmph = 0; 
float windgustmph = 0; 
int windgustdir = 0; 
float windspdmph_avg2m = 0; 
int winddir_avg2m = 0; 
float windgustmph_10m = 0; 
int windgustdir_10m = 0; 
float humidity = 0; 
float temperature = 0; 
float rainin = 0; 
volatile float dailyrainin = 0; 
float pressure = 0;
float batt_lvl = 11.8; 
float light_lvl = 455; 


volatile unsigned long raintime, rainlast, raininterval, rain;


void rainIRQ()

{
  raintime = millis(); 
  raininterval = raintime - rainlast; 

  if (raininterval > 10) 
  {
    dailyrainin += 0.011; 
    rainHour[minutes] += 0.011; 

    rainlast = raintime; 
  }
}

void wspeedIRQ()

{
  if (millis() - lastWindIRQ > 10) 
  {
    lastWindIRQ = millis(); 
    windClicks++; 
  }
}


void setup()
{
  Serial.begin(9600);
  Serial.println("Weather Shield Example");

  pinMode(STAT1, OUTPUT); 
  pinMode(STAT2, OUTPUT); 

  pinMode(WSPEED, INPUT_PULLUP); 
  pinMode(RAIN, INPUT_PULLUP); 

  pinMode(REFERENCE_3V3, INPUT);
  pinMode(LIGHT, INPUT);

 
  myPressure.begin(); 
  myPressure.setModeBarometer(); 
  myPressure.setOversampleRate(7); 
  myPressure.enableEventFlags(); 

  
  myHumidity.begin();

  seconds = 0;
  lastSecond = millis();

  // attach external interrupt pins to IRQ functions
  attachInterrupt(0, rainIRQ, FALLING);
  attachInterrupt(1, wspeedIRQ, FALLING);

  // turn on interrupts
  interrupts();

  Serial.println("Weather Shield online!");

}

void loop()
{
  
  if (millis() - lastSecond >= 1000)
  {
    digitalWrite(STAT1, HIGH); //Blink stat LED

    lastSecond += 1000;

   
    if (++seconds_2m > 119) seconds_2m = 0;

   
    float currentSpeed = get_wind_speed();
    windspeedmph = currentSpeed;
    
    int currentDirection = get_wind_direction();
    windspdavg[seconds_2m] = (int)currentSpeed;
    winddiravg[seconds_2m] = currentDirection;
    

    
    if (currentSpeed > windgust_10m[minutes_10m])
    {
      windgust_10m[minutes_10m] = currentSpeed;
      windgustdirection_10m[minutes_10m] = currentDirection;
    }

    
    if (currentSpeed > windgustmph)
    {
      windgustmph = currentSpeed;
      windgustdir = currentDirection;
    }

    if (++seconds > 59)
    {
      seconds = 0;

      if (++minutes > 59) minutes = 0;
      if (++minutes_10m > 9) minutes_10m = 0;

      rainHour[minutes] = 0; 
      windgust_10m[minutes_10m] = 0; 
    }

  
    

    digitalWrite(STAT1, LOW); //Turn off stat LED
  }

  delay(100);
}


void calcWeather()
{
  
  winddir = get_wind_direction();

 
  float temp = 0;
  for (int i = 0 ; i < 120 ; i++)
    temp += windspdavg[i];
  temp /= 120.0;
  windspdmph_avg2m = temp;

  
  long sum = winddiravg[0];
  int D = winddiravg[0];
  for (int i = 1 ; i < WIND_DIR_AVG_SIZE ; i++)
  {
    int delta = winddiravg[i] - D;

    if (delta < -180)
      D += delta + 360;
    else if (delta > 180)
      D += delta - 360;
    else
      D += delta;

    sum += D;
  }
  winddir_avg2m = sum / WIND_DIR_AVG_SIZE;
  if (winddir_avg2m >= 360) winddir_avg2m -= 360;
  if (winddir_avg2m < 0) winddir_avg2m += 360;

 
  windgustmph_10m = 0;
  windgustdir_10m = 0;

  for (int i = 0; i < 10 ; i++)
  {
    if (windgust_10m[i] > windgustmph_10m)
    {
      windgustmph_10m = windgust_10m[i];
      windgustdir_10m = windgustdirection_10m[i];
    }
  }

  
  humidity = myHumidity.getRH();
 

  
  temperature = myPressure.readTemp();
  

  
  rainin = 0;
  for (int i = 0 ; i < 60 ; i++)
    rainin += rainHour[i];

  
  pressure = myPressure.readPressure();


  light_lvl = get_light_level();

  
  batt_lvl = get_battery_level();
}


float get_light_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float lightSensor = analogRead(LIGHT);

  operatingVoltage = 3.3 / operatingVoltage; 

  lightSensor = operatingVoltage * lightSensor;

  return (lightSensor);
}


float get_battery_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float rawVoltage = analogRead(BATT);

  operatingVoltage = 3.30 / operatingVoltage; 

  rawVoltage = operatingVoltage * rawVoltage; 

  rawVoltage *= 4.90; 

  return (rawVoltage);
}


float get_wind_speed()
{
  float deltaTime = millis() - lastWindCheck; 

  deltaTime /= 1000.0; 

  float windSpeed = (float)windClicks / deltaTime; 

  windClicks = 0; 
  lastWindCheck = millis();

  windSpeed *= 1.492; 

  

  return (windSpeed);
}


int get_wind_direction()
{
  unsigned int adc;

  adc = analogRead(WDIR); 

 
  if (adc < 380) return (113);
  if (adc < 393) return (68);
  if (adc < 414) return (90);
  if (adc < 456) return (158);
  if (adc < 508) return (135);
  if (adc < 551) return (203);
  if (adc < 615) return (180);
  if (adc < 680) return (23);
  if (adc < 746) return (45);
  if (adc < 801) return (248);
  if (adc < 833) return (225);
  if (adc < 878) return (338);
  if (adc < 913) return (0);
  if (adc < 940) return (293);
  if (adc < 967) return (315);
  if (adc < 990) return (270);
  return (-1); 
}




void printWeather(char label[], float value, int decPlaces = 0)
{
  Serial.print(label);
  Serial.print(value, decPlaces);
  Serial.println();
}

void loop() 
{
printWeather("winddir=" , winddir);
printWeather("windspeedmph=" , windspeedmph , 1);
printWeather("windgustmph=" , windgustmph , 1);
printWeather("windgustmph=" , windgustmph , 1);
printWeather("windgusdir=" , windgustdir);
printWeather("windspdmph_avg2m=" , winddir_avg2m);
printWeather("winddir_avg2m=" , rainin , 2);
printWeather("windgustmph_10m=" , windgustmph_10m, 1);
printWeather("windgustdir_10m=" , windgustdir_10m);
printWeather("humidity=" , humidity, 1);
printWeather("tempf=" , temperature, 1);
printWeather("rainin=" , rainin , 2);
printWeather("dailyrainin=" , dailyrainin, 2);
printWeather("pressure=" , pressure, 2);
printWeather("batt_lvl=" , batt_lvl, 2);
printWeather("light_lvl=" , light_lvl, 2);


}




this is the new code without printWeather(); on line 170 an the new error when I compile is : redefinition of ‘void loop()’ line 333

this are the libraries

  • you have “void loop()” on lines 123 and 333.
  • do you have line numbers switched on? see File, Preferences.
  • I don’t like the find function in the Ard IDE, I find it much easier in Notepad++. You can edit in both at once as long as you save before editing in the other.
  • for the libraries, I meant tell me what to search for in Sketch, Manage Libraries.

After I eliminated void loop() on line 123 ,the code doesn’t have any error but when I read data from serial port all the values are 0

No error, good, please post latest code.

#include <Wire.h> 
#include "SparkFunMPL3115A2.h" 
#include "SparkFun_Si7021_Breakout_Library.h" 

MPL3115A2 myPressure; 
Weather myHumidity;



const byte WSPEED = 3;
const byte RAIN = 2;
const byte STAT1 = 7;
const byte STAT2 = 8;

const byte REFERENCE_3V3 = A3;
const byte LIGHT = A1;
const byte BATT = A2;
const byte WDIR = A0;

long lastSecond; 
byte seconds; 
byte seconds_2m; 
byte minutes; 
byte minutes_10m; 

long lastWindCheck = 0;
volatile long lastWindIRQ = 0;
volatile byte windClicks = 0;


byte windspdavg[120]; 

#define WIND_DIR_AVG_SIZE 120
int winddiravg[WIND_DIR_AVG_SIZE]; 
float windgust_10m[10]; 
int windgustdirection_10m[10]; 
volatile float rainHour[60]; 


int winddir = 0; 
float windspeedmph = 0; 
float windgustmph = 0; 
int windgustdir = 0; 
float windspdmph_avg2m = 0; 
int winddir_avg2m = 0; 
float windgustmph_10m = 0; 
int windgustdir_10m = 0; 
float humidity = 0; 
float temperature = 0; 
float rainin = 0; 
volatile float dailyrainin = 0; 
float pressure = 0;
float batt_lvl = 11.8; 
float light_lvl = 455; 


volatile unsigned long raintime, rainlast, raininterval, rain;


void rainIRQ()

{
  raintime = millis(); 
  raininterval = raintime - rainlast; 

  if (raininterval > 10) 
  {
    dailyrainin += 0.011; 
    rainHour[minutes] += 0.011; 

    rainlast = raintime; 
  }
}

void wspeedIRQ()

{
  if (millis() - lastWindIRQ > 10) 
  {
    lastWindIRQ = millis(); 
    windClicks++; 
  }
}


void setup()
{
  Serial.begin(9600);
  Serial.println("Weather Shield Example");

  pinMode(STAT1, OUTPUT); 
  pinMode(STAT2, OUTPUT); 

  pinMode(WSPEED, INPUT_PULLUP); 
  pinMode(RAIN, INPUT_PULLUP); 

  pinMode(REFERENCE_3V3, INPUT);
  pinMode(LIGHT, INPUT);

 
  myPressure.begin(); 
  myPressure.setModeBarometer(); 
  myPressure.setOversampleRate(7); 
  myPressure.enableEventFlags(); 

  
  myHumidity.begin();

  seconds = 0;
  lastSecond = millis();

  // attach external interrupt pins to IRQ functions
  attachInterrupt(0, rainIRQ, FALLING);
  attachInterrupt(1, wspeedIRQ, FALLING);

  // turn on interrupts
  interrupts();

  Serial.println("Weather Shield online!");

}


void calcWeather()
{
  
  winddir = get_wind_direction();

 
  float temp = 0;
  for (int i = 0 ; i < 120 ; i++)
    temp += windspdavg[i];
  temp /= 120.0;
  windspdmph_avg2m = temp;

  
  long sum = winddiravg[0];
  int D = winddiravg[0];
  for (int i = 1 ; i < WIND_DIR_AVG_SIZE ; i++)
  {
    int delta = winddiravg[i] - D;

    if (delta < -180)
      D += delta + 360;
    else if (delta > 180)
      D += delta - 360;
    else
      D += delta;

    sum += D;
  }
  winddir_avg2m = sum / WIND_DIR_AVG_SIZE;
  if (winddir_avg2m >= 360) winddir_avg2m -= 360;
  if (winddir_avg2m < 0) winddir_avg2m += 360;

 
  windgustmph_10m = 0;
  windgustdir_10m = 0;

  for (int i = 0; i < 10 ; i++)
  {
    if (windgust_10m[i] > windgustmph_10m)
    {
      windgustmph_10m = windgust_10m[i];
      windgustdir_10m = windgustdirection_10m[i];
    }
  }

  
  humidity = myHumidity.getRH();
 

  
  temperature = myPressure.readTemp();
  

  
  rainin = 0;
  for (int i = 0 ; i < 60 ; i++)
    rainin += rainHour[i];

  
  pressure = myPressure.readPressure();


  light_lvl = get_light_level();

  
  batt_lvl = get_battery_level();
}


float get_light_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float lightSensor = analogRead(LIGHT);

  operatingVoltage = 3.3 / operatingVoltage; 

  lightSensor = operatingVoltage * lightSensor;

  return (lightSensor);
}


float get_battery_level()
{
  float operatingVoltage = analogRead(REFERENCE_3V3);

  float rawVoltage = analogRead(BATT);

  operatingVoltage = 3.30 / operatingVoltage; 

  rawVoltage = operatingVoltage * rawVoltage; 

  rawVoltage *= 4.90; 

  return (rawVoltage);
}


float get_wind_speed()
{
  float deltaTime = millis() - lastWindCheck; 

  deltaTime /= 1000.0; 

  float windSpeed = (float)windClicks / deltaTime; 

  windClicks = 0; 
  lastWindCheck = millis();

  windSpeed *= 1.492; 

  

  return (windSpeed);
}


int get_wind_direction()
{
  unsigned int adc;

  adc = analogRead(WDIR); 

 
  if (adc < 380) return (113);
  if (adc < 393) return (68);
  if (adc < 414) return (90);
  if (adc < 456) return (158);
  if (adc < 508) return (135);
  if (adc < 551) return (203);
  if (adc < 615) return (180);
  if (adc < 680) return (23);
  if (adc < 746) return (45);
  if (adc < 801) return (248);
  if (adc < 833) return (225);
  if (adc < 878) return (338);
  if (adc < 913) return (0);
  if (adc < 940) return (293);
  if (adc < 967) return (315);
  if (adc < 990) return (270);
  return (-1); 
}




void printWeather(char label[], float value, int decPlaces = 0)
{
  Serial.print(label);
  Serial.print(value, decPlaces);
  Serial.println();
}

void loop() 
{
printWeather("winddir=" , winddir);
printWeather("windspeedmph=" , windspeedmph , 1);
printWeather("windgustmph=" , windgustmph , 1);
printWeather("windgustmph=" , windgustmph , 1);
printWeather("windgusdir=" , windgustdir);
printWeather("windspdmph_avg2m=" , winddir_avg2m);
printWeather("winddir_avg2m=" , rainin , 2);
printWeather("windgustmph_10m=" , windgustmph_10m, 1);
printWeather("windgustdir_10m=" , windgustdir_10m);
printWeather("humidity=" , humidity, 1);
printWeather("tempf=" , temperature, 1);
printWeather("rainin=" , rainin , 2);
printWeather("dailyrainin=" , dailyrainin, 2);
printWeather("pressure=" , pressure, 2);
printWeather("batt_lvl=" , batt_lvl, 2);
printWeather("light_lvl=" , light_lvl, 2);


}