#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
#define LDR_ANALOG_PIN A0 // Leitura do Photoresistor - A0
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Ajustar o número de passos por rotação do seu motor
//---------------- Stepper Motor angles ------------------------
const float theta0 = 136.0; // ~61.2º // Each step = 1.8 degrees.
const float theta1 = -68.0; // ~30.6º // Adds clockwise angle.
//----------------- Stepper Motor pins -----------------------
#define directionPin 12 // DIR
#define stepPin 13 // STEP
// initialize the stepper library on pins 12 e 13:
Stepper myStepper(theta0, stepPin, directionPin); // Starting at azimuth
// Essas constantes devem corresponder aos atributos "gama" e "rl10" do fotoresistor
const float GAMMA = 0.7;
const float RL10 = 50;
const int t_wait = 2000; // Tempo de espera (ms) entre giro do motor de passo.
const int ledCount = 10; // Set number of LEDs in the bar graph
// Array of pins for LEDs in the bar graph
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
};
float lux=0;
float analogLux=0; // Valor lido da entrada analógica LDR_ANALOG_PIN A0
// Map the result to a range from lux to the number of LEDs
int ledLevel = map(lux, 0, 10000, 0, ledCount);
void setup() {
// initialize the serial port:
Serial.begin(9600);
analogLux = analogRead(LDR_ANALOG_PIN); // Variável global
// Loop over the LEDs pins and set all to output
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
// set the speed at 30 rpm:
myStepper.setSpeed(30);
// Display LCD
pinMode(LDR_ANALOG_PIN, INPUT);
lcd.init();
lcd.backlight();
// -------------------------------------------------------------------
} // end setup
float convert() {
float voltage;
float resistance;
float lux;
// Converte o valor analógico em valor lux:
analogLux = analogRead(LDR_ANALOG_PIN); // Variável global
voltage = analogLux / 1024. * 5;
resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
return lux;
} // end convert
void leds() {
// Map the result to a range from lux to the number of LEDs
ledLevel = map(lux, 0, 10000, 0, ledCount);
// Loop over the LEDs pins and set element
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// If the array element's index is less than ledLevel
if (thisLed < ledLevel) {
// Turn the pin for this element on
digitalWrite(ledPins[thisLed], HIGH);
} else {
// Turn off all pins higher than the ledLevel
digitalWrite(ledPins[thisLed], LOW);
}
}
} // end leds
// Show messages of values and luminosity
void displayLCD() {
// Mostra o valor luminosidade no display do LCD
lcd.setCursor(0, 0);
lcd.print("Sunlight: ");
lcd.print(analogLux);
lcd.setCursor(2, 1); // Imprime a mensagem sobre a intensidade de luz
if (analogLux <= 39)
lcd.print("Excelent! ");
else if (analogLux <= 54 and analogLux > 39)
lcd.print("Good ");
else if (analogLux <= 86 and analogLux > 54)
lcd.print("Moderate ");
else if (analogLux <= 169 and analogLux > 86)
lcd.print("Weak ");
else
lcd.print("Inappropriate ");
} // end displayLCD
// Printscreen angle values
void printscreen(float rotate) {
Serial.print("Luminosidade: ");
Serial.println(analogLux);
myStepper.step(rotate); // Gira conforme o valor de theta0 e theta1
} // end printscreen
void loop() {
// -------------------------------------------------------------------
// Converte o valor analógico em valor lux:
lux = convert();
leds(); // Update lights leds
displayLCD(); // Update data display
// Posição inicial de Leste --> Oeste (60º)
printscreen(theta0); // Oeste 60º
Serial.println("At 6 am - 8 am");
delay(t_wait);
//--------------------------------------------------------------------
// Converte o valor analógico em valor lux:
lux = convert();
leds(); // Update lights leds
displayLCD(); // Update data display
printscreen(theta1); // Oeste 30º
Serial.println("At 8 am - 10 am");
delay(t_wait);
//--------------------------------------------------------------------
// Converte o valor analógico em valor lux:
lux = convert();
leds(); // Update lights leds
displayLCD(); // Update data display
printscreen(theta1); // Azimute 90º
Serial.println("At 10 am - 1 pm");
delay(t_wait);
//--------------------------------------------------------------------
// Converte o valor analógico em valor lux:
lux = convert();
leds(); // Update lights leds
displayLCD(); // Update data display
printscreen(theta1); // Leste 30º
Serial.println("At 1 pm - 3 pm");
delay(t_wait);
//--------------------------------------------------------------------
// Converte o valor analógico em valor lux:
lux = convert();
leds(); // Update lights leds
displayLCD(); // Update data display
printscreen(theta1); // Leste 60º
Serial.println("At 3 pm - 5 pm");
delay(t_wait);
//--------------------------------------------------------------------
lux = convert();
leds(); // Update lights leds
displayLCD(); // Update data display
printscreen(theta0); // retorna a posição do Azimuth
Serial.println("Returning the panel to the east...");
delay(t_wait);
} // end loop