Lab_interaccio/2014/refarm/refarm.ino
2025-02-25 21:29:42 +01:00

384 lines
11 KiB
C++

#include "OneWire.h"
#include "DallasTemperature.h"
#include <Wire.h>
#define AWAKE 4 // WIFLY AWAKE
#define PANEL A8 // PANEL LEVEL
#define BAT A7 // BAT LEVEL
#define IO0 5 // MICS5525_HEATHER
#define IO1 13 // MICS2710_HEATHER
#define IO2 9 // MICS2710_HIGH_IMPEDANCE
#define IO3 10 // MICS2710_HIGH_IMPEDANCE
#define FACTORY 7 // WIFLY - Factory RESET/AP RN131
#define CONTROL 12 // WIFLY - CONTROL
#define S0 A4 //MICS_5525
#define S1 A5 //MICS_2710
#define S2 A2 //SENS_5525
#define S3 A3 //SENS_2710
#define S4 A0 //MICRO
#define S5 A1 //LDR
uint8_t dataPin = IO1;
uint8_t latchPin = IO3;
uint8_t clockPin = IO2;
uint8_t val_shift; //Valor del shift register
uint8_t val_shift1; //Valor del shift register
#define bh1730 0x29 // Direction of the light sensor
#define Temperature 0x40 // Direction of the sht21
#define MCP1 0x2E // Direction of the mcp1 Potenciometers
#define PH 0x00 // Direction of ph resistor
#define MOTOR0 0x01 // Direction of motor 1 resistor
#define MOTOR1 0x07 // Direction of motor 2 resistor
#define TWI_FREQ 400000L //Frecuencia bus I2C
// DS18S20 Temperature chip i/o
OneWire ds(IO0); // on pin 10
OneWire ds1(S5); // on pin 10
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&ds);
DallasTemperature sensors1(&ds1);
void led_rgb(boolean red, boolean green, boolean blue){
shiftWrite(11, !blue);
shiftWrite(10, !green);
shiftWrite( 9, !red);
}
float average(int anaPin) {
int lecturas = 100;
long total = 0;
float average = 0;
for(int i=0; i<lecturas; i++)
{
//delay(1);
total = total + analogRead(anaPin);
}
average = (float)total / lecturas;
return(average);
}
float EC_final = 12880;
float EC_inicial = 1413;
float Voltage_inicial = 99.56;
float Voltage_final = 1189.39;
float kbEC = (((EC_final*Voltage_inicial)/Voltage_final) - EC_inicial)/((Voltage_inicial/Voltage_final)-1);
float kmEC = (EC_inicial - kbEC)/Voltage_inicial;
float kmPH = 3.5;
float kbPH = 0;
void resetshift()
{
val_shift = 0x00;
val_shift1 = 0x00;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, val_shift);
shiftOut(dataPin, clockPin, MSBFIRST, val_shift1);
digitalWrite(latchPin, HIGH);
}
void shiftWrite(uint8_t pin, boolean state)
{
if (pin<8) bitWrite(val_shift, pin, state);
else bitWrite(val_shift1, pin-8, state);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, val_shift);
shiftOut(dataPin, clockPin, MSBFIRST, val_shift1);
digitalWrite(latchPin, HIGH);
}
float temperature,ECcurrent;
unsigned int AnalogAverage = 0,averageVoltage=0; // the average
float EcSensor()
{
sensors.requestTemperatures(); // Send the command to get temperatures
temperature = sensors.getTempCByIndex(0);
// Serial.print("kb: ");
// Serial.print(kbEC);
// Serial.print(" km: ");
// Serial.println(kmEC);
averageVoltage = average(S4)*(float)3300/1024;
float TempCoefficient=1.0+0.0185*(temperature-25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.0185*(fTP-25.0));
float CoefficientVolatge=(float)averageVoltage/TempCoefficient;
if(CoefficientVolatge<90);//Serial.println("No solution!"); //25^C 1413us/cm<-->about 216mv if the voltage(compensate)<150,that is <1ms/cm,out of the range
// else if(CoefficientVolatge>3300)Serial.println("Out of the range!"); //>20ms/cm,out of the range
else
{
ECcurrent = kmEC*CoefficientVolatge + kbEC;
// if(CoefficientVolatge<=448)ECcurrent=(6.84*CoefficientVolatge-64.32)*1.51; //1ms/cm<EC<=3ms/cm
// else if(CoefficientVolatge<=1457)ECcurrent=6.98*CoefficientVolatge-127; //3ms/cm<EC<=10ms/cm
// else ECcurrent=5.3*CoefficientVolatge+2278; //10ms/cm<EC<20ms/cm
return ECcurrent/=1000; //convert us/cm to ms/cm
}
return 0; //convert us/cm to ms/cm
}
float PHSensor()
{
averageVoltage = average(S3)*(float)3300/1024;
float PHcurrent = kmPH*averageVoltage/1000. + kbPH;
return PHcurrent;
// return averageVoltage;
}
uint32_t lastHumidity;
uint32_t lastTemperature;
uint16_t readSHT21(uint8_t type){
uint16_t DATA = 0;
Wire.beginTransmission(Temperature);
Wire.write(type);
Wire.endTransmission();
Wire.requestFrom(Temperature,2);
unsigned long time = millis();
while (!Wire.available()) if ((millis() - time)>500) return 0x00;
DATA = Wire.read()<<8;
while (!Wire.available());
DATA = (DATA|Wire.read());
DATA &= ~0x0003;
return DATA;
}
void getSHT21()
{
lastTemperature = readSHT21(0xE3); // RAW DATA for calibration in platform
lastHumidity = readSHT21(0xE5); // RAW DATA for calibration in platform
Serial.print(" Temperature SHT21: ");
Serial.print((-46.85 + 175.72 / 65536.0 * (float)lastTemperature));
Serial.print(" C, Humidity SHT21: ");
Serial.print(-6.0 + 125.0 / 65536.0 * (float)lastHumidity);
Serial.print(" % ");
}
uint16_t getLight()
{
uint8_t TIME0 = 0xDA;
uint8_t GAIN0 = 0x00;
uint8_t DATA [8] = {0x03, TIME0, 0x00 ,0x00, 0x00, 0xFF, 0xFF ,GAIN0} ;
uint16_t DATA0 = 0;
uint16_t DATA1 = 0;
Wire.beginTransmission(bh1730);
Wire.write(0x80|0x00);
for(int i= 0; i<8; i++) Wire.write(DATA[i]);
Wire.endTransmission();
delay(100);
Wire.beginTransmission(bh1730);
Wire.write(0x94);
Wire.endTransmission();
Wire.requestFrom(bh1730, 4);
DATA0 = Wire.read();
DATA0=DATA0|(Wire.read()<<8);
DATA1 = Wire.read();
DATA1=DATA1|(Wire.read()<<8);
uint8_t Gain = 0x00;
if (GAIN0 == 0x00) Gain = 1;
else if (GAIN0 == 0x01) Gain = 2;
else if (GAIN0 == 0x02) Gain = 64;
else if (GAIN0 == 0x03) Gain = 128;
float ITIME = (256- TIME0)*2.7;
float Lx = 0;
float cons = (Gain * 100) / ITIME;
float comp = (float)DATA1/DATA0;
if (comp<0.26) Lx = ( 1.290*DATA0 - 2.733*DATA1 ) / cons;
else if (comp < 0.55) Lx = ( 0.795*DATA0 - 0.859*DATA1 ) / cons;
else if (comp < 1.09) Lx = ( 0.510*DATA0 - 0.345*DATA1 ) / cons;
else if (comp < 2.13) Lx = ( 0.276*DATA0 - 0.130*DATA1 ) / cons;
else Lx=0;
Serial.print(" BH1730: ");
Serial.print(Lx);
Serial.print(" Lx");
return Lx;
}
#define RES 256 // Digital pot. resolution
#define P1 5 //Digital potentiometer resistance 100Kohm
#define VCC 3300 //Voltage VCC
float kr= ((float)P1*1000)/RES; // Resistance conversion Constant for the digital pot.
void writeMCP(byte deviceaddress, byte address, int data ) {
if (data>RES) data=RES;
Wire.beginTransmission(deviceaddress);
address=(address<<4)|bitRead(data, 8) ;
Wire.write(address);
Wire.write(lowByte(data));
Wire.endTransmission();
delay(4);
}
int readMCP(int deviceaddress, uint16_t address ) {
byte rdata = 0xFF;
int data = 0x0000;
Wire.beginTransmission(deviceaddress);
address=(address<<4)|B00001100;
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(deviceaddress,2);
unsigned long time = millis();
while (!Wire.available()) if ((millis() - time)>500) return 0x00;
rdata = Wire.read();
data=rdata<<8;
while (!Wire.available());
rdata = Wire.read();
data=data|rdata;
return data;
}
void writeRL(byte device, long resistor) {
int data=0x00;
data = (int)(resistor/kr);
writeMCP(MCP1, device, data);
}
float readRL(byte device)
{
return (kr*readMCP(MCP1, device)); // Returns Resistance (Ohms)
}
void writeGAIN(float gain) {
long resistor=0x00;
resistor = (long)((((1+P1)*(gain-1)-2.2)/gain)*1000);
writeRL(PH, resistor);
}
float readGAIN()
{
float resistor = readRL(PH)/1000.;
return (2.2 + resistor)/(1 + P1 - resistor) + 1; // Returns Resistance (Ohms)
}
void motor_orientation(int motor, int orientation){
if (motor==MOTOR1)
{
if (orientation == 0 )
{
shiftWrite(1, 0);
shiftWrite(2, 0);
}
else if (orientation == 1 )
{
shiftWrite(1, 0);
shiftWrite(2, 1);
}
else if (orientation == -1 )
{
shiftWrite(1, 1);
shiftWrite(2, 0);
}
}
else
{
if (orientation == 0 )
{
shiftWrite(3, 0);
shiftWrite(4, 0);
}
else if (orientation == 1 )
{
shiftWrite(3, 0);
shiftWrite(4, 1);
}
else if (orientation == -1 )
{
shiftWrite(3, 1);
shiftWrite(4, 0);
}
}
}
void writeVoltage(byte device, float voltage) {
int data=0x00;
if (voltage==0) motor_orientation(device, 0);
else if (voltage>0) motor_orientation(device, 1);
else if (voltage<0) motor_orientation(device, -1);
data = (int)(abs(voltage)*P1*1000/VCC);
// Serial.println(data);
writeRL(device, data);
}
float readVoltage(byte device) {
return (VCC*readRL(device))/(P1*1000); // Returns Resistance (Ohms)
}
byte opto[4] = {12, 13, 14, 15};
void relay(byte device, boolean state) {
shiftWrite(opto[device], state);
}
void setup(){
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
Wire.begin();
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
Serial.begin(115200);
resetshift();
led_rgb(0, 0, 4095);
// Start up the library DS18B20
sensors.begin();
sensors1.begin();
for (int i=0; i<4; i++) relay(i, LOW);
}
void loop(){
Serial.print("EC: ");
Serial.print(EcSensor());
Serial.print(" ms/cm");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.print(" Temperature DS18B20: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.print(" ^C");
getSHT21();
getLight();
writeGAIN(3.5);
Serial.print(" GAIN PH: ");
Serial.print(readGAIN());
Serial.print(" PH: ");
Serial.print(PHSensor());
Serial.print(" HUMIDITY: ");
Serial.print(average(S0));
sensors1.requestTemperatures(); // Send the command to get temperatures
Serial.print(" Temperature1 DS18B20: ");
Serial.print(sensors1.getTempCByIndex(0));
Serial.print(" ^C");
writeVoltage(MOTOR0, 0);
Serial.print(" Voltage MOTOR1: ");
Serial.print(readVoltage(MOTOR0));
writeVoltage(MOTOR1, 0);
Serial.print(" Voltage MOTOR2: ");
Serial.println(readVoltage(MOTOR1));
// for(int i=7; i<16; i++)
// {
// Serial.println(i);
// shiftWrite(i, LOW);
// delay(1000);
// shiftWrite(i, HIGH);
// delay(1000);
// }
}