Show Posts
Pages: 1 [2] 3 4
16  Forum 2005-2010 (read only) / Syntax & Programs / Writing DS1621 Library on: February 07, 2009, 12:37:15 pm
Hi,

Im trying to convert the code found here http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1198065647 into a library similar to the one written for the DS1307 RTC. No credit being taken for the actual code, cos none of its mine!

This is the first library i've tried to write, and i'm having some problems getting it to compile. First the errors:

Code:
from hardware\libraries\DS1621/DS1621.h:11,


d:/arduino/arduino-0013/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected unqualified-id before 'int'


d:/arduino/arduino-0013/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'


d:/arduino/arduino-0013/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:111: error: expected `)' before 'int'


In file included from D:\Arduino\arduino-0013\hardware\cores\arduino/wprogram.h:6,


                 from hardware\libraries\DS1621/DS1621.h:11,

Is this the same error that is something to do with the bytecasting in Ard-012? I'm using Ard-013 and its supposed to have fixed that?

And the DS1621.h:
Code:
/*
      DS1621.h - Library for DS1621 Temp Sensor
*/

// ensure this library description is only included once
#ifndef DS1621_h
#define DS1621_h

// include types & constants of Wiring core API
#include <WConstants.h>
#include <wprogram.h>
// include types & constants of Wire ic2 lib
#include <../Wire/Wire.h>

// device ID and address

#define DEV_TYPE   0x90 >> 1                    // shift required by wire.h
#define DEV_ADDR   0x00                         // DS1621 address is 0
#define SLAVE_ID   DEV_TYPE | DEV_ADDR

// DS1621 Registers & Commands

#define RD_TEMP    0xAA                         // read temperature register
#define ACCESS_TH  0xA1                         // access high temperature register
#define ACCESS_TL  0xA2                         // access low temperature register
#define ACCESS_CFG 0xAC                         // access configuration register
#define RD_CNTR    0xA8                         // read counter register
#define RD_SLOPE   0xA9                         // read slope register
#define START_CNV  0xEE                         // start temperature conversion
#define STOP_CNV   0X22                         // stop temperature conversion

// DS1621 configuration bits

#define DONE       B10000000                    // conversion is done
#define THF        B01000000                    // high temp flag
#define TLF        B00100000                    // low temp flag
#define NVB        B00010000                    // non-volatile memory is busy
#define POL        B00000010                    // output polarity (1 = high, 0 = low)
#define ONE_SHOT   B00000001                    // 1 = one conversion; 0 = continuous conversion

// library interface description
class DS1621
{
  // user-accessible "public" interface
  public:
    DS1621();
            void setConfig(byte cfg);
            byte getReg(byte reg);
            void setThresh(byte reg, int tC);
            void startConversion(boolean start);
            int getTemp(byte reg);
            int getHrTemp();
            
  // library-accessible "private" interface
  private:
    
};

extern DS1621 TEMP;

#endif

and DS1621.cpp :

Code:
extern "C" {
#include <../Wire/Wire.h>
}
#include "DS1621.h"

DS1621::DS1621()
{
  Wire.begin();
}

DS1621 TEMP=DS1621();

//Private Functions:


//Public Functions

void DS1621::setConfig(byte cfg)   // Aquire data from buffer and convert to int, refresh buffer if required
{
      Wire.beginTransmission(SLAVE_ID);
      Wire.send(ACCESS_CFG);
      Wire.send(cfg);
      Wire.endTransmission();
      delay(15);
}

byte DS1621::getReg(byte reg)
{
 Wire.beginTransmission(SLAVE_ID);
 Wire.send(reg);                               // set register to read
 Wire.endTransmission();
 Wire.requestFrom(SLAVE_ID, 1);
 byte regVal = Wire.receive();
 return regVal;
}


// Sets temperature threshold
// -- whole degrees C only
// -- works only with ACCESS_TL and ACCESS_TH

void DS1621::setThresh(byte reg, int tC)
{
 if (reg == ACCESS_TL || reg == ACCESS_TH) {
   Wire.beginTransmission(SLAVE_ID);
   Wire.send(reg);                             // select temperature reg
   Wire.send(byte(tC));                        // set threshold
   Wire.send(0);                               // clear fractional bit
   Wire.endTransmission();
   delay(15);
 }
}


// Start/Stop DS1621 temperature conversion

void DS1621::startConversion(boolean start)
{
 Wire.beginTransmission(SLAVE_ID);
 if (start == true)
   Wire.send(START_CNV);
 else
   Wire.send(STOP_CNV);
 Wire.endTransmission();
}


// Reads temperature or threshold
// -- whole degrees C only
// -- works only with RD_TEMP, ACCESS_TL, and ACCESS_TH

int DS1621::getTemp(byte reg)
{
 int tC;

 if (reg == RD_TEMP || reg == ACCESS_TL || reg == ACCESS_TH) {
   byte tVal = getReg(reg);
   if (tVal >= B10000000) {                    // negative?
     tC = 0xFF00 | tVal;                       // extend sign bits
   }
   else {
     tC = tVal;
   }
   return tC;                                  // return threshold
 }
 return 0;                                     // bad reg, return 0
}


// Read high resolution temperature
// -- returns temperature in 1/100ths degrees
// -- DS1620 must be in 1-shot mode

int DS1621::getHrTemp()
{
 startConversion(true);                        // initiate conversion
 byte cfg = 0;
 while (cfg < DONE) {                          // let it finish
   cfg = getReg(ACCESS_CFG);
 }

 int tHR = getTemp(RD_TEMP);                   // get whole degrees reading
 byte cRem = getReg(RD_CNTR);                  // get counts remaining
 byte slope = getReg(RD_SLOPE);                // get counts per degree

 if (tHR >= 0)
   tHR = (tHR * 100 - 25) + ((slope - cRem) * 100 / slope);
 else {
   tHR = -tHR;
   tHR = (25 - tHR * 100) + ((slope - cRem) * 100 / slope);
 }
 return tHR;
}

Now its possible i'm doing it competely wrong, because i dont really get whats going on. I've just tried to use code that works in the format of other libraries that work (specifically the DS1307 one someone here wrote). Any ideas/Help would be gratefully recieved.

Harry
17  Forum 2005-2010 (read only) / Interfacing / Re: Parse data in Python on: November 24, 2010, 12:57:33 pm
Thanks, I'll give that a go and see how i get on!
18  Forum 2005-2010 (read only) / Interfacing / Parse data in Python on: November 24, 2010, 07:17:39 am
Hi,

I've got project which needs to take input from a set of switches (keypad) on an arduino, send the keypress to my windows box where it will be read, probably by python, and then a keystroke sent to the currently active window. It is going to be a way to control a carpc with the buttons from a standard head unit. I've got the circuit and connections sorted, and I can send the data, but i would like some advice on the best way to implement the serial.

Is Python going to be the easiest and least resource hungry way to do this? Can anyone offer any sample code on how to go about parsing a simple serial protocol? I've done some googling but i cant really find what i'm after.

The protocol only needs to be simple, something like Startbit, key pressed, end bit, maybe '@[keypressed]#'.

Thanks for your help!
19  Forum 2005-2010 (read only) / Interfacing / Arduino + Python to read input and output keypress on: September 28, 2010, 10:15:20 am
Afternoon.

I've been working on some arduino/python code to read a rotary encoder and a few buttons, when a button is pressed send a message to a python script and in turn output a keystroke to whichever program has the focus. this is where I am so far...

Arduino19 Code:

Quote
#include <RotaryEncoder.h>
#include <EEPROM.h>
#include <Button.h>

//PIN Defines
#define PIN_ENC_A    11
#define PIN_ENC_B    12
#define PIN_ENC_BTN  16
#define PIN_BTN_1    3
#define PIN_BTN_2    4
#define PIN_BTN_3    5
#define PIN_BTN_4    6
#define PIN_BTN_5    7
#define PIN_BTN_6    8
#define PIN_BTN_7    9
#define PIN_BTN_8    13
#define PIN_BTN_9    14
#define PIN_HBT      13

#define ADDR_BTN_1    10
#define ADDR_BTN_2    20
#define ADDR_BTN_3    30
#define ADDR_BTN_4    40
#define ADDR_BTN_5    50
#define ADDR_BTN_6    60
#define ADDR_BTN_7    70
#define ADDR_BTN_8    80
#define ADDR_BTN_9    90
#define ADDR_ENC_CW   100
#define ADDR_ENC_CCW 120
//Constructors
RotaryEncoder enc(PIN_ENC_A,PIN_ENC_B,PIN_ENC_BTN);
Button btn_1 = Button(PIN_BTN_1,PULLUP);
Button btn_2 = Button(PIN_BTN_2,PULLUP);
Button btn_3 = Button(PIN_BTN_3,PULLUP);
Button btn_4 = Button(PIN_BTN_4,PULLUP);
Button btn_5 = Button(PIN_BTN_5,PULLUP);
Button btn_6 = Button(PIN_BTN_6,PULLUP);
Button btn_7 = Button(PIN_BTN_7,PULLUP);
Button btn_8 = Button(PIN_BTN_8,PULLUP);
Button btn_9 = Button(PIN_BTN_9,PULLUP);

//Global Var
int encPos = 0;
int encNewPos = 0;

void setup()
{
  //Set Buttons to Inputs
  pinMode(PIN_ENC_A, INPUT);
  pinMode(PIN_ENC_B, INPUT);
  pinMode(PIN_ENC_BTN, INPUT);
  pinMode(PIN_BTN_1, INPUT);
  pinMode(PIN_BTN_2, INPUT);
  pinMode(PIN_BTN_3, INPUT);
  pinMode(PIN_BTN_4, INPUT);
  pinMode(PIN_BTN_5, INPUT);
  pinMode(PIN_BTN_6, INPUT);
  pinMode(PIN_BTN_7, INPUT);
  pinMode(PIN_BTN_8, INPUT);
  pinMode(PIN_BTN_9, INPUT);
  pinMode(PIN_HBT, OUTPUT);
  //Enable in-chip PullUp resistors
  digitalWrite(PIN_BTN_1 , INPUT);
  digitalWrite(PIN_BTN_2 , INPUT);
  digitalWrite(PIN_BTN_3 , INPUT);
  digitalWrite(PIN_BTN_4 , INPUT);
  digitalWrite(PIN_BTN_5 , INPUT);
  digitalWrite(PIN_BTN_6 , INPUT);
  digitalWrite(PIN_BTN_7 , INPUT);
  digitalWrite(PIN_BTN_8 , INPUT);
  digitalWrite(PIN_BTN_9 , INPUT);

  Serial.begin(9600);
//Serial.println("START");
}

void loop()
{
  //read Encoder
  encNewPos = enc.position();
  if (encNewPos != encPos)
  {
    if (encNewPos < encPos)
    {
      SendKeyStroke(ADDR_ENC_CCW); 
  
  }

    if (encNewPos > encPos)
    {
      SendKeyStroke(ADDR_ENC_CW); 
    }
    
    encPos = encNewPos;
    //read Keypad


  }
}

void SendKeyStroke(int c)
{
  Serial.print(EEPROM.read(c));
}


and the python3.1.2 code:

Quote
import serial

port = "COM2"
ser = serial.Serial(port,9600)
value = 0

while 1:
    value = ser.read();
    print(type(value))
    print(value)
    strValue = value.decode("utf-8")
    if strValue == "m":
        print("CCW")
    elif strValue == "k":
        print("CW")
    #elif strValue != "m" or strValue != "k":
        #print("Value not recognised")
    else:
        continue

This is my first look at python, and in one evening this is what i've come up with. Now the problems...

I've only implemented the rotary encoder so far, and it seems to work ok but i was wondering if there was a better way to handle the reading of the serial input? it seems a bit long winded and will end up with a whole lot of 'if.. elif' code when i get round to implementing the next 9 buttons.

Also, what would be the best way to code in an escape sequence to handle closing the serial port if the script is terminated?

Not looking for the answers, just a point in the right direction! Many thanks in advance!



20  Forum 2005-2010 (read only) / Interfacing / Re: Get Ipod Name (receiving data from Ipod) on: February 07, 2009, 04:02:00 pm
Are you aware that you need a 500ohm resistor on the Serial Enable pin of the ipod connector to gnd? I think there is a post on here somewhere with some code that works, but it only does play, FF RW, Pause and menu.
21  Forum 2005-2010 (read only) / Development / Re: AVRProjectIDE - Editor for AVR/Arduino Projects on: January 12, 2011, 03:16:55 pm
Hi

I have logged an issue detailing what changes i have done. So far I have got it working with Attiny2313 core and attiny84 core. I had a look at multiple processor cores, one for the attiny45/85 and one for many different chips, but i couldnt get these to work. Maybe something to do with how each chip is defined?

Also i noticed that if i compile a simple sketch (no libraries) it will compile and run fine, even serial library worked on 2313, but if i added any other library such as liquid crystal it would bin out saying it couldn't find wire.h despite it being in the library folder. any idea? haven't really investigated yet.

Also, is there a way to set the path of the core you want to use with the templates.xml file? It'll cut down one step of setting up the project.

22  Forum 2005-2010 (read only) / Development / Re: AVRProjectIDE - Editor for AVR/Arduino Projects on: January 10, 2011, 12:04:07 pm
Hi,

First off, well done! this is a cracking editor for windows (no doubt the open source crowd will start moaning now smiley-razz)

Im interested in using this editor for attinys using custom arduino cores, is it going to be as simple as creating a new template in the templates xml file and specifying a custom arduino core in the config?

Many thanks for all your hard work so far, i'd love to contribute if theres anything i can help with.

23  Forum 2005-2010 (read only) / Development / Re: .net based Arduino Sketch Editor on: May 20, 2009, 05:25:50 pm
When using visual basic (years ago, but getting back into it) i really like the popup menu with suggestions for what you are trying to write. Is this what i can see here? Looks very good anyway, keep us updated!

24  Forum 2005-2010 (read only) / Development / Re: DS1621 Library as learning experience on: April 15, 2009, 12:37:14 pm
Yeah I put tem in just to find out where the program was stalling. They'll come out soon. I also hope to add alot more functionality to it. Thanks for the tips!
25  Forum 2005-2010 (read only) / Development / DS1621 Library as learning experience on: April 15, 2009, 12:18:44 pm
Hi,

I'm trying to create a library for the DS1621 I2C Temperature sensor as a learning experience to teach myself how to make libraries.

So far its gone quite well, however i've got a few issues.

Even though i've got #include <../Wire/Wire.h> in DS1621.h i still have to put it in the main sketch to get it to work. why?

Also i'm having trouble with the initialisation of the chip. It seems to be hanging at the Wire.endTransmission();. i've verified this by adding a serial print out of when it passes every line. I can comment this line out and it still seems to work, but does anyone know why this might be happening?

I've used code already on the forum as a basis to get this working and verified it does work as a basic sketch.

Heres the Sketch i'm using:
Quote

 
#include <DS1621.h>
#include <Wire.h>

DS1621 DS1621();

void setup() {
  Serial.begin(9600);
  Serial.print("Start");
}

void loop() {
  Serial.print("T= ");
  int tempC =  THERM.getTemp();
  Serial.println(tempC);
  delay(1000);
}


 

heres the DS1621.cpp:

Quote

 
/*       DS1621 I2C Thermometer Library
     DS1621.CPP V1.1
     14 April 2009
*/

extern "C" {

#include <../Wire/Wire.h>
}
#include "DS1621.h"
#define DEV_ID 0x90 >> 1                    // shift required by wire.h


DS1621 THERM=DS1621();
DS1621::DS1621()
{
      Serial.begin(9600);
      Serial.println("Start Library Init");
      Wire.begin();
      Serial.println("Wire.begin();");
      Wire.beginTransmission(DEV_ID);           // connect to DS1621 (#0)
      Serial.println("Wire.beginTransmission(DEV_ID);");
      Wire.send(0xAC);                          // Access Config
      Serial.println("Wire.send(0xAC);");
      Wire.send(0x02);                          // set for continuous conversion
      Serial.println("Wire.send(0x02);");
      Wire.beginTransmission(DEV_ID);           // restart
      Serial.println("Wire.beginTransmission(DEV_ID);");
      Wire.send(0xEE);                          // start conversions
      Serial.println("Wire.send(0xEE);");
      //Wire.endTransmission();
      //Serial.println("Wire.endTransmission();");
      Serial.println("End Library Init");
}



int DS1621::getTemp() {
      Serial.println("getTemp() Start");
      int tempC = 0;

      delay(1000); //Time for measurement
      Wire.beginTransmission(DEV_ID);
      Wire.send(0xAA);                          // read temperature
      Wire.endTransmission();
      Wire.requestFrom(DEV_ID, 1);              // request one byte from DS1621
      tempC = Wire.receive();                   // get whole degrees reading
      Serial.print("Get Temp Finished. Temp = ");
      Serial.println(tempC);
      return(tempC);
}





 

and DS1621.h:

Quote

 
/*       DS1621 I2C Thermometer Library
     DS1621.H V1.1
     14 April 2009
*/

// ensure this library description is only included once
#ifndef DS1621_h
#define DS1621_h

// include types & constants of Wiring core API
#include <WProgram.h>

// include types & constants of Wire ic2 lib
#include <../Wire/Wire.h>


class DS1621
{
  public:
  DS1621();
  int getTemp();
};
extern DS1621 THERM;
#endif




 

This is still in its infancy, If anyone has any tips for better code/library creation i'd be happy to learn!  Thanks all!
26  Forum 2005-2010 (read only) / Development / Non class type error on: April 16, 2009, 02:03:59 pm
Hi,

Im getting this error when i try and compile a library i've written. Can anyone shed any light on it?


error:
Code:
In function 'void loop()':
error: request for member 'getButton' in 'Nunchuck', which is of non-class type 'Nunchuck ()()'

.h:

Code:
// ensure this library description is only included once
#ifndef Nunchuck_h
#define Nunchuck_h

// include types & constants of Wiring core API
#include <WProgram.h>

// include types & constants of Wire ic2 lib
#include <../Wire/Wire.h>



class Nunchuck
{
  public:
      void nunchuck();
      int getButton(char button);
      
      
  private:
      void Send(byte Value);
      void initialize();
      byte nunchuckDecodeByte(byte x);
      int outBuffer[6];
      int cnt;
      int z_button;
      int c_button;
};

extern Nunchuck Wii;
#endif

sketch:

Code:
#include <Wire.h>
#include <Nunchuck.h>

Nunchuck Nunchuck();

void setup() {
Serial.begin(9600);  
}

void loop() {
  int button_value = Nunchuck.getButton("c");
  Serial.print("ButtonC: ");
  
  Serial.println(button_value);
  delay(100);
}

Thanks!
27  Forum 2005-2010 (read only) / Development / Re: ATTiny85 port (work in progress) on: September 29, 2010, 10:16:33 am
Turns out it was easier than i thought.

Because the ATtiny45 only has one timer the register is called TIMSK not TIMSK0. Therefore, if you edit the end of NewSoftSerial.cpp to look like this:

Quote
#if defined(__AVR_ATtiny45__)
    sbi(TIMSK, TOIE0);
#else
    sbi(TIMSK0, TOIE0);
#endif
  else
#if defined(__AVR_ATtiny45__)
    cbi(TIMSK, TOIE0);
#else
    cbi(TIMSK0, TOIE0);
#endif

and recompile it will work. I think you'll probably have to have the 45/85 running at atleast 8MHz though.
28  Forum 2005-2010 (read only) / Development / Re: ATTiny85 port (work in progress) on: September 29, 2010, 09:57:46 am
Hi,

I'm trying to get a tiny45 and NSSerial working, i'm getting an error relating to timer0, i assume its because the tiny45 either doesnt have a timer0 or its called something else (it seems to be looking for TIMSK0).

Quote
C:\Program Files (x86)\arduino-0019\libraries\NewSoftSerial\NewSoftSerial.cpp: In static member function 'static void NewSoftSerial::enable_timer0(bool)':
C:\Program Files (x86)\arduino-0019\libraries\NewSoftSerial\NewSoftSerial.cpp:520: error: 'TIMSK0' was not declared in this scope
C:\Program Files (x86)\arduino-0019\libraries\NewSoftSerial\NewSoftSerial.cpp:526: error: 'TIMSK0' was not declared in this scope

I'm going to compare datasheets now and see what i can find.
29  Forum 2005-2010 (read only) / Development / Attiny84 board on: May 21, 2009, 07:05:18 am
Hi all,

I want to use the luminet IDE with the Attiny84 so i've put together a board for use on breadboard. Its the same width as an atmega32 (dip40), and has a voltage reg to 3.3v, pin headers for all pins and a programming connector. I havent put on a header for the ftdi cable yet, next revision!

Can someone look at it and tell me if i've made any glaring ommisions or errors before i have it fabbed up?

Thanks!
http://www.flickr.com/photos/38604845@N02/3551444552/in/photostream/

http://www.flickr.com/photos/38604845@N02/3551444552/in/photostream/
30  Forum 2005-2010 (read only) / Interfacing / Re: Arduino Addict (SMALL CNC Rounter attempt) on: December 30, 2008, 11:50:46 am
Would looking at the rep-rap project offer some inspiration? surely what they can do in 3-d can be turned into a 2-d version?
Pages: 1 [2] 3 4