Hello! I am working on a sun-tracking solar panel. I’ve gotten the sun tracking portion to work, but I’m having trouble getting data to appear in the app. I’m connecting to the Arduino Uno via bluetooth. I have been trying to use the terminal to ask the user if it is sunny or cloudy out, and based on their answer, store the light flux value to the corresponding data set (to be displayed on the graph). I have been trying to do this with digital pins, but I don’t think I know enough about coding to recognize what I am doing incorrectly. I would appreciate any help/suggestions! Below is my current code:
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Auth Token : e2bc64ad889b46b091f58415da3b050b";
SoftwareSerial SerialBLE(10, 11); // RX, TX
#include <Servo.h> //includes servo
#include <stdlib.h> //includes standard library
Servo myservo; // create servo object to control a servo
int sensorOne=2; //digital pin 2 has connected to the resistor of first sensor
int inputOne=0; //variable to store the values from the analog pin 0
int servoAngle=0; // variable to store the servo position, initial angle set to zero
WidgetTerminal terminal(V1);
BLYNK_WRITE(V1)
{
if (String("sunny") == param.asStr()); {
terminal.println("It is currently sunny.");
Blynk.virtualWrite(V2, analogRead(inputOne)); //sends data to sunny data set (virtual pin V2)
}
if (String("cloudy") == param.asStr()) {
Blynk.virtualWrite(V3, analogRead(inputOne)); //sends data to cloudy data set (virtual pin v3)
terminal.println("It is currently cloudy.");
}
terminal.flush(); //Ensure everything is sent.
}
void setup()
{
Serial.begin(9600);
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
//Serial.println("Waiting for connections...");
pinMode(sensorOne,OUTPUT); //initialize the digital pin as output
digitalWrite(sensorOne,HIGH); //voltage set to corresponding value of 5V
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(servoAngle); // tell servo to go to position in variable 'servoAngle'
terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
terminal.println(F("-------------"));
terminal.println(F("Is it sunny or cloudy today?"));
terminal.flush();
}
void loop()
{
Blynk.run();
float flux[179];
for (servoAngle=0; servoAngle<180; servoAngle++) {
flux[servoAngle]= analogRead(inputOne); //creates an array of values read from the LDR
myservo.write(servoAngle); //moves servo from 0 to 180 degrees
Serial.println(flux[servoAngle]); //writes values in flux array to graph
Serial.println("");
}
int servoAngle2 = findMax(flux); //tells it to find max loctaion, see max function below
myservo.write(servoAngle2); //tells servo to go to angle at which voltage is max, found in previous line
delay(9000000); //delays for 15 min before checking again
}
int findMax(float* flux){ //function to find the location at which the voltage is maxiumum
int maximum = 0;
for (int c = 0; c < 180; c++)
{
if (flux[c] > flux[maximum])
{
maximum = c;
}
}
return maximum;
}