Hi Guys.
I am having trouble understanding what is happening inside my void loop.
This is my first Arduino project, and my code (stitched together from snippets), so please pardon my lack of finesse in the code. I have attached the ui below.
This is a weather station project with a twist.
I am running this code on a Giga with a Giga display. The twist is that when wind direction and velocity are outside a range, the DMX transmission values will cause the receiving DMX lights to shut off. (all of that yet to come).
So my question is, I set several variables early outside the void loop on line 9. And the GUI seems to be displaying the data correctly, after line #49 (inside the void loop). BUT line 40 through 49 (also inside the loop), the exact same variable name "direction" doesn't change with the wind angle as it correctly does later in the code.
It appears that when I null out the variable on line 9 the code is looping back up to line 9 and resetting the variable back to null. So why is the variable "direction" displaying properly on line 49 and not on line 40?
Thank you for helping this newb.
1. #include "Arduino_H7_Video.h"
2. #include "Arduino_GigaDisplayTouch.h"
3. #include "lvgl.h"
4. #include <ui.h>
5. Arduino_H7_Video Display( 800, 480, GigaDisplayShield ); //( 800, 480, GigaDisplayShield );
6. Arduino_GigaDisplayTouch TouchDetector;
7. // These constants won't change. They're used to give names to the pins used:
8. const int analogOutPin = A3; // Analog output pin that the DMX data is written to
9. int DMX_Value = 0; // init
10. int direction= 0;//value read from the wind vane
11. int outputValue = 0; // value output to A3
12. void setup() {
13. Display.begin();
14. TouchDetector.begin();
15. Serial.begin(9600);// prepare Giga to output DMX (windspeed & direction)
16. ui_init();
17. }
18. // this section is for DMX Transmit function
19. // ui_LBL_Transmit_DMX variable used in this section
20. // ui_LBL_Transmit_DMX =(the raw data read)
21. /*
22. Analog input, analog output, serial output
23. Reads an analog input pin, maps the result to a range from 0 to 255 and uses
24. the result to set the pulse width modulation (PWM) of an output pin.
25. Also prints the results to the Serial Monitor.
26. The circuit:
27. - potentiometer connected to analog pin 0.
28. Center pin of the potentiometer goes to the analog pin.
29. side pins of the potentiometer go to +5V and ground
30. - LED connected from digital pin 9 to ground through 220 ohm resistor
31. created 29 Dec. 2008
32. modified 9 Apr 2012
33. by Tom Igoe
34. This example code is in the public domain.
35. https://docs.arduino.cc/built-in-examples/analog/AnalogInOutSerial/
36. */
37. void loop()
38. {
39. lv_timer_handler();
40. DMX_Value = map(outputValue, 0, 359, 0, 255); // remap the analog out value:
41. analogWrite(analogOutPin, DMX_Value);
42. // print the results to the Serial Monitor:
43. Serial.print("DMX = ");
44. Serial.print(DMX_Value);
45. Serial.print("\t Direction = ");
46. Serial.println(direction);
47. // wait 2 milliseconds before the next loop for the analog-to-digital
48. // converter to settle after the last reading:
49. // this section is for weathervane read and display
50. // variable used in this section
51. // Weather_vane =(the raw data read from the sensor)
52. // float voltage = (calculated wind direction corrected for scale)
53. // direction = (the wind direction displayed on the ui)
54. int Weather_vane = analogRead(A0);
55. float voltage = Weather_vane*5/600.0;
56. //correct low end jitter
57. if(voltage < .02){
58. voltage = 0;
59. }
60. int direction = map(Weather_vane, 0, 102.9, 0, 360);
61. lv_img_set_angle(ui_Weather_Vane_Pointer1, direction); // wind direction image screen1
62. lv_img_set_angle(ui_Weather_Vane_Pointer2, direction); // wind direction image screen2
63. lv_label_set_text_fmt(ui_Weather_Vane_Data, "%d", (direction/10)); // wind direction text screen1
64. lv_label_set_text_fmt(ui_Weather_Vane_Data1, "%d", (direction/10)); // wind direction text screen2
65. // windvane section end
66. // this section is for anemometer read and display
67. // variable used in this section
68. // anemometer =(the raw data read from the sensor)
69. // float voltage = (calculated windspeed corrected for scale)
70. // windspeed = (the speed displayed on the ui)
71. int anemometer = analogRead(A1); // write data to pin 1
72. float voltage1 = anemometer*5/102.0; // scale data
73. //correct low end jitter
74. if(voltage1 < .02){
75. voltage1 = 0;
76. }
77. int windspeed = map(anemometer, 0, 636, 0, 50); // remap data
78. // print out the value you read:
79. // the next lines place values into the components both in both panels and both screens
80. lv_bar_set_value(ui_Bar1, windspeed, LV_ANIM_ON); //windspeed bar screen 1
81. lv_bar_set_value(ui_Bar2, windspeed, LV_ANIM_ON); //windspeed bar screen 2
82. lv_label_set_text_fmt(ui_Anenometer_Data, "%d", windspeed); //windspeed screen 1
83. lv_label_set_text_fmt(ui_Anenometer_Data1, "%d", windspeed); //windspeed screen 2
84. delay(1000);
85. }
86. // anemometer section end