Ciao a tutti, mi son fatto una piccola funzione di blink per vedere quando il mio loop inizia solo che non succede assolutamente nulla.
Parto dal presupposto che questa funzione l'ho fatta perche' il delay(int millis) di arduino sembra non contare ed aspettare il tempo giusto. Ho messo delay(100060MIN) ma non aspetta 10 minuti prima di proseguire con il loop, cosi' ho voluto provare a vedere mettendo un blink quando riparte il loop per capire se effettivamente rimane in atesa questi 10 minuti.
La funzione di sleep e' questa:
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void blink(int times)
{
while(times > 0)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(300); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(300);
times--;
}
}
void loop
{
blink(10);
}
Costruita cosi' da sola funziona benissimo appena la importo nel mio programma il LED non si accende!
Premetto di non aver provato ad usare altri pin e led perche' volevo far una prova al volo per vedere se funziona. QUalcuno ha idea del perche' non va? Ho pensato che nel setup c'e' qualcosa di strano che blocca l'uso del pin 13 del LED pero' non saprei.
/*
* Bartolomei Nicolo'
*/
#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <DHT.h> // for the DHT sensor
//#include "LowPower.h"
//define DHT pin
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// Create a file to store the data
File myFile;
//Change to 1 to remove the file from SD and create a new one
const int deleteFile = 0;
//const int cycle = 75;
int count = 0;
void setup()
{
//initializing the DHT sensor
dht.begin();
//initializing Serial monitor
Serial.begin(9600);
// setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//Se abilitato cancella il file
if(deleteFile == 1)
{
SD.remove("logBox.txt");
}
//Check if file exists and create it in case it doesnt
Serial.println("Checking if file exists...");
if (SD.exists("logBox.txt"))
{
Serial.println("File logBox.txt exists.");
myFile = SD.open("logBox.txt", FILE_WRITE);
if(myFile)
{
myFile.println("Temperature ºC, Humidity, Heat Index");
}
else
{
Serial.println("[SD.exists]Error opening file");
}
myFile.close();
}
else
{
Serial.println("File does not exists...");
// open a new file and immediately close it:
Serial.println("Creating logBox.txt...");
myFile = SD.open("logBox.txt", FILE_WRITE);
myFile.close();
Serial.println("File logBox.txt created.");
}
/*
Serial.println("Loop starting in 3...");
delay(1000);
Serial.println("Loop starting in 2...");
delay(1000);
Serial.println("Loop starting in 1...");
delay(3000);
*/
Serial.println("Blink now...");
delay(1000);
pinMode(LED_BUILTIN, OUTPUT);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
/*
void sleepy(int cycle)
{
int sleep = cycle;
while (sleep > 0)
{
sleep--;
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
}
*/
void loggingTemperatureHumidityHeatIndex()
{
Serial.print("Reading...");
delay(5000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
delay(300);
humidity = dht.readHumidity();
delay(1000);
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
delay(300);
temperature = dht.readTemperature();
Serial.println("Finished");
// Check if any reads failed and exit early (to try again).
if (isnan(temperature))
{
Serial.println("Failed to read temperature from DHT sensor!");
return;
}
if (isnan(humidity))
{
Serial.println("Failed to read humidity from DHT sensor!");
return;
}
float heat_index = dht.computeHeatIndex(temperature, humidity, false);
//debugging purposes
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("*C");
Serial.print(" Humidity: ");
Serial.print(humidity);
Serial.println("%");
myFile = SD.open("logBox.txt", FILE_WRITE);
if (myFile)
{
Serial.print("SD file opened and...");
myFile.print(count);
myFile.print(",");
myFile.print(temperature);
myFile.print(",");
myFile.print(humidity);
myFile.print(",");
myFile.print(heat_index);
myFile.println(",");
Serial.println("data wrote");
}
myFile.close();
}
void blink(int times)
{
while(times > 0)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
times--;
}
}
void loop()
{
//loggingTemperatureHumidityHeatIndex();
count++;
blink(10);
//Wait 10 mins then get the data from sensor
//for(int i = 10; i > 0; i++)
//{
//delay(60000);
//}
}