Arduino Code Guidance -

Hello,

I am kind of new in Arduino projects. I did so far only some small applications. I have a project with 6 indicators in an html page and I get the inputs from the Arduino. So I have the code in the atachement.

I need some guidance how can I integrate the next code into my application. I tried several ways but I am not able to make it work. The code below will go to this line → // analog_val = Wind Speed

I would really appreciate a little help!

Thank you.

// diameter of anemometer
float diameter = 2.75; //inches from center pin to middle of cup
float mph;

// read RPM
int half_revolutions = 0;
int rpm = 0;
unsigned long lastmillis = 0;

void setup(){
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
attachInterrupt(0, rpm_fan, FALLING);
}

void loop(){
if (millis() - lastmillis == 1000){ //Update every one second, this will be equal to reading frequency (Hz).
detachInterrupt(0);//Disable interrupt when calculating
rpm = half_revolutions * 30; // Convert frequency to RPM, note: 60 works for one interruption per full rotation. For two interrupts per full rotation use half_revolutions * 30.
half_revolutions = 0; // Restart the RPM counter
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
mph = diameter / 12 * 3.14 * rpm * 60 / 5280;
mph = mph * 3.5; // calibration factor for anemometer accuracy, adjust as necessary
Serial.println(mph);
}
}
// this code will be executed every time the interrupt 0 (pin2) gets low.
void rpm_fan(){
half_revolutions++;
}

dht22_bmp180_wind_rain.ino (6.41 KB)

Phia:
Hello,

I am kind of new in Arduino projects. I did so far only some small applications. I have a project with 6 indicators in an html page and I get the inputs from the Arduino. So I have the code in the atachement.

I need some guidance how can I integrate the next code into my application. I tried several ways but I am not able to make it work. The code below will go to this line -> // analog_val = Wind Speed

I would really appreciate a little help!

Thank you.

// diameter of anemometer
float diameter = 2.75; //inches from center pin to middle of cup
float mph;

// read RPM
int half_revolutions = 0;
int rpm = 0;
unsigned long lastmillis = 0;

void setup(){
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
attachInterrupt(0, rpm_fan, FALLING);
}

void loop(){
if (millis() - lastmillis == 1000){ //Update every one second, this will be equal to reading frequency (Hz).
detachInterrupt(0);//Disable interrupt when calculating
rpm = half_revolutions * 30; // Convert frequency to RPM, note: 60 works for one interruption per full rotation. For two interrupts per full rotation use half_revolutions * 30.
half_revolutions = 0; // Restart the RPM counter
lastmillis = millis(); // Update lastmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
mph = diameter / 12 * 3.14 * rpm * 60 / 5280;
mph = mph * 3.5; // calibration factor for anemometer accuracy, adjust as necessary
Serial.println(mph);
}
}
// this code will be executed every time the interrupt 0 (pin2) gets low.
void rpm_fan(){
half_revolutions++;
}

My anemometer must be similar to yours. Mine is a commercial unit similar to a unit priced at around $300. Got mine at a swap meet for $10. It rotates a bar magnet over a reed switch and gets two closures per rotation like yours. My unit is so closely resembles a unit on the company wed page that I used their wind speed formula. Their unit does not begin to turn until a wind of 1 mph is blowing, so any count above 0 must begin with 1 mph. Then once turning, the unit needs 0.8+ wind to continue to turn. I forget the exact decimal value. You might need to adjust your calculation to allow for a minimum wind speed.

I do just like you, in counting interrupts for 1 second and then compute the speed and save it in an array of the last 15 seconds. Also, if the new speed it greater than an old maximum speed, I save the new value.

I display on an LCD, the average of the last 15 seconds, and the maximum wind speed. The wind speed varies greatly here every second and is not very interesting on a one line display!

Suggestions for your program would be to not do any calculations at all after you disable interrupts. Just save the count in a work int then reset the count, and enable the interrupts.

Can't help with the rest of your posting.

Paul

I just recalled what I saw in your program:

if (millis() - lastmillis == 1000){ //Update every one second, this will be equal to reading frequency (Hz).

Change the “==” to “=>” so you can still compute properly if you happen to miss the exact millisecond.

Paul

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Before posting select TOOL, AUTO FORMAT in the IDE, this will ident and space your code to make it easier to read.

Thanks.. Tom... :slight_smile:

Hi,

Thanks Tom for the info. Sorry for not reading first "how to use this forum".

Paul, I don't use a LCD to display the values. I have the kit from SparkFun. It has a wind vane, anemometer and rain gauge.
My application works with a web page on a micro SD card. So all my values are displayed on the web page. Temperature, humidity, atmospherical pressure and wid direction work fine. But my issues are only with the wind speed and rain gauge. Both have similar functionality, so if I make one to work, I will make also the other one.

I tried to integrate the code for the wind speed into my code and is not working with the web page. I can see the values on a LCD display and in Serial monitor.

Best regards.

Hi,

I tried to integrate the code for the wind speed into my code and is not working with the web page. I can see the values on a LCD display and in Serial monitor.

So we need to see your code that doesn't work, remember code tags.

Tom... :slight_smile:

Hi,

So, below is the code. I used the “code tags” this time :slight_smile: :slight_smile:

When I try to compile it, I get this error : “‘analog_val’ was not declared in this scope” I have to declare “mph” in my XML_response function?

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

#define REQ_BUF_SZ   50

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 20);
EthernetServer server(80);

File webFile;

char HTTP_req[REQ_BUF_SZ] = {0};
char req_index = 0;

float diameter = 2.75;
float mph;

int half_revolutions = 0;
int rpm = 0;
unsigned long lastmillis = 0;

void setup()
{
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

  pinMode(2, INPUT_PULLUP);
  attachInterrupt(0, rpm_fan, FALLING);

  Serial.begin(9600);

  Serial.println("Initializing SD card...");
  
  if (!SD.begin(4))
  {
    Serial.println("ERROR - SD card initialization failed!");
    return;
  }
  Serial.println("SUCCESS - SD card initialized.");
  
  if (!SD.exists("index.htm"))
  {
    Serial.println("ERROR - Can't find index.htm file!");
    return;
  }
  Serial.println("SUCCESS - Found index.htm file.");

  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  EthernetClient client = server.available();

  if (millis() - lastmillis == 1000)
  {
    detachInterrupt(0);
    rpm = half_revolutions * 30;
    half_revolutions = 0;
    lastmillis = millis();
    
    attachInterrupt(0, rpm_fan, FALLING);
    mph = diameter / 12 * 3.14 * rpm * 60 / 5280;
    mph = mph * 3.5;
  }

  if (client)
  {
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        if (req_index < (REQ_BUF_SZ - 1))
        {
          HTTP_req[req_index] = c;
          req_index++;
        }

        if (c == '\n' && currentLineIsBlank)
        {
          client.println("HTTP/1.1 200 OK");
          if (StrContains(HTTP_req, "ajax_inputs"))
          {
            client.println("Content-Type: text/xml");
            client.println("Connection: keep-alive");
            client.println();
            XML_response(client);
          }
          else
          {
            client.println("Content-Type: text/html");
            client.println("Connection: keep-alive");
            client.println();
            webFile = SD.open("index.htm");
            
            if (webFile)
            {
              while (webFile.available())
              {
                client.write(webFile.read());
              }
              webFile.close();
            }
          }

          Serial.print(HTTP_req);
          req_index = 0;
          StrClear(HTTP_req, REQ_BUF_SZ);
          break;
        }
        if (c == '\n')
        {
          currentLineIsBlank = true;
        }
        else if (c != '\r')
        {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
  }
}

void XML_response(EthernetClient cl)
{

  cl.print("<inputs>");

  analog_val = mph;
  cl.print("<analog>");
  cl.print(analog_val);
  cl.print("</analog>");

  sensorValue = analogRead(A0); // for wind direction
  analog_val = map(sensorValue, 0, 1023, 0, 255);
  cl.print("<analog>");
  cl.print(analog_val);
  cl.print("</analog>");

  cl.print("</inputs>");
}

void StrClear(char *str, char length)
{
  for (int i = 0; i < length; i++) {
    str[i] = 0;
  }
}

char StrContains(char *str, char *sfind)
{
  char found = 0;
  char index = 0;
  char len;

  len = strlen(str);

  if (strlen(sfind) > len) {
    return 0;
  }
  while (index < len) {
    if (str[index] == sfind[found]) {
      found++;
      if (strlen(sfind) == found) {
        return 1;
      }
    }
    else {
      found = 0;
    }
    index++;
  }
  return 0;
}

void rpm_fan()
{
  half_revolutions++;
}

Hi,
You have a variable called analog_val, but you have not declared as to what type of variable it is, in this case an int.

Tom.... :slight_smile:

Hi,

Accidentally I deleted my declarations for "analog_val" and "sensorValue" in my XML_response function. I declared them as int.

But not I get a lot of compiling errors:

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: In function 'void loop()':

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 79: 29: warning: array subscript has type 'char' [-Wchar - subscripts]

HTTP_req[req_index] = c;

^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 86: 50: warning: deprecated conversion from string constant to 'char*' [-Wwrite - strings]

if (StrContains(HTTP_req, "ajax_inputs"))

  ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: In function 'char StrContains(char*, char*)':

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 167: 21: warning: comparison between signed and unsigned integer expressions [-Wsign - compare]

  if (strlen(sfind) > len) {

    ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 171: 18: warning: array subscript has type 'char' [-Wchar - subscripts]

    if (str[index] == sfind[found]) {

      ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 171: 34: warning: array subscript has type 'char' [-Wchar - subscripts]

      if (str[index] == sfind[found]) {

        ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 173: 25: warning: comparison between signed and unsigned integer expressions [-Wsign - compare]

        if (strlen(sfind) == found) {

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: At global scope:

C: \Users\Fia\Desktop\23.12\23.12.ino: 13: 10: error: redefinition of 'byte mac []'

          byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

          ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 7: 6: note: 'byte mac [6]' previously defined here

          byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 14: 13: error: redefinition of 'IPAddress ip'

          IPAddress ip(192, 168, 0, 20); // IP address, may need to change depending on network

          ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 8: 11: note: 'IPAddress ip' previously declared here

          IPAddress ip(192, 168, 0, 20);

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 15: 22: error: redefinition of 'EthernetServer server'

          EthernetServer server(80);  // create a server at port 80

          ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 9: 16: note: 'EthernetServer server' previously declared here

          EthernetServer server(80);

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 16: 6: error: redefinition of 'SDLib::File webFile'

          File webFile;               // the web page file on the SD card

          ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 11: 6: note: 'SDLib::File webFile' previously declared here

          File webFile;

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 17: 25: error: redefinition of 'char HTTP_req [50]'

          char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string

          ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 13: 6: note: 'char HTTP_req [50]' previously defined here

          char HTTP_req[REQ_BUF_SZ] = {0};

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 18: 6: error: redefinition of 'char req_index'

          char req_index = 0;              // index into HTTP_req buffer

          ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 14: 6: note: 'char req_index' previously defined here

          char req_index = 0;

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: In function 'void setup()':

C: \Users\Fia\Desktop\23.12\23.12.ino: 26: 6: error: redefinition of 'void setup()'

          void setup()

          ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 23: 6: note: 'void setup()' previously defined here

          void setup()

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: In function 'void loop()':

C: \Users\Fia\Desktop\23.12\23.12.ino: 55: 6: error: redefinition of 'void loop()'

          void loop()

          ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 53: 6: note: 'void loop()' previously defined here

          void loop()

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 67: 39: warning: array subscript has type 'char' [-Wchar - subscripts]

          HTTP_req[req_index] = c;          // save HTTP request character

          ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 78: 60: warning: deprecated conversion from string constant to 'char*' [-Wwrite - strings]

          if (StrContains(HTTP_req, "ajax_inputs")) {

            ^

C: \Users\Fia\Desktop\23.12\23.12.ino: In function 'void XML_response(EthernetClient)':

C: \Users\Fia\Desktop\23.12\23.12.ino: 125: 6: error: redefinition of 'void XML_response(EthernetClient)'

            void XML_response(EthernetClient cl)

            ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 130: 6: note: 'void XML_response(EthernetClient)' previously defined here

            void XML_response(EthernetClient cl)

            ^

C: \Users\Fia\Desktop\23.12\23.12.ino: In function 'void StrClear(char*, char)':

C: \Users\Fia\Desktop\23.12\23.12.ino: 168: 6: error: redefinition of 'void StrClear(char*, char)'

            void StrClear(char *str, char length)

            ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 152: 6: note: 'void StrClear(char*, char)' previously defined here

            void StrClear(char *str, char length)

            ^

C: \Users\Fia\Desktop\23.12\23.12.ino: In function 'char StrContains(char*, char*)':

C: \Users\Fia\Desktop\23.12\23.12.ino: 175: 6: error: redefinition of 'char StrContains(char*, char*)'

            char StrContains(char *str, char *sfind)

            ^

C: \Users\Fia\Desktop\23.12\dht22_bmp180_wind_rain.ino: 159: 6: note: 'char StrContains(char*, char*)' previously defined here

            char StrContains(char *str, char *sfind)

            ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 183: 23: warning: comparison between signed and unsigned integer expressions [-Wsign - compare]

            if (strlen(sfind) > len) {

              ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 187: 22: warning: array subscript has type 'char' [-Wchar - subscripts]

              if (str[index] == sfind[found]) {

                ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 187: 38: warning: array subscript has type 'char' [-Wchar - subscripts]

                if (str[index] == sfind[found]) {

                  ^

C: \Users\Fia\Desktop\23.12\23.12.ino: 189: 31: warning: comparison between signed and unsigned integer expressions [-Wsign - compare]

                  if (strlen(sfind) == found) {

                    ^

                    exit status 1
                    Error compiling for board Arduino / Genuino Uno.