View Single Post
Old 04-03-14, 01:28 AM   #7
AC_Hacker
Supreme EcoRenovator
 
AC_Hacker's Avatar
 
Join Date: Mar 2009
Location: Portland, OR
Posts: 4,004
Thanks: 303
Thanked 723 Times in 534 Posts
Default

If you go back to GITHUB you will find an examples link that will give you a working example of Arduino code, T6603Example.ino

Here it is:

Code:
/*
Copyright (c) 2014 Brian Manley

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "SoftwareSerial.h"
#include <T6603.h>

T6603 sensor;

void setup() {
    Serial.begin(19200);
    sensor.begin(10, 11);  
}

void loop() {
    Serial.println(sensor.get_co2());
    delay(2000);
}
This piece of code calls on two libraries to be loaded at compile time:

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


one of them is T6603.h which is also at the GITHUB page, and is shown here:

Code:
/*
Copyright (c) 2014 Brian Manley

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <SoftwareSerial.h>
#include "T6603.h"

T6603::T6603() {

}

T6603::~T6603() {
    
    if ( NULL != _serial ) {
        delete _serial;
        _serial = NULL;
    }
}

void T6603::begin(uint8_t rx, uint8_t tx) {

    _serial = new SoftwareSerial(rx, tx);
    _serial->begin(19200);
}

int T6603::get_co2(void) {

    _serial->overflow();
    _serial->write(FLAG);
    _serial->write(BRDCST);
    _serial->write(0x02);
    _serial->write(CMD_READ); 
    _serial->write(CO2_PPM);
    delay(50);

    for ( int attempts = 0; attempts < MAX_ATTEMPTS; attempts++ ) {

        byte reading[5]; 
        int bytesRead = 0;        
        
        while ( _serial->available() && bytesRead < 6) {
            reading[bytesRead] = _serial->read();
            bytesRead++;
            delay(10);
        }

        if ( reading[0] == 0xFF && reading[1] == 0xFA ) {
            int i = 0;
            i |= reading[3] & 0xFF;
            i <<= 8;
            i |= reading[4] & 0xFF;
            _lastReading = i;
            return (_lastReading); 
        }
    }
    
    return (_lastReading);
}

byte T6603::get_status(void) {

    _serial->overflow();
    _serial->write(FLAG);
    _serial->write(BRDCST);
    _serial->write(0x01);
    _serial->write(CMD_STATUS); 
    delay(50);

    for ( int attempts = 0; attempts < MAX_ATTEMPTS; attempts++ ) {

        byte reading[4]; 
        int bytesRead = 0;        
        
        while ( _serial->available() && bytesRead < 4) {
            reading[bytesRead] = _serial->read();
            bytesRead++;
            delay(10);
        }

        if ( reading[0] == 0xFF && reading[1] == 0xFA ) {
            return ( reading[3] );
        }
    }
    
    return (NULL);
}

void T6603::set_idle(bool onOff) {

    byte cmd = onOff ? 0x01 : 0x02;
    
    _serial->overflow();
    _serial->write(FLAG);
    _serial->write(BRDCST);
    _serial->write(0x02);
    _serial->write(CMD_IDLE); 
    _serial->write(cmd);
    delay(50);

    for ( int attempts = 0; attempts < MAX_ATTEMPTS; attempts++ ) {

        byte reading[3]; 
        int bytesRead = 0;        
        
        while ( _serial->available() && bytesRead < 3) {
            reading[bytesRead] = _serial->read();
            bytesRead++;
            delay(10);
        }

        if ( reading[0] == 0xFF && reading[1] == 0xFA ) {
            return;
        }
    }
        
}
The other library is probably already part of your Arduino IDE. But if it is not, you can find it HERE, and the code is shown below:

Code:
/*
SoftwareSerial.h (formerly NewSoftSerial.h) - 
Multi-instance software serial library for Arduino/Wiring
-- Interrupt-driven receive and other improvements by ladyada
   (http://ladyada.net)
-- Tuning, circular buffer, derivation from class Print/Stream,
   multi-instance support, porting to 8MHz processors,
   various optimizations, PROGMEM delay tables, inverse logic and 
   direct port writing by Mikal Hart (http://www.arduiniana.org)
-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

The latest version of this library can always be found at
http://arduiniana.org.
*/

#ifndef SoftwareSerial_h
#define SoftwareSerial_h

#include <inttypes.h>
#include <Stream.h>

/******************************************************************************
* Definitions
******************************************************************************/

#define _SS_MAX_RX_BUFF 64 // RX buffer size
#ifndef GCC_VERSION
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#endif

class SoftwareSerial : public Stream
{
private:
  // per object data
  uint8_t _receivePin;
  uint8_t _receiveBitMask;
  volatile uint8_t *_receivePortRegister;
  uint8_t _transmitBitMask;
  volatile uint8_t *_transmitPortRegister;

  uint16_t _rx_delay_centering;
  uint16_t _rx_delay_intrabit;
  uint16_t _rx_delay_stopbit;
  uint16_t _tx_delay;

  uint16_t _buffer_overflow:1;
  uint16_t _inverse_logic:1;

  // static data
  static char _receive_buffer[_SS_MAX_RX_BUFF]; 
  static volatile uint8_t _receive_buffer_tail;
  static volatile uint8_t _receive_buffer_head;
  static SoftwareSerial *active_object;

  // private methods
  void recv();
  uint8_t rx_pin_read();
  void tx_pin_write(uint8_t pin_state);
  void setTX(uint8_t transmitPin);
  void setRX(uint8_t receivePin);

  // private static method for timing
  static inline void tunedDelay(uint16_t delay);

public:
  // public methods
  SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
  ~SoftwareSerial();
  void begin(long speed);
  bool listen();
  void end();
  bool isListening() { return this == active_object; }
  bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
  int peek();

  virtual size_t write(uint8_t byte);
  virtual int read();
  virtual int available();
  virtual void flush();
  
  using Print::write;

  // public only for easy access by interrupt handlers
  static inline void handle_interrupt();
};

// Arduino 0012 workaround
#undef int
#undef char
#undef long
#undef byte
#undef float
#undef abs
#undef round

#endif

The idea is that if you go to your Arduino IDE installation, and drill down, you will find a folder called libraries. You'll probably see a few libraries in there already.

So you make a folder by the exact name of the library being called, and you put the library-code file by it's exact name into that folder.

You need to have all called libraries available when you compile, in this case you are compiling T6603Example.ino.


Is life any easier now?

-AC
__________________
I'm not an HVAC technician. In fact, I'm barely even a hacker...

Last edited by AC_Hacker; 04-03-14 at 01:31 AM..
AC_Hacker is offline   Reply With Quote