Show Posts
Pages: [1]
1  Products / Arduino Due / Re: Due I2C not working on: February 06, 2013, 06:48:59 pm
Well this is where it gets confusing. It works without the logic converter. But not with the logic converter. I have tried two types of logic converters, both bi-directional. The signal comes through but the Arduino will not cooperate
2  Products / Arduino Due / Re: Due I2C not working on: February 05, 2013, 06:01:30 pm
I was under the impression the Due had internal 3.3v pull up resistors on the I2C lines. Is this correct? Nevertheless it is working as is, but just not 100% reliable
3  Products / Arduino Due / Re: Due Interrupt Bug on: February 04, 2013, 06:17:29 pm
Thanks Liarco, from initial testing the patch worked wonders.
4  Products / Arduino Due / Re: Due I2C not working on: February 04, 2013, 06:00:34 pm
Update:

I have gotten the sensor to work without level shifting it (as sda/scl are open collector). However it is very temperamental and will sometimes just not work at all. I also cannot get four devices to work, three will but not four. Again, this code and these devices work fine together on a Mega.

Does anyone have any experience with similar I2C problems?
5  Products / Arduino Due / Due I2C not working on: February 03, 2013, 07:23:58 pm
Hi Guys,

I am having trouble interfacing a 5v SRF02 sonar sensor with the Arduino Due. I have tried a few different level shifters, resistor combinations, and both I2C ports on the Due. Using an oscilloscope I can see the signal is being sent. There is just no response from the sensor.

Main program
Code:
//External Libraries
#include <Wire.h>

//Internal Headers
#include "IC2Sonar.h"

//Generate Sonar Objects
IC2Sonar sonar0(113);
IC2Sonar sonar1(114);
IC2Sonar sonar2(115);
IC2Sonar sonar3(116);

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  delay(2000);
}

void loop()
{

  //SONAR DEBUG - must be running updateMap to fill SonarArray
   Serial.print(sonar0.updateSonar());
   Serial.print(" ");
   Serial.print(sonar1.updateSonar());
   Serial.print(" ");
   Serial.print(sonar2.updateSonar());
   Serial.print(" ");
   Serial.print(sonar3.updateSonar());
   Serial.println();
 
   delay(500);
}

Class for each sonar sensor
Code:
#ifndef __IC2SONAR_H__
#define __IC2SONAR_H__

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

class IC2Sonar {

  //Define class variables (private)
  int sensorVal;
  int sensorAddress;

public:
  //Constructor
  IC2Sonar(int address)
  {
    sensorVal = 200;
    sensorAddress = address; 
  };

  int updateSonar()
  {
    Wire.beginTransmission(sensorAddress);
    Wire.write(byte(0x00));                               
    Wire.write(byte(0x51));           
    Wire.endTransmission();

    delay(100);
   
    Wire.beginTransmission(sensorAddress);
    Wire.write(byte(0x02));                                           
    Wire.endTransmission();
   
    Wire.requestFrom(sensorAddress, 2);
 
    if(2 <= Wire.available())
   {   
      int reading = Wire.read();
      reading = reading << 8;    // shift high byte to be high 8 bits
      reading |= Wire.read();
      sensorVal = reading;
   }

    return sensorVal;
  }

private:
};

#endif // __IC2Sonar_H__

This program works fine on a Mega and returns 200 for each sensor not plugged in. The Due, however, always returns 0 for each sensor.
Your help is appreciated
6  Products / Arduino Due / Re: Due Interrupt Bug on: January 23, 2013, 03:30:56 am
Code:
#define VELOCITY_SAMPLE_TIME 10 //sample time (ms) between velocity checks
#define ENCODER_COUNTS 250

#include "Encoder.h"

unsigned long VelocityTimeStamp=0;

Encoder encoderFL(27, 29);
Encoder encoderFR(45, 43);
Encoder encoderRL(31, 33);
Encoder encoderRR(41, 39);

void setup()
{
  Serial.begin(9600);
 
  encoderFL.initilise();
  encoderFR.initilise();
  encoderRL.initilise();
  encoderRR.initilise();

  //Attach Interupts to both pins for each encoder(for full resolution)
  attachInterrupt(encoderFL.pin_a,doEncoderFL,CHANGE);
  attachInterrupt(encoderFL.pin_b,doEncoderFL,CHANGE);
  attachInterrupt(encoderFR.pin_a,doEncoderFR,CHANGE);
  attachInterrupt(encoderFR.pin_b,doEncoderFR,CHANGE);
  attachInterrupt(encoderRL.pin_a,doEncoderRL,CHANGE);
  attachInterrupt(encoderRL.pin_b,doEncoderRL,CHANGE);
  attachInterrupt(encoderRR.pin_a,doEncoderRR,CHANGE);
  attachInterrupt(encoderRR.pin_b,doEncoderRR,CHANGE);

}

void loop()
{
   updateVelocity();
   //ENCODER DEBUG
   Serial.print(encoderFL.getPosition(),DEC);
   Serial.print(" ");
   Serial.print(encoderFR.getPosition(),DEC);
   Serial.print(" ");
   Serial.print(encoderRL.getPosition(),DEC);
   Serial.print(" ");
   Serial.print(encoderRR.getPosition(),DEC);
   Serial.print(" ---- ");
   Serial.print(encoderFL.getVelocity(),DEC);
   Serial.print(" ");
   Serial.print(encoderFR.getVelocity(),DEC);
   Serial.print(" ");
   Serial.print(encoderRL.getVelocity(),DEC);
   Serial.print(" ");
   Serial.print(encoderRR.getVelocity(),DEC);
   Serial.println();

}

//Wrapper Functions to call correct encoder object method on interupt
void doEncoderFL(){
  encoderFL.doCount();
}
void doEncoderFR(){
  encoderFR.doCount();
}
void doEncoderRL(){
  encoderRL.doCount();
}
void doEncoderRR(){
  encoderRR.doCount();
}


//Function to calculate velocity of encoders once every VELOCITY_SAMPLE_TIME
void updateVelocity(){
  if(VelocityTimeStamp+VELOCITY_SAMPLE_TIME<millis()){
    encoderFL.calculateVelocity();
    encoderFR.calculateVelocity();
    encoderRL.calculateVelocity();
    encoderRR.calculateVelocity();
    VelocityTimeStamp=millis();
  }
}


Code:
#ifndef __ENCODER_H__
#define __ENCODER_H__

#include "Arduino.h"

//This class is designed to manage the encoders
//Four instances of this class are created: one for each encoder
//The methods in this class are called by interupts (attached in the setup function of the main program)
//There is a wrapper function called 'doEncoder' in the main function designed to call the correct encoder method
//this function is required as interupts cannot call object methods directly (unless static there is no pointer)

class Encoder {
public:

  int pin_a;

  int pin_b;

  // constructor : sets pins as inputs and turns on pullup resistors

  Encoder(int A, int B){
    // Set pin a and b to be input
    Evelocity=0;
    Eposition=0;
    EpositionCache=0;
    pin_a=A;
    pin_b=B;

  };

  void initilise(){
    pinMode(pin_a, INPUT);
    pinMode(pin_b, INPUT);

    // Turn on pullup resistors
    digitalWrite(pin_a, HIGH);   
    digitalWrite(pin_b, HIGH);   
  }
 
  void doCount(){
    //Serial.println("Doing Count");
    State = (digitalRead(pin_a)<<1)|digitalRead(pin_b);
    switch(State)
    {
      case 0:
        if(PreviousState==1)Eposition++;
        else Eposition--;
      break;
      case 1:
        if(PreviousState==3)Eposition++;
        else Eposition--;
      break;
      case 2:
        if(PreviousState==0)Eposition++;
        else Eposition--;
      break;
      case 3:
        if(PreviousState==2)Eposition++;
        else Eposition--;
      break;
    }
    PreviousState=State;
  }

  // returns current position
  long int getPosition () {
    return Eposition;
  };

  float getVelocity () {
    return Evelocity;
  };

  void calculateVelocity(){
    Evelocity = (float(Eposition-EpositionCache)/float(millis()-TimeStamp))*1000/(ENCODER_COUNTS*4);
    TimeStamp = millis();
    EpositionCache=Eposition;
  }

  // set the position value
  void setPosition ( const long int p) {
    Eposition = p;
  };

private:

  float Evelocity;

  unsigned long TimeStamp;

  long int EpositionCache;

  volatile long int Eposition;
 
  byte State;
 
  byte PreviousState;
};

#endif // __ENCODER_H__
7  Products / Arduino Due / [FIXED]Due Interrupt Bug on: January 23, 2013, 01:21:45 am
I believe I have found a bug with the external interrupts of the due. I was trying to use 8 interrupts simultaneously for 4 encoders but I could never get all of them to work. Eventually I got them to work by trying different pins for each channel until they all worked.
Perhaps there is an error in the compiler?

Using pins 23,24,45,43,31,33,37,35 works. But I couldn't get any simpler sequential sets of pins to work together. Sometime only one channel would be picked up by the interrupt and sometimes only half of the encoder resolution would be returned.

I can attach my code if needed.
Pages: [1]