Автор Тема: часовник  (Прочетена 11145 пъти)

mirandy

  • Новодошъл
  • *****
  • Публикации: 1
    • Профил
часовник
« -: Юни 13, 2016, 11:36:41 am »
Здравейте. От скоро се занимавам с ардуино, опитвам се да си направя един седем сегментен часовник с температура и срещтнах проблем - за часовника използвам гпс и срещтнах проблем, че когато ми отпадне гпс-а програмата ми забива. Искам да го направя така че когато нямам гпс да ми изписва само температура, но и да мери а не да забива и да ми изписва последната измерена температура както прави сега, а когато се появи гпс-а пак да започне да изписва час и температура. Опитах се с  if (uart_gps.available) но не се получава така както аз искам. Благодаря предварително за съдействието!

gamabg

  • Новодошъл
  • *****
  • Публикации: 1
    • Профил
Re: часовник
« Отговор #1 -: Септември 06, 2016, 05:14:04 am »
/*
* Arduino Clock with RTC Module + OpenSegment Shield - Red and RHT03 Sensor
* can give time, and every 20 seconds show humidity and then temperature.
*
* Code is just combination of examples I found for these nice modules.
*
* OpenSegment Shield Examples
* https://github.com/sparkfun/Serial7SegmentDisplay/
*
* RHT03 examples by Tihomir Trifonov
* http://playground.arduino.cc/Bulgarian/Dht22
*
* RTC1307 module examples by John Vaughters
* http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock
*
* Version 1.0 , 31 July 2014 by Svetlin Simeonov
*/

#include <SoftwareSerial.h>
#include "Wire.h"

#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address
#define I2C_WRITE Wire.write
#define I2C_READ Wire.read

#define APOSTROPHE  5
#define COLON       4
#define DECIMAL4    3
#define DECIMAL3    2
#define DECIMAL2    1
#define DECIMAL1    0

SoftwareSerial Serial7Segment(7, 8); //RX pin, TX pin

int cycles = 0;

int i;
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year,mm,hh;
byte test;
byte zero;
boolean colonOn = false;
char curtime[100];
int count=0;
int maxcount=20; // how many seconds to wayt until show humidity/temp
byte brightnessLevel = 30; // 0 to 100 %

// RTH03
byte dht_dpin = 5; // pin for RTH03 temperature sensor
byte dht_dat[5];   // temperature data array

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val) { return ( (val/16*10) + (val%16) ); }

int getMM()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(I2C_READ() & 0x7f);
  minute     = bcdToDec(I2C_READ());
  hour       = bcdToDec(I2C_READ() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(I2C_READ());
  dayOfMonth = bcdToDec(I2C_READ());
  month      = bcdToDec(I2C_READ());
  year       = bcdToDec(I2C_READ());

  return minute;
}

int getHH()
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(I2C_READ() & 0x7f);
  minute     = bcdToDec(I2C_READ());
  hour       = bcdToDec(I2C_READ() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(I2C_READ());
  dayOfMonth = bcdToDec(I2C_READ());
  month      = bcdToDec(I2C_READ());
  year       = bcdToDec(I2C_READ());

  return hour;
}

// RTH03 functions

void InitDHT(){
  pinMode(dht_dpin,OUTPUT);
  digitalWrite(dht_dpin,HIGH);
}


void StartDHT22(){

  // start signal 
  pinMode(dht_dpin,OUTPUT);
  digitalWrite(dht_dpin,LOW);
  delay(1);
  digitalWrite(dht_dpin,HIGH);
  delayMicroseconds(40);

  // wait for answer 
  pinMode(dht_dpin,INPUT); // now pin goes INPUT
  while(digitalRead(dht_dpin) == LOW);
  while(digitalRead(dht_dpin) == HIGH);
}

byte readByte(){

  byte result = 0;

  for(byte i=0; i< 8; i++){ // read 8 bytes

      while(digitalRead(dht_dpin) == LOW);
      delayMicroseconds(45);
      if (digitalRead(dht_dpin) == HIGH)
     result |= (1 << (7 - i));        // counts byt for 1 and add to possition
     
      while (digitalRead(dht_dpin)==HIGH); // waits for new byte signal
    }

  return result;
}

// Main program

void setup() {
  Wire.begin();
  zero=0x00;
  Serial.begin(9600);

  Serial7Segment.begin(9600); //Talk to the Serial7Segment at 9600 bps
  Serial7Segment.write('v'); //Reset the display - this forces the cursor to return to the beginning of the display
  Serial7Segment.print("0822"); //Send the hour and minutes to the display
  Serial7Segment.write(0x7A);  // Brightness control command
  Serial7Segment.write((byte) brightnessLevel);  // 0 is dimmest, 255 is brightest
  sprintf(curtime, "b%03d", brightnessLevel); //Convert deciSecond into a string that is right adjusted
  Serial7Segment.print(curtime);
  InitDHT();
  //delay(1000);
}

void loop()
{


    //Blink the colon every other second
    if(colonOn == true)
    {
      colonOn = false;
      Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
      Serial7Segment.write((byte) 0); // Turns off colon, apostrophoe, and all decimals
    }
    else
    {
      colonOn = true;
      Serial7Segment.write(0x77);  // Decimal, colon, apostrophe control command
      Serial7Segment.write( (1<<APOSTROPHE) | (1<<COLON) | (1<<DECIMAL2) ); // Turns on colon, apostrophoe, and far-right decimal
    }

  if((count>maxcount) && (colonOn == true)) {
     // display other
     
     StartDHT22();
     
      for (byte i=0; i<5; i++) { dht_dat = readByte(); }
      byte checksum = dht_dat[0] + dht_dat[1] + dht_dat[2 ] + dht_dat[3];   
     
      if(dht_dat[4] == checksum) { // if data is correct

          //// finds whole and fract part for humidity
          int HighByte = dht_dat[0]; // first byte
          int LowByte = dht_dat[1];  // second byte
          int TReading = (HighByte << 8) + LowByte;
          int Whole = TReading / 10; // whole part
          int Fract = TReading % 10; // fract part

          // print in serial monitor
     Serial.print("humdity = ");
     Serial.print(Whole);
     Serial.print(".");
     Serial.print(Fract);
     Serial.print("%  ");

          // display fix to display temperature and humidity as 26.10 instead of 26.01
          // since sensor has 0.1 accuracy
          if(Fract<10) Fract = Fract * 10;

          sprintf(curtime, "%02d%02d", Whole, Fract);
          Serial7Segment.print(curtime);
          delay(2000);   

          // same procedure now for temperature
          HighByte = dht_dat[2];  // third byte
          LowByte = dht_dat[3];   // fourth byte
          TReading = (HighByte << 8) + LowByte;
          Whole = TReading / 10;
          Fract = TReading % 10;
         
          // display fix
          if(Fract<10) Fract = Fract * 10;

          // prints in serial monitor
          Serial.print("temperature = ");
          int SignBit = TReading & 0x8000; // check sign of temperature
          if (SignBit)           // if it's bellow zero
           Serial.print("-");  // we set dash
     Serial.print(Whole); 
     Serial.print(".");   
     Serial.print(Fract); 
     Serial.println("C  ");

          sprintf(curtime, "%02d%02d", Whole, Fract);
          Serial7Segment.print(curtime);
          delay(2000);
     }
     
     // we zero counter again - so it waits another
     // maxcount cycles before display this
     count=0;

  } else {
     // display time
     hh=getHH();
     mm=getMM();

     sprintf(curtime, "Time: %02d:%02d", hh, mm);
     Serial.println(curtime);
     
     sprintf(curtime, "%02d%02d", hh, mm);
     Serial7Segment.print(curtime); //Send serial string out the soft serial port to the S7S
  }

  delay(1000);
  count++;
}

https://github.com/PaulStoffregen/Time