Template:Team:Aachen/arduino device.ino

From 2014.igem.org

Revision as of 19:08, 11 October 2014 by Mjoppich (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

/* includes

  • /
  1. include <DHT.h>
  2. include <LedControl.h>
  1. define MIN(a,b) ((a)<(b)?(a):(b))
  2. define MAX(a,b) ((a)>(b)?(a):(b))

/*

PINS

  • /
  1. define DHTPIN 9 // what pin we're connected to
  2. define DHT2PIN 10 //room temperature
  3. define RELAY1 6
  1. define LED_450_PIN 5
  2. define LED_480_PIN 8
  3. define LED_450_CHAR 53
  4. define LED_480_CHAR 56

/*

Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 12 is connected to the DataIn 
pin 11 is connected to the CLK 
pin 10 is connected to LOAD 
We have only a single MAX72XX.
*/
  1. define LCD_DI 13
  2. define LCD_CLK 12
  3. define LCD_LD 11
  1. define PEL_MOSFET 4
  1. define LEDS 3
  1. define T_TARGET 38

// Uncomment whatever type you're using! //#define DHTTYPE DHT11 // DHT 11

  1. define DHTTYPE DHT22 // DHT 22 (AM2302)

//#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht_int(DHT2PIN, DHTTYPE); DHT dht_ext(DHTPIN, DHTTYPE);

LedControl lc=LedControl(LCD_DI, LCD_CLK, LCD_LD, 1);

/*

Peltier

  • /

int power = 0; //Power level fro 0 to 99% int peltier_level = map(power, 0, 99, 0, 255); //This is a value from 0 to 255 that actually controls the MOSFET


/* CONTROLS

bTemp switches Temp sensor on bPeltier switches Peltier on bDisplay switches Display on bRelay enables Relay

  • /

bool bTemp = true; bool bPeltier = true; bool bDisplay = true; bool bRelay = true; bool bManualControl = false;

unsigned int delaytime = 100;

int iHeatingLevel = 0;


void setup() {

 Serial.begin(9600); 
 Serial.println("Serial Communication 9600 baud started");
 
 pinMode(LED_450_PIN,OUTPUT);
 pinMode(LED_480_PIN,OUTPUT);
 pinMode(LEDS,OUTPUT);


 if (bTemp)
 {
   dht_int.begin();
   dht_ext.begin();
 }
 
 if (bPeltier)
 {
 }
 /*
 if (bRelay)
 {
   pinMode(RELAY1, OUTPUT); 
 }
 */
 if (bDisplay)
 {
   lc.shutdown(0,false);
   /* Set the brightness to a medium values */
   lc.setIntensity(0,8);
   /* and clear the display */
   lc.clearDisplay(0);
 }

}


void loop() {

     char option;
     if(Serial.available()>0){
     option = Serial.read();
     if( option == 'l' ){
       //turn on leds
       digitalWrite(3,HIGH);
     }
     
     if( option == LED_450_CHAR ){
       //turn on leds
       digitalWrite(LED_450_PIN,HIGH);
     }
     
     if( option == LED_480_CHAR ){
       //turn on leds
       digitalWrite(LED_480_PIN,HIGH);
     }
     
     if( option == 'o' ){
       digitalWrite(3,LOW);
       
       digitalWrite(LED_450_PIN, LOW);
       digitalWrite(LED_480_PIN, LOW);
       //turn off leds
     }   
   }
 
 float h_pel = dht_ext.readHumidity();
 float t_pel = dht_ext.readTemperature();
 
 float t_room = dht_int.readTemperature();
 float h_room = dht_int.readHumidity();
 
 if (bTemp)
 {
 
   // check if returns are valid, if they are NaN (not a number) then something went wrong!
   if (isnan(t_pel) || isnan(t_room)) {
     //Serial.println("Failed to read from DHT");
   } else {
     //Serial.print("Humidity: "); 
     //Serial.print("\t");
     //Serial.print("Temperature: ");       
     //Serial.print("\n");
     
     
     //Serial.print("ext/pel ");
     //Serial.print(h_pel);
     //Serial.print("\t");
     //Serial.print(t_pel);
     //Serial.print("\n");
     
     //Serial.print("room/int ");
     //Serial.print(h_room);
     //Serial.print("\t");
     //Serial.print(t_room);
     //Serial.print("\n");
     
     if( bDisplay ){
     //lc.clearDisplay(0);
     
     //double t = t_pel;
     double t = t_room;
     
     lc.setRow(0,0,B01001110);
     lc.setRow(0,1,B01100011);
     
     if( iHeatingLevel>0 ){
       lc.setRow(0,7,B00110111);
     }
     int digit;
     bool display_digit;
     for( int i=3; i<8; i++ ){
       display_digit = true;
       if( i>3 ){
         digit = t/(pow(10,i-4));
         if( pow(10,i-4)>t ) display_digit=false;
       }
       else{
         digit = t*pow(10,4-i);  
       }
       if( display_digit==false ){
         lc.setRow(0,i,B00000000);
       }
       else{
         digit = abs(digit%10);
         if( i==4 ){
           lc.setDigit(0,i,digit,true);
         }
         else{  
           lc.setDigit(0,i,digit,false);
         }
       }
       delay(delaytime);
       ////Serial.println(digit);
     }
     }
   }
 }
 
 /*
 
 PELTIER
 
 */
 
 if (bPeltier || bRelay)
 {
   int pel_level_manual;
       
   /*if(Serial.available() > 0)
   {
     option = Serial.read();
     if(option == 'a') 
     power += 5;
     else if(option == 'z')
     power -= 5;
     else if(option == 's')
     power = 0;
     else if (option == 'm')
     bManualControl = !bManualControl;
     
     if(power > 99) power = 99;
     if(power < 0) power = 0;
     
     pel_level_manual = map(power, 0, 99, 0, 255);
     
     if(option == '1'){
      digitalWrite(RELAY1,LOW);           // Turns ON Relays 1
      //Serial.println("Turned on");
     }
     else if(option=='0'){
      digitalWrite(RELAY1,HIGH);          // Turns Relay Off
      //Serial.println("Turned off");
     }
   }*/
   
   /*if (bManualControl)
   {
     analogWrite(PEL_MOSFET, pel_level_manual); //Write this new value out to the port
     return;
   }*/
   
   // stupid temperature control
   
   
   if ((t_pel > 50) && ( t_pel < 60 ))
   {
     iHeatingLevel = 70;
   }
   
   if (t_pel < 50)
   {
     iHeatingLevel = 99;
   }
   
   if (t_room < T_TARGET - 5)
   {
     iHeatingLevel = MIN(iHeatingLevel, 99);
   }
   if (t_room > T_TARGET - 2)
   {
     iHeatingLevel = MIN(iHeatingLevel, 85);
   }
   if (t_room > T_TARGET)
   {
     iHeatingLevel = 0;
   }
       
   //Serial.println("Automatic Control:");
   //Serial.print(" Heating Level ");
   //Serial.print(iHeatingLevel);
   //Serial.println("");
   power = map(iHeatingLevel,0,99,0,255);
   
   //Serial.print(" Power Level ");
   //Serial.println(power);
       
   analogWrite(PEL_MOSFET, power); //Write this new value out to the port
   
 }

}