Show Posts
Pages: 1 [2] 3 4 5
16  Using Arduino / Networking, Protocols, and Devices / RS232 reading problems on: August 22, 2012, 10:53:07 am
Hello forum, I am trying to interface an Arduino Mega 2560 to another board (where an ELM232 chip lives) via serial.

To troubleshoot, I have connected as follows on a breadboard:

ELM232 board's Tx to:
  • PC via USB to serial adaptor.
  • Arduino's Rx3.

All share a common ground.

When powering up the ELM232, it sends the following string:

ELM323 v2.0

>


I see this perfectly well on the PC, using a terminal program at 9600 8N1.

The Mega, however, spits out complete garbage. The source code I use is here:

Quote
#define ELM_BAUD 9600


int buffer[50];

void setup() {

  Serial.begin(9600);
  Serial3.begin(ELM_BAUD);


}

void loop() {
  
  int c;
  
  // We will let a long time pass between the incoming data burst to make 100% sure no timing problems occur
  if (Serial3.available() > 0) {
    delay(1000);                  // twiddle thumbs for a second to make sure burst is over
    int index = 0;                // this will count our incoming bytes
    
    while ( (c = Serial3.read()) != -1) buffer[index++] = c;    // fetch the received bytes
    
    Serial.print("Read these ");
    Serial.print(index, DEC);
    Serial.println(" characters:");
    
    
    for (int i=0; i < index; i++) {
      Serial.write(buffer);            // print as a byte
      Serial.print(":");
      Serial.println(buffer, DEC);    // print ASCII value
    }
    
    
  }
  
}

The result I 'm getting on the Arduino IDE's terminal is:

Read these 18 characters:
y:121
½:189
×:215
V:86
V:86
¶:182
–:150
ö:246
›:155
›:155
£:163
Ÿ:159
å:229
ë:235
å:229
ë:235
ƒ:131
 :0


I 'm completely stumped. I 've tried all the Mega's available UARTs but it's the same everywhere. Any idea what is happening here?

P.S. The characters are indeed 18 and in Hex they are these (copy pasted from the PC terminal shown in hex, including linefeeds and carriage returns):

0d 0a 45 4c 4d 33 32 33   20 76 32 2e 30 0d 0a 0d
0a 3e


Out of desperation, I have tried other baud rates (by changing the #define ELM_BAUD ... in the code) but, unsurprisingly, only 9600 gets the correct number of bytes.
17  Using Arduino / LEDs and Multiplexing / Re: High current LED's on: August 22, 2012, 10:22:56 am
Check this out:

http://www.instructables.com/id/Circuits-for-using-High-Power-LED-s/?ALLSTEPS

(step 8 ).

I 've tried it out with an Arduino and works quite well.
18  Using Arduino / Programming Questions / Re: Passing arrays to classes on: August 05, 2012, 07:19:04 pm
It's trying to make "a1d" refer to the same thing in "classy" as "myArray" refers to in setup() so that a1d[m][n] in "classy" will fetch me the same result (by reading the same address) as myArray[m][n] would in setup().

In the grander scheme of things, when this class grows up, it will be used to play tunes on a speaker. The object is pointed towards an array of notes and durations and handles the rest.

So far it's working with no obvious bugs but if you see any problems I 'd be grateful if you 'd point them out.
19  Using Arduino / Programming Questions / Re: Passing arrays to classes on: August 05, 2012, 03:42:26 pm
This is great, thank you.

I certainly don't want the array copied, I just want the object to have the address of the original array so it can access its elements.

One thing I 'd like to add for any poor guys struggling with similar. When stepping this up to 2D arrays, it gets slightly tricky due to operator precedence of * and []. The way to do it is this:

Quote
class classy {
  
  public:
  
  int (*a1d)[5];                      // mind the ()
  
  classy(int passedArray[][5]) {
    a1d = passedArray;
    
    Serial.print("CONSTRUCTOR:\nElement at [1][3] of passed array is ");
    Serial.println(a1d[1][3]);
    Serial.println("Now setting it to -1\n--------------");
    a1d[1][3] = -1;
  }
  
};



void setup() {
  
  
  Serial.begin(57600);
  
  int myArray[][5] = {{ 0, 1, 2, 3, 4 }, { 10, 11, 12, 13, 14 }};
  
  classy c = classy(myArray);
  
  Serial.print("SETUP():\nElement at [1][3] of original array is ");
  Serial.println(myArray
  [1][3]);
  
}

void loop() {}

20  Using Arduino / Programming Questions / Passing arrays to classes on: August 05, 2012, 01:51:22 pm
Hello forum, I have what I think is a simple question that is causing me unexpectedly large headaches.

I want to pass an array to a class. In my naive mind, an array is passed by passing (by value) a pointer to its first element. So, the idea goes, the elements of an array declared in a different scope, will be accessible to the methods of the class. The code below explodes:



Quote
class classy {
  
  public:
  
  int a1d[];                      // this should end up pointing to the array myArray in setup()
  
  classy(int passedArray[]) {
    a1d = passedArray;            // this is the line that blows up

  }
  
};



void setup() {
  
  int myArray[5] = { 0, 1, 2, 3, 4 };
  
  classy c = classy(myArray);
  
}

void loop() {}



So, I need to make it so that a method in classy referring to, say, a1d[2] will access myArray[2].

However, I get the following error:

sketch_aug05c.cpp: In constructor ‘classy::classy(int*)’:
sketch_aug05c:7: error: incompatible types in assignment of ‘int*’ to ‘int [ 0 ]’


So, if I got that right, passedArray[] is considered an integer pointer (‘int*’, which is what I think a reference to an array is. Note that the constructor is referred to as ‘classy::classy(int*)’ whereas I have declared it as ‘classy(int passedArray[]’), which  I guess amounts to the same thing) but a1d is considered the contents of the first element of an integer array (‘int [ 0 ]’)? Both are referred to in exactly the same way in the offending line. Am I going crazy or is the compiler linguistically challenged?
21  Using Arduino / Storage / Re: Problem powering OpenLog on Mega 2560 on: December 03, 2011, 01:36:10 pm
Yes, that's what I meant by "burst of data comes through". In the mininalist version, I only connect Vcc, GND and Rx on the OpenLog. When sending data on the Rx, there is always a noticeable downwards fluctuation in the voltage (I measure across Vcc and GND) no matter where GND is connected. However, if it's connected to the Mega's ground, the blue LED flashes and the data actually gets written, whereas if I have it connected to a generic pin set to OUTPUT/LOW, no data gets written.
22  Using Arduino / Storage / Problem powering OpenLog on Mega 2560 on: December 02, 2011, 08:12:56 pm
Hello forum, I have encountered strange behaviour using an OpenLog module with a Mega 2560.

OpenLog has the following pins:

GRN (pull down to reset)
RX
TX
Vcc
GND
BLK

For simple use, I can get it working on the breadboard by connecting Vcc, Ground and its Rx to the board's Tx and it happily logs.

Now, I want to take it off the breadboard and stick it directly on the board. For this, I need 6 pins in a row, preferably with an existing hardware UART among them. A good choice is Serial2, which is padded with pins on both sides which I thought I 'd use for power. So I did the following connection:

GRN -------> 15 (RX3) ignored (defaults to INPUT, high impedance)
RX ---------> 16 (TX2) enabled by firing up Serial2.begin()
TX ---------> 17 (RX2) enabled by firing up Serial2.begin()
Vcc --------> 18 (TX1) pinMode(OUTPUT) and digitalWrite(HIGH)
GND ------> 19 (RX1) pinMode(OUTPUT) and digitalWrite(LOW)
BLK -------> 20 (SDA) ignored (defaults to INPUT, high impedance)

Now, trying to write to Serial2, does not result in writing to the card. When writing successfully, the right LED flashes blue but I do not get that. What I do get are green flashes on the left LED.

Now, the funny thing. If I disconnect the GND from pin19 and connect it to the Mega's ground, everything works perfectly!

I have tried checking out what is wrong with a multimetre but there's nothing I can see that is out of the ordinary, both pin19 and the board's ground pin (in conjunction with pin 18 being set HIGH) seem to power the OpenLog board with around 4.7 Volts. In both cases, there is a small drop as a burst of data comes through. I also tried using some other random pin (pin 22 which is next door) to use as a sink but got the same problem. For some obscure reason, the only ground that the OpenLog board will accept is the Mega's ground.

Surely it can't be a question of trying to sink too much current, according to this page, maximum current consumption is a laughable 6mA.

Anyone got any ideas? I 'm really puzzled by this whole thing.
23  Using Arduino / Programming Questions / Re: Stalker V2 Help!! Set the time? RFID Logger on: August 04, 2011, 12:02:19 pm
Argh, darn brackets  smiley-razz

Thanks a lot, you saved me a lot of misery.
24  Using Arduino / Programming Questions / Re: Stalker V2 Help!! Set the time? RFID Logger on: August 04, 2011, 10:52:40 am
Thank you very much mem, I was about to try adding some function to the RX8025 library that returns time_t so I could feed it to the Time library's setSyncProvider() but thankfully your library will save me from getting mauled by the compiler.

I just tried your code and it initialises the RX8025 fine, as well as reading from it. However, the Time library doesn't seem to like its get() for some bizarre reason. Here is my code:

Quote
#include <RTC.h>
#include <Time.h>
#include <Wire.h>
 
void setup() {
  
  Serial.begin(9600);
  if (RTC.begin()) Serial.println("Valid RTC device initialised");
  else Serial.println("Problem initialising RTC");
  
  Serial.println("Testing the two flavours of the RTC.get() function (should return time_t):");
  Serial.print(RTC.get());
  Serial.print(" : ");
  Serial.println(RTCget());
  
  //setSyncProvider(RTCget());
  //setSyncProvider(RTC.get());
  
}
 
void loop() {
}

If I uncomment either line:

setSyncProvider(RTCget());
or
setSyncProvider(RTC.get());


I get the following error:

error: invalid conversion from ‘time_t’ to ‘time_t (*)()’

which seems strange as, if you look at the DS1307RTC library (which works fine with the Time library) the get() is declared in the DS1307RTC.h (line 17) as follows:

static time_t get();


So, the expected return type is time_t. No idea, how the static is breaking things. Could you please post some sample code of registering your lib's get() function as a sync provider?
25  Using Arduino / Programming Questions / Re: Stalker V2 Help!! Set the time? RFID Logger on: July 27, 2011, 05:10:35 pm
DS1307RTC.cpp:

Quote
/*
 * DS1307RTC.h - library for DS1307 RTC
  
  Copyright (c) Michael Margolis 2009
  This library is intended to be uses with Arduino Time.h library functions

  The 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
  
  30 Dec 2009 - Initial release
 */

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

#define DS1307_CTRL_ID 0x68 

DS1307RTC::DS1307RTC()
{
  Wire.begin();
}
  
// PUBLIC FUNCTIONS
time_t DS1307RTC::get()   // Aquire data from buffer and convert to time_t
{
  tmElements_t tm;
  read(tm);
  return(makeTime(tm));
}

void  DS1307RTC::set(time_t t)
{
  tmElements_t tm;
  breakTime(t, tm);
  tm.Second |= 0x80;  // stop the clock
  write(tm);
  tm.Second &= 0x7f;  // start the clock
  write(tm);
}

// Aquire data from the RTC chip in BCD format
void DS1307RTC::read( tmElements_t &tm)
{
  Wire.beginTransmission(DS1307_CTRL_ID);
  Wire.send(0x00);
  Wire.endTransmission();

  // request the 7 data fields   (secs, min, hr, dow, date, mth, yr)
  Wire.requestFrom(DS1307_CTRL_ID, tmNbrFields);
  
  tm.Second = bcd2dec(Wire.receive() & 0x7f);   
  tm.Minute = bcd2dec(Wire.receive() );
  tm.Hour =   bcd2dec(Wire.receive() & 0x3f);  // mask assumes 24hr clock
  tm.Wday = bcd2dec(Wire.receive() );
  tm.Day = bcd2dec(Wire.receive() );
  tm.Month = bcd2dec(Wire.receive() );
  tm.Year = y2kYearToTm((bcd2dec(Wire.receive())));
}

void DS1307RTC::write(tmElements_t &tm)
{
  Wire.beginTransmission(DS1307_CTRL_ID);
  Wire.send(0x00); // reset register pointer
  
  Wire.send(dec2bcd(tm.Second)) ;   
  Wire.send(dec2bcd(tm.Minute));
  Wire.send(dec2bcd(tm.Hour));      // sets 24 hour format
  Wire.send(dec2bcd(tm.Wday));   
  Wire.send(dec2bcd(tm.Day));
  Wire.send(dec2bcd(tm.Month));
  Wire.send(dec2bcd(tmYearToY2k(tm.Year)));   

  Wire.endTransmission(); 
}
// PRIVATE FUNCTIONS

// Convert Decimal to Binary Coded Decimal (BCD)
uint8_t DS1307RTC::dec2bcd(uint8_t num)
{
  return ((num/10 * 16) + (num % 10));
}

// Convert Binary Coded Decimal (BCD) to Decimal
uint8_t DS1307RTC::bcd2dec(uint8_t num)
{
  return ((num/16 * 10) + (num % 16));
}

DS1307RTC RTC = DS1307RTC(); // create an instance for the user


26  Using Arduino / Programming Questions / Re: Stalker V2 Help!! Set the time? RFID Logger on: July 27, 2011, 05:09:56 pm
The plot thickens. The RTC behaves itself when using the RX8025 library. Here is a very stripped down version of the example source code linked in the Stalker wiki page.

Quote
#include "RX8025.h"
#include <Wire.h>

unsigned char hour=0;
unsigned char minute=0;
unsigned char second=0;
unsigned char week=0;
unsigned char year=0;
unsigned char month=0;
unsigned char date=0;

unsigned char RX8025_time[7]=
{
  0x00,0x54,0x09,0x03,0x10,0x11,0x10 //second, minute, hour, week, date, month, year, BCD format
};


//======================================
void setup(void)
{
  Serial.begin(9600);
  RX8025_init();
}
//======================================
void loop(void)
{
  getRtcTime();
  print_RX8025_time();
  Serial.println("--------------next--data---------------");
  delay(2000);
}


//==============================================
void print_RX8025_time(void)
{
  Serial.print(year,DEC);
  Serial.print("/");
  Serial.print(month,DEC);
  Serial.print("/");
  Serial.print(date,DEC);
  switch(week)
  {
  case 0x00:
    {
      Serial.print("/Sunday  ");  
      break;
    }
  case 0x01:
    {
      Serial.print("/Monday  ");
      break;
    }
  case 0x02:
    {
      Serial.print("/Tuesday  ");
      break;
    }
  case 0x03:
    {
      Serial.print("/Wednesday  ");
      break;
    }
  case 0x04:
    {
      Serial.print("/Thursday  ");
      break;
    }
  case 0x05:
    {
      Serial.print("/Friday  ");
      break;
    }
  case 0x06:
    {
      Serial.print("/Saturday  ");
      break;
    }
  }
  Serial.print(hour,DEC);
  Serial.print(":");
  Serial.print(minute,DEC);
  Serial.print(":");
  Serial.println(second,DEC);
}


Pressing the reset button with this code loaded does not reset the RTC, unlike in the case of the DS1307RTC and Time libs, used in the "TimeRTCSet" example. Reading through the code of the RX8025 and DS1307RTC libs, I cannot for the life of me figure out what the offending difference might be. Even worse, I 'd have thought DS1307RTC shouldn't work at all since it seems to think the address of the RTC is 0x68 (#define DS1307_CTRL_ID 0x68 in DS1307RTC.cpp below) whereas the RX8025 suggests the RTC lives at number 0x32 (#define  RX8025_address  0x32 RX8025.cpp below).

DS1307RTC has what looks like a constructor whose only statement is to fire up the Wire comms:
Code:
DS1307RTC::DS1307RTC()
{
  Wire.begin();
}

whereas RX8025 has function RX8025_init() which both fires up Wire comms as well as sending 0xe0 to the RTC, followed by 0x20 and 0x00. I don't know what these characters are actually doing but, for some reason, DS1307RTC seems to be communicating OK with something, even if it's at another address than whatever RX8025 is talking to and without having sent these characters.

When it comes to reading time, both seem to be doing similar things. They send 0x00 and ask for some chars. However, notice a difference, in function void DS1307RTC::read( tmElements_t &tm), DS1307RTC seemingly asks for 7 values (assuming tmNbrFields == 7 as the comment suggests) whereas in function void getRtcTime(void) RX8025 asks for 8 values and discards the first one! WTF? Full code of RX8025.cpp and DS1307RTC follow.

RX8025.cpp:

Quote
#include <Wire.h>
#include "RX8025.h"
//********************************************************************

//===============================================
#define  RX8025_address  0x32

unsigned char RX8025_Control[2]=
{
  0x20,0x00
};

//===============================================
void setRtcTime(void)
{
  Wire.beginTransmission(RX8025_address);
  Wire.send(0x00);
  for(unsigned char i=0; i<7; i++)
  {
    Wire.send(RX8025_time);
  }
  Wire.endTransmission();
}

//===============================================
uint8_t bcd2bin (uint8_t val)
{
  return val - 6 * (val >> 4);
}

uint8_t bin2bcd (uint8_t val)
{
  return val + 6 * (val / 10);
}

//===============================================
void getRtcTime(void)
{
  unsigned char i=0;
  Wire.beginTransmission(RX8025_address);
  Wire.send(0x00);
  Wire.endTransmission();//
  Wire.requestFrom(RX8025_address,8);
  RX8025_time= Wire.receive();//not use
  while(Wire.available())
  {
    RX8025_time= Wire.receive();
    i++;
  }
  Wire.endTransmission();//

  year   = bcd2bin(RX8025_time[6]&0xff);
  month  = bcd2bin(RX8025_time[5]&0x1f);
  date   = bcd2bin(RX8025_time[4]&0x3f);
  week   = bcd2bin(RX8025_time[3]&0x07);
  hour   = bcd2bin(RX8025_time[2]&0x3f);
  minute = bcd2bin(RX8025_time[1]&0x7f);
  second = bcd2bin(RX8025_time[0]&0x7f);
}

//===============================================
void RX8025_init(void)
{
  Wire.begin();
  Wire.beginTransmission(RX8025_address);//clear power on reset flag, set to 24hr format
  Wire.send(0xe0);
  for(unsigned char i=0; i<2; i++)
  {
    Wire.send(RX8025_Control);
  }
  Wire.endTransmission();
  //setRtcTime();
}

27  Using Arduino / Programming Questions / Re: Stalker V2 Help!! Set the time? RFID Logger on: July 27, 2011, 03:35:20 am
The Stalker v2.0 doesn't have a battery but a super capacitor for RTC backup (see here and here).

I presume that the capacitor should charge immediately as soon as the board is powered up and keep the RTC ticking between resets.
28  Using Arduino / Programming Questions / Re: Stalker V2 Help!! Set the time? RFID Logger on: July 26, 2011, 06:04:54 pm
I don't want to clutter the thread with duplicate code since the two links in my original post both lead to code and the TimeRTCSet example is part of the Time library. Here it is:

Quote
/*
 * TimeRTCSet.pde
 * example code illustrating Time library with Real Time Clock.
 *
 * RTC clock is set in response to serial port time message 
 * A Processing example sketch to set the time is inclided in the download
 */

#include <Time.h>  
#include <Wire.h> 
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t


void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");     
}

void loop()
{
  if(Serial.available())
  {
     time_t t = processSyncMessage();
     if(t >0)
     {
        RTC.set(t);   // set the RTC and the system time to the received value
        setTime(t);          
     }
  }
   digitalClockDisplay();  
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*  code to process time sync messages from the serial port   */
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message

time_t processSyncMessage() {
  // return the time if a valid sync message is received on the serial port.
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ;
    Serial.print(c); 
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();         
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number   
        }
      }   
      return pctime;
    }  
  }
  return 0;
}


I can send an ASCII string to set the time to whatever I want over the serial terminal but resetting the board resets the RTC.

Example output:

RTC has set the system time
0:00:00 31 12 1999
0:00:01 31 12 1999
0:00:02 31 12 1999
0:00:03 31 12 1999
0:00:04 31 12 1999
T23:31:31 13 2 2009  <--- Sent string T1234567890
23:31:32 13 2 2009
23:31:33 13 2 2009
23:31:34 13 2 2009
23:31:35 13 2 2009
RTC has set the system time  <--- Reset button pressed
0:00:00 31 12 1999
0:00:01 31 12 1999
0:00:02 31 12 1999
29  Using Arduino / Programming Questions / Re: Stalker V2 Help!! Set the time? RFID Logger on: July 26, 2011, 03:54:49 pm
I have a problem with figuring out the RTC of a newly acquired stalker v2.0.

I have played around with code such as found on this site or in this thread (nasukaren's post of 06.08.2010 at 12:55:33) or as in the TimeRTCSet example in the Time library.

The problem is, I can set the RTC to a specified time but this time resets when I reset the board. In the case of the Time library example, I can send an ASCII time string over Serial (character 'T' followed by ten digits) to set time to whatever I want but it still resets when I reset the board (to midnight December 31st 1999).

All of these examples use Time.h and DS1307RTC.h (and of course, Wire.h). Could it be that DS1307RTC.h cannot set the Stalker's RTC and I have to use RX8025.h instead? If I understand things correctly, RX8025.h does not use time_t which can be used by Time.h. If I got this right, I find it awkward (the Time library offers functionality I 'd rather not have to do without).

Am I making some stupid mistake?
30  Using Arduino / General Electronics / Re: ADC noise problems on: June 09, 2011, 03:59:35 am
I did, it was fine switching arduino on with the motor on, and it was fine *most* of the time if I switched the arduino on first. I only got around two or three resets at, say, around seven or eight tries (and the FTDI chip immediately reconnected to another port) so it was a rather minor glitch compared to the case of the raw motor.
Pages: 1 [2] 3 4 5