129 lines
4.5 KiB
Arduino
129 lines
4.5 KiB
Arduino
|
#include <Wire.h>
|
||
|
#include <Servo.h>
|
||
|
#include "pitches.h"
|
||
|
|
||
|
#define POWER_RED 10
|
||
|
#define POWER_GREEN 11
|
||
|
#define POWER_BLUE 13
|
||
|
|
||
|
#define ECONOMY_RED 5
|
||
|
#define ECONOMY_GREEN 6
|
||
|
#define ECONOMY_BLUE 9
|
||
|
|
||
|
#define SERVO0 A1
|
||
|
#define SERVO1 A2
|
||
|
|
||
|
#define BUZZER A3
|
||
|
|
||
|
// notes in the melody:
|
||
|
int melody[] = {
|
||
|
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
|
||
|
|
||
|
// note durations: 4 = quarter note, 8 = eighth note, etc.:
|
||
|
int noteDurations[] = {
|
||
|
4, 8, 8, 4,4,4,4,4 };
|
||
|
|
||
|
boolean values_ok =false;
|
||
|
unsigned long Sensor[9];
|
||
|
Servo servo0, servo1; // create servo object to control a servo
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
Wire.begin(4); // join i2c bus with address #4
|
||
|
Wire.onReceive(receiveEvent); // register event
|
||
|
Serial.begin(9600); // start serial for output
|
||
|
// servo0.attach(SERVO0);
|
||
|
// servo1.attach(SERVO1);
|
||
|
|
||
|
for (int red =0; red<256; red++) {power_led(red, 0, 0); delay(10);}
|
||
|
for (int green =0; green<256; green++) {power_led(0, green, 0); delay(10);}
|
||
|
for (int blue =0; blue<256; blue++) {power_led(0, 0, blue); delay(10);}
|
||
|
power_led(0, 0, 0);
|
||
|
|
||
|
for (int red =0; red<256; red++) {economy_led(red, 0, 0); delay(10);}
|
||
|
for (int green =0; green<256; green++) {economy_led(0, green, 0); delay(10);}
|
||
|
for (int blue =0; blue<256; blue++) {economy_led(0, 0, blue); delay(10);}
|
||
|
economy_led(0, 0, 0);
|
||
|
|
||
|
// iterate over the notes of the melody:
|
||
|
for (int thisNote = 0; thisNote < 8; thisNote++) {
|
||
|
|
||
|
// to calculate the note duration, take one second
|
||
|
// divided by the note type.
|
||
|
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
|
||
|
int noteDuration = 1000/noteDurations[thisNote];
|
||
|
tone(BUZZER, melody[thisNote],noteDuration);
|
||
|
|
||
|
// to distinguish the notes, set a minimum time between them.
|
||
|
// the note's duration + 30% seems to work well:
|
||
|
int pauseBetweenNotes = noteDuration * 1.30;
|
||
|
delay(pauseBetweenNotes);
|
||
|
// stop the tone playing:
|
||
|
noTone(8);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
void loop()
|
||
|
{
|
||
|
if (values_ok)
|
||
|
{
|
||
|
Serial.print("Temperatura: "); Serial.print(-46.85 + 175.72 / 65536.0 * (float)Sensor[0]); Serial.println(" C");
|
||
|
Serial.print("Humedad: "); Serial.print(-6.0 + 125.0 / 65536.0 * (float)Sensor[1]); Serial.println(" %");
|
||
|
Serial.print("Luz: "); Serial.print(Sensor[2]/10.); Serial.println(" lx");
|
||
|
Serial.print("Bateria: "); Serial.print(Sensor[3]/10.); Serial.println(" %");
|
||
|
Serial.print("Panel Solar: "); Serial.print(Sensor[4]); Serial.println(" mV");
|
||
|
Serial.print("Monoxido de Carbono: "); Serial.print(Sensor[5]/1000.); Serial.println(" kOhm");
|
||
|
Serial.print("Dioxido de Nitrogeno: "); Serial.print(Sensor[6]/1000.); Serial.println(" kOhm");
|
||
|
Serial.print("Ruido: "); Serial.print(Sensor[7]); Serial.println(" mV");
|
||
|
Serial.print("Puntos WIFI: "); Serial.println(Sensor[8]);
|
||
|
Serial.println(F("*******************"));
|
||
|
values_ok=false;
|
||
|
}
|
||
|
// for (int i=0; i<180; i++)
|
||
|
// {
|
||
|
// servo0.write(i); // sets the servo position according to the scaled value
|
||
|
// servo1.write(i); // sets the servo position according to the scaled value
|
||
|
// delay(15); // waits for the servo to get there
|
||
|
// }
|
||
|
// for (int i=180; i>0; i--)
|
||
|
// {
|
||
|
// servo0.write(i); // sets the servo position according to the scaled value
|
||
|
// servo1.write(i); // sets the servo position according to the scaled value
|
||
|
// delay(15); // waits for the servo to get there
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
// function that executes whenever data is received from master
|
||
|
// this function is registered as an event, see setup()
|
||
|
int count = 0;
|
||
|
int value = 0;
|
||
|
|
||
|
void receiveEvent(int howMany)
|
||
|
{
|
||
|
while (Wire.available())
|
||
|
{
|
||
|
unsigned long x = Wire.read(); // receive byte as an integer
|
||
|
if (count == 0) {count=count + 1; Sensor[value] = x<<24;}
|
||
|
else if (count == 1) {count=count + 1; Sensor[value] = Sensor[value]|(x<<16);}
|
||
|
else if (count == 2) {count=count + 1; Sensor[value] = Sensor[value]|(x<<8);}
|
||
|
else if (count == 3) {count = 0; Sensor[value] = Sensor[value]|x; value=value+1; if(value==9) {value=0; values_ok=true; }}
|
||
|
// Serial.println(x); // print the integer
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void power_led(int red, int green, int blue)
|
||
|
{
|
||
|
analogWrite(POWER_RED, red);
|
||
|
analogWrite(POWER_GREEN, green);
|
||
|
analogWrite(POWER_BLUE, blue);
|
||
|
}
|
||
|
|
||
|
void economy_led(int red, int green, int blue)
|
||
|
{
|
||
|
analogWrite(ECONOMY_RED, red);
|
||
|
analogWrite(ECONOMY_GREEN, green);
|
||
|
analogWrite(ECONOMY_BLUE, blue);
|
||
|
}
|