testfase

Dependencies:   TextLCD mbed DS1307 Servo

main.cpp

Committer:
JonathanCaes
Date:
2014-12-04
Revision:
1:83cbca2c2d86
Parent:
0:09e0c8d681a8

File content as of revision 1:83cbca2c2d86:

//Library's
#include "mbed.h"
#include "TextLCD.h"
#include "Servo.h"
#include "ds1307.h"

//Pin initialisatie
Serial pc(USBTX,USBRX);
AnalogIn temp1(p20);
AnalogIn temp2(p19);
DigitalIn knopUP(p5);
DigitalIn knopDOWN(p6);
DigitalIn knopINSTEL(p7);
DigitalIn knopBACK(p8);
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
TextLCD lcd(p15, p16, p21, p22, p23, p24); // rs, e, d4-d7
PwmOut Servo(p25);
DS1307 my1307(p9, p10);
DigitalOut relais1(p30);
DigitalOut RGBblauw(p29);
DigitalOut RGBgroen(p28);
DigitalOut RGBrood(p27);

//variabelen declareren/initialiseren
float i = 0;
float j = 0;
int temperatuur = 23;
int counter = 49;
int counter2 = 249;
int teller = 0;
int RTC = 0;
int motor = 2; //motor = 2, dan is hij in ruststand

//variabelen RTC
 int sec;
 int min;
 int hours;
 int day;
 int date;
 int month;
 int year;
 
 void test_rw(int test) {
} 

//Main Programma
int main()
{
    Servo.period_ms(20);
    Servo = (90 * 0.000511 + 0.026); // rust
    wait(0.1);
    if((i*3.3)>= temperatuur)// te warm
    {
        motor = 0;
        myled1 = 1;
        relais1 = 0;    //Relais aansturen
        RGBrood = 1;    //RGB aansturen
        RGBgroen = 0;
        RGBblauw = 0; 
        if(motor == 0 || motor == 2)//motor = 0, dan staat hij links
        { 
            Servo.period_ms(20);
            Servo = (170 * 0.000511 + 0.026); // rechts
            motor = 1;
        }  
    }
    if((i*3.3) < temperatuur)// te koud
    {
        motor = 1;
        myled1 = 0;
        relais1 = 1;    //Relais aansturen
        RGBrood = 0;    //RGB aansturen
        RGBgroen = 1;
        RGBblauw = 1;
        if(motor == 1 || motor == 2) //motor = 1, dan staat hij rechts
        {
            Servo.period_ms(20); 
            Servo = (1 * 0.000511 + 0.026); // links
            motor = 0;
        }
    }
    pc.printf("\x1B\x48\x1B\x4A");
    pc.printf("Temperatuur");
    while(1)
    {
/*------------------------------------------------------------------------*/
        //Seriele communicatie met pc
        pc.printf("\x1B\x48");
        i=0;
        j=0;
/*------------------------------------------------------------------------*/
        //Temperatuur inlezen
        for(int t=0;t<100;t++) 
        {
            i=i+temp1.read();
            
        }
        for(int k=0;k<100;k++) 
        {
            j=j+temp2.read();
        }
/*------------------------------------------------------------------------*/
        //Motor aansturen
        /*if(knopUP)
        {
            Servo.period_ms(20);
            Servo = (90 * 0.000511 + 0.026); // rust
            wait(1);
            Servo = (7 * 0.000511 + 0.026); // links 7
            wait(1);
            Servo = (90 * 0.000511 + 0.026); // rust
            wait(1);
            Servo = (170 * 0.000511 + 0.026); // rechts
            wait(1);
        }*/
/*------------------------------------------------------------------------*/
        //Menu Instellingen
        if(knopINSTEL)
        {
            teller = 0;
            wait(0.3);
            lcd.cls();
            while(knopBACK == 0)
            {    
                lcd.locate(0,0);
                lcd.printf(" Instellen Temp");
                lcd.locate(0,1);
                lcd.printf("   Temp = %iC", temperatuur);
                if(knopUP)
                {
                    temperatuur = temperatuur + 1;
                    if(temperatuur > 30)
                    {
                        temperatuur = 20;  
                    }
                    if(temperatuur < 20)
                    {
                        temperatuur = 30;   
                    }
                    wait(0.2); 
                }
                if(knopDOWN)
                {
                    temperatuur = temperatuur - 1;
                    if(temperatuur == 30)
                    {
                        temperatuur = 20;  
                    }
                    if(temperatuur < 20)
                    {
                        temperatuur = 30;   
                    } 
                    wait(0.2);
                    /*
                    lcd.cls();
                    lcd.locate(0,0);
                    lcd.printf("Ingestelde Temp");
                    lcd.locate(0,1);
                    lcd.printf("       %iC     ", temperatuur);
                    wait(0.6);
                    lcd.cls();*/
                }
            }
            wait(0.5);
        }
/*------------------------------------------------------------------------*/
        /*Temperatuur: Wanneer temperatuur hoger wordt dan ingesteld
                       zal een ledje dit aangeven
          Relais aansturen
          RGB led aansturen
          Servomotor aansturen
        */        
        if((i*3.3)>= temperatuur + 1)// te warm
        {
            myled1 = 1;
            relais1 = 0;    //Relais aansturen
            RGBrood = 1;    //RGB aansturen
            RGBgroen = 0;
            RGBblauw = 0; 
            if(motor == 0)//motor = 0, dan staat hij links
            { 
                Servo.period_ms(20);
                Servo = (170 * 0.000511 + 0.026); // rechts
                motor = 1;
            }  
        }
        if((i*3.3) <= temperatuur - 1)// te koud
        {
            myled1 = 0;
            relais1 = 1;    //Relais aansturen
            RGBrood = 0;    //RGB aansturen
            RGBgroen = 1;
            RGBblauw = 1;
            if(motor == 1) //motor = 1, dan staat hij rechts
            {
                Servo.period_ms(20); 
                Servo = (1 * 0.000511 + 0.026); // links
                motor = 0;
            }
        }
/*------------------------------------------------------------------------*/    
        //LCD Display uitschrijven RTC of Temperatuur
        if(knopBACK)
        {
            RTC ++;
            if(RTC == 2)
            {
                RTC = 0;    
            }
            wait(0.5);    
        }
        if(RTC == 0)
        {
            //LCD uitschrijven
            counter++;
            if(counter == 50)
            {
                lcd.cls();
                lcd.locate(0,0);
                lcd.printf("Temp 1:%4.1fC", i*3.3);
                lcd.locate(0,1);
                lcd.printf("Temp 2:%4.1fC", j*3.3);
                pc.printf("\n\n\rTemperature1 %4.1f C ",i*3.3);
                pc.printf("\n\rTemperature2 %4.1f C ",j*3.3);
                counter = 0;
            }
        }
        else if(RTC == 1)
        {   
            //RTC uitschrijven
            counter2++;
            if(counter2 == 250)
            {
                lcd.cls();
                test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year));
                //Dag uitschrijven op lcd
                if(day == 1)
                {lcd.locate(3,1);
                lcd.printf("Ma");}
                if(day == 2)
                {lcd.locate(3,1);
                lcd.printf("Di");}
                if(day == 3)
                {lcd.locate(3,1);
                lcd.printf("Wo");}
                if(day == 4)
                {lcd.locate(3,1);
                lcd.printf("Do");}
                if(day == 5)
                {lcd.locate(3,1);
                lcd.printf("Vr");}
                if(day == 6)
                {lcd.locate(3,1);
                lcd.printf("Za");}
                if(day == 7)
                {lcd.locate(3,1);
                lcd.printf("Zo");}
                //Uur min sec //Dag datum maand jaar
                if(sec >= 9 && min >= 9 && hours >= 9)
                {
                    lcd.locate(4,0);
                    lcd.printf("%d:%d:%d",hours,min,sec);
                    lcd.locate(6,1);
                    lcd.printf("%d/%d/%d",date,month,year);
                    if(knopBACK)
                    {
                        RTC = 0;    
                    }
                }
                if(sec <= 9 && min >= 9 && hours >= 9)
                {
                    lcd.locate(4,0);
                    lcd.printf("%d:%d:0%d",hours,min,sec);
                    lcd.locate(6,1);
                    lcd.printf("%d/%d/%d",date,month,year);
                    if(knopBACK)
                    {
                        RTC = 0;    
                    }
                }
                if(sec >= 9 && min <= 9 && hours >= 9)
                {
                    lcd.locate(4,0);
                    lcd.printf("%d:0%d:%d",hours,min,sec);
                    lcd.locate(6,1);
                    lcd.printf("%d/%d/%d",date,month,year);
                    if(knopBACK)
                    {
                        RTC = 0;    
                    }
                }
                if(sec >= 9 && min >= 9 && hours <= 9)
                {
                    lcd.locate(4,0);
                    lcd.printf("0%d:%d:%d",hours,min,sec);
                    lcd.locate(6,1);
                    lcd.printf("%d/%d/%d",date,month,year);
                    if(knopBACK)
                    {
                        RTC = 0;    
                    }
                }
                if(sec <= 9 && min <= 9 && hours >= 9)
                {
                    lcd.locate(4,0);
                    lcd.printf("%d:0%d:0%d",hours,min,sec);
                    lcd.locate(6,1);
                    lcd.printf("%d/%d/%d",date,month,year);
                    if(knopBACK)
                    {
                        RTC = 0;    
                    }
                }
                if(sec <= 9 && min >= 9 && hours <= 9)
                {
                    lcd.locate(4,0);
                    lcd.printf("0%d:%d:0%d",hours,min,sec);
                    lcd.locate(6,1);
                    lcd.printf("%d/%d/%d",date,month,year);
                    if(knopBACK)
                    {
                        RTC = 0;    
                    }
                }
                if(sec >= 9 && min <= 9 && hours <= 9)
                {
                    lcd.locate(4,0);
                    lcd.printf("0%d:0%d:%d",hours,min,sec);
                    lcd.locate(6,1);
                    lcd.printf("%d/%d/%d",date,month,year);
                    if(knopBACK)
                    {
                        RTC = 0;    
                    }
                }
                if(sec <= 9 && min <= 9 && hours <= 9)
                {
                    lcd.locate(4,0);
                    lcd.printf("0%d:0%d:0%d",hours,min,sec);
                    lcd.locate(6,1);
                    lcd.printf("%d/%d/%d",date,month,year);
                    if(knopBACK)
                    {
                        RTC = 0;    
                    }
                }
                counter2 = 0;
            }
        }
    }
}