Show Posts
Pages: [1] 2 3
1  Using Arduino / Audio / Help- I broke something and cannot figure it out re: Adafruit Wave Shield on: June 06, 2013, 01:00:21 pm
The attached code was working perfectly and then it stopped playing the music. I had added a couple variables and some code inside the loop(). Even thought the IDE said SRAM was 1785 out of 2000 (UNO) I thought I might have gone over the limit so I commented out the changes and reduced the SRAM but it still didn't work. If I load the daphc example from the WaveHC library, it plays all of the sound files perfectly. With my code, I now get a consistent "Couldn't open file kids2.wav" or any other file I try. To the best of my knowledge I haven't touched the WaveHC code. Can anyone see the discrepancy? I'm 70 and need all the help I can get.

Code:
//  this is the "final" test version that is working well. remote can trigger a song.

//                slave 3        slave 3

//#include <FatReader.h>
//#include <SdReader.h>
//#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"
#include <Wire.h>

SdReader card;    // This object holds the information for the card
FatVolume vol;    // This holds the information for the partition on the card
FatReader root;   // This holds the information for the filesystem on the card
FatReader f;      // This holds the information for the file we're play

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

//***********  I2C  variables  *************
byte cb[100];   // [0] to [49] are incoming from master, [50] to [99] are outgoing to master
byte cbb[25];
byte cbsend[25];
byte tled=A0;
byte rled=A1;
int swt=0;
int swr=0;
int insw=0;
int outsw=0;
byte slavenum=3;
byte jsw=0;
//**************end I2C variables  *****************
//***********  I2C  variables definitions *************
byte& mode=cb[0]; 
byte& function=cb[1];
byte& action=cb[2];
byte& sysmode=cb[3];
byte& perfmode=cb[4];
byte& testmode=cb[5];
byte& songnum=cb[10];// index # of song to be played


//**************end I2C variables definitions *****************
//*****************  system variables  **********************
byte oldaction=0;
byte oldsongnum=0;
byte song=0;
//******************* end system variables  *****************************

//this handy function will return the number of bytes currently free in RAM, great for debugging!   
// int freeRam(void)
// {
  // extern int  __bss_end;
  // extern int  *__brkval;
  // int free_memory;
  // if((int)__brkval == 0) {
    // free_memory = ((int)&free_memory) - ((int)&__bss_end);
  // }
  // else {
    // free_memory = ((int)&free_memory) - ((int)__brkval);
  // }
  // return free_memory;
// }

void sdErrorCheck(void)
{
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);
  while(1);
}
//********************   begin SETUP  *******************************
void setup() {
  // set up serial port
  Serial.begin(57600);
 
 //  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
 // Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(tled, OUTPUT);
  pinMode(rled, OUTPUT);
    Wire.begin(slavenum);
    Wire.onReceive(cbin);
    Wire.onRequest(cbout);
    for(int jk=0;jk<=24;jk++)cbb[jk]=0;
   
    for(int jj=0;jj<=99;jj++)
    {
      cb[jj]=0;
    }
    cb[74]=24;
    cb[99]=49;
    for (int jj=0;jj<=24;jj++)
    {
      cbsend[jj]=0;
    }
    digitalWrite(rled,LOW);
digitalWrite(tled,LOW);
//************** end I2C setup  ************************
 
 
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!) 
    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
    sdErrorCheck();
    while(1);                            // then 'halt' - do nothing!
  }
 
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
 
// Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
    if (vol.init(card, part))
      break;                             // we found one, lets bail
  }
  if (part == 5) {                       // if we ended up not finding one  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }
 
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
 
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
 
  // Whew! We got past the tough parts.
  putstring_nl("Ready!");
}
//**************************  end SETUP  ******************************
//**********************  loop begins  *******************************
void loop() {
// Serial.print(mode);
// Serial.print("  ");
// Serial.print(function);
// Serial.print("  ");
// Serial.print(action);
// Serial.print("  ");
// Serial.print(sysmode);
// Serial.print("  ");
// Serial.print(perfmode);
// Serial.print("  ");
// Serial.print(testmode);
// Serial.print("  ");
// Serial.println(songnum);
sysmode=2;
songnum=3;
  switch(sysmode){
case 1:
wave.stop();
break;
case 2:
if(songnum == 0){wave.stop(); return;}
if(songnum != 0){
if(songnum == 99) {wave.stop(); return;}
if(songnum == oldsongnum && wave.isplaying)return;
if(songnum != oldsongnum){
wave.stop();
oldsongnum=songnum;
//Serial.print("songnum = ");
//Serial.println(songnum);
switch (songnum) {
case 1:
playfile("atmos.wav");
break;
case 2:
playfile("melbel.wav");
break;
case 3:
playfile("kids2.wav");
break;
case 4:
playfile("kids3.wav");
break;
case 5:
playfile("spacy1.wav");
break;
case 6:
playfile("mspolly.WAV");
break;
}
   }
}
break;
case 3:
if(mode == 2 && function == 4) {
        if(action==0)return;
        if(action==99) {wave.stop(); return; }
        if(action==oldaction && wave.isplaying) return;
        if(action != oldaction) {
        wave.stop();
        oldaction=action;
        // Serial.print("action - ");
// Serial.println(action);
switch (action) {
case 1:
playfile("atmos.wav");
break;
case 2:
playfile("melbel.wav");
break;
case 3:
playfile("kids2.wav");
break;
case 4:
playfile("kids3.wav");
break;
case 5:
playfile("spacy1.wav");
break;
case 6:
playfile("mspolly.WAV");
break;
}
}
}
break;
  }
  delay(500);
}
//********************  loop ends   *****************************************

// Plays a full file from beginning to end with no pause.
void playcomplete(char *name) {
  // call our helper to find and play this name
  playfile(name);
  while (wave.isplaying) {
  // do nothing while its playing
  }
  // now its done playing
}

void playfile(char *name) {
  // see if the wave object is currently doing something
  if (wave.isplaying) {// already playing something, so stop it!
    wave.stop(); // stop it
  }
  // look in the root directory and open the file
  if (!f.open(root, name)) {
    putstring("Couldn't open file "); Serial.print(name); return;
  }
  // OK read the file and turn it into a wave object
  if (!wave.create(f)) {
    putstring_nl("Not a valid WAV"); return;
  }
 
  // ok time to play! start playback
  wave.play();
}
//****IN ******* IN *****************
void cbin (int x){
if(swr==0)digitalWrite(rled,HIGH);
for(int jj=0;jj<=24;jj++)cbb[jj]=Wire.read();
for(int jj=0;jj<=24;jj++)cb[(cbb[24]-24)+jj]= cbb[jj];
if(swr==5)digitalWrite(rled,LOW);
if(swr==8)swr=-1;
swr++;
}
//************** end IN  ***************************
//*****OUT*********OUT****************
void cbout(){
if(swt==0)digitalWrite(tled,HIGH);
if(outsw==2)outsw=0;
for(int jj=0;jj<=24;jj++)cbsend[jj]= cb[jj+50+(outsw*25)];
Wire.write(cbsend,25);
outsw++;
if(swt==5)digitalWrite(tled,LOW);
if(swt==8)swt=-1;
swt++;
}
//******************* end OUT  *********************
2  Using Arduino / Networking, Protocols, and Devices / Re: I2C use 2 libraries on the same I2C bus ? on: April 04, 2013, 04:25:35 pm
wayneft  Thanks for solving my problem !!

Your suggestion that it should work prompted me to load the servo example from the adafruit library. Lo and behold it works. I have a bug I'll have to work on.

Thanks for correcting my bad assumption.
3  Using Arduino / Networking, Protocols, and Devices / Re: I2C use 2 libraries on the same I2C bus ? on: April 04, 2013, 04:10:26 pm
Here's the code. No compile errors. Serial.prints indicate am sending the right values to the pwm.setPWM command. Getting nothing at the servos.

Had to remove several sections in order to get under 9500 word limit. Didn't remove anything relevant to the problem.

Code:

#include <Wire.h>
#include <EEPROM.h>
#include <MatrixOrbitali2c.h>
#include <stdlib.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40);
MatrixOrbitali2c lcd(0x28);
 
 

//***********  new sweep structure  **************
struct ServoMgr{
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Variables for each instance of ServoMgr (e.g. fl, fr, etc.)
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    int top;
    int bottom;
    int period;
int servoValue;
    int servoRange;
int pwmvalue;
int corFact;
    long startTime;
    long time;
    String name; // Just so we know which one we are for debugging.
    int pwmpin;
        
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Instance functions to act on the variables above and any parameters
    // passed in.  We start with a Setup() function here, but you can
    // use any names you want.
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
    void Setup(int servoPin, String servoName){
        pwmpin = servoPin;
        name = servoName;
        
        /* The following is not necessary, but this would be a good place to
           initialize anything at startup. */
        top = 0;
        bottom = 0;
        servoRange=top-bottom;
        startTime=0;
        time=0;
    }
    void Move(int newBottom, int newTop, int newPeriod) {
if(bottom != newBottom || top != newTop || period != newPeriod*100)
{
            Initialize(newBottom, newTop, newPeriod);
          //  Serial.println(name + " sweep after Initialize");
        }

time = millis() - startTime;
if(top>bottom && servoValue>=top){
         Serial.println(servoValue);
return;
};
if(top<bottom && servoValue <=top){
Serial.println(servoValue);
return;
};
servoValue=map(time,0, period,bottom,top);
pwmvalue=map(servoValue,0,180,150,600);
pwm.setPWM(pwmpin,0,pwmvalue);
Serial.print("servovalue  ");
Serial.print(servoValue);
Serial.print("     pwmvalue  ");
Serial.println(pwmvalue);

}
    void Sweep(long newBottom, long newTop, long newPeriod){
 //  Serial.println(String(bottom)+"  "+String(newBottom)+"  "
 //  +String(top)+"  "+String(newTop)+"  "+String(period)+"  "
 //  +String(newPeriod*100));
        if(bottom != newBottom || top != newTop || period != newPeriod*100)
{
            Initialize(newBottom, newTop, newPeriod);
          //  Serial.println(name + " sweep after Initialize");
        }
        
        time = millis() - startTime;
        if(time >= period)
            InitTime();
        
        /* Little Trick: The absolute value function, abs(), makes a nice up/down when spread accross 0 (-10 to 10).
           I'm screwing it up right now, but you might consider it :)
          
           servoValue = map(servoRange*abs(period/2 - time),0,servoRange*(period),bottom,top);
        */
        
        
        if(time >=0 && time <= period/2){
// Serial.println(String(servoRange)+"  "+String(time)+"  0  "+String(servoRange)
// +"  "+String(period/2)+"  "+String(bottom)+"  "+String(top));
            servoValue=map(servoRange*time,0, servoRange*(period/2),bottom,top)+corFact;
}
        if(time > period/2 && time <= period){
            servoValue=map(-servoRange*(time-period),0,servoRange*(period/2),bottom,top)+corFact;
}        
        /*  Serial.print (time,DEC);
 Serial.print("  ");
 Serial.print(name);
 Serial.print("  value =  ");
 Serial.println(servoValue+corFact,DEC);
*/
         //Serial.println(name + ": bottom = " + String(bottom) + " top = " + String(top) + " servoValue = " + String(servoValue+corFact,DEC));

       // myServo.write(servoValue);//DEBUG-UNCOMMENT THIS LINE IN REAL LIFE
    }
    
    void Initialize(int newBottom, int newTop, int newPeriod){
        bottom = newBottom;
        top = newTop;
        period = newPeriod * 100;
        servoRange=top-bottom;
        InitTime();        
      //  Serial.println(name + ": - New Values Initialized.");
      //  Serial.println("  " + name + ": bottom = " + String(bottom) + " top = " + String(top) + " period = " + String(period));
    }
    
    void InitTime(){
        time=0;
        startTime=millis();
    }
};
ServoMgr lc,rc;



//************************  Master Variables   *******************************
int rled = A0;
int tled = A1;
byte inByte;
byte stringPos;
byte cb[1200];  // for maximum of one master and 12 slaves
byte cbb[25];
byte cbsend[25];
int swt=0;
int swr=0;


//********************** end Master Variables  ********************************

// *******************    begin setup   **************************************
void setup() {
Serial.begin(57600);
Serial1.begin(57600);
Wire.begin();  
lcd.begin(4,20);
pwm.begin();
pwm.setPWMFreq(1600);
uint8_t twbrbackup = TWBR;
TWBR = 12;
pinMode(tled,OUTPUT);
pinMode(rled,OUTPUT);
lc.Setup(lcircles,"left ciccles");
rc.Setup(rcircles,"right circles");


//  *********************  end of setup  ***************************************

//****************  loop    loop    loop   *****************************
void loop(){
 //if(schedule(3))cbout();
// if(schedule(3))cbin();
cbout();
delay(30);
cbin();
delay(30);
//if(schedule(0)) printcb();   // {2000,0},{50,0},{1000,0},{50,0}
if(schedule(1)) lcdprint();  //          
if(schedule(2)) slave10();
if(schedule(1)) mfa();
mfa();

updatebuffer();

}

//**************** end loop     end loop    end loop  *****************
//******************  mode function action  **************************
void mfa(){
//Serial.println("mfa");
switch(mode){  // 2-8 only here, 0-2 and 9 are in teh R2Base
case 2:
switch(function){
case 1:  // misc testing here
switch(action){
case 1:  // circles folded back
Serial.println("2 1 1");
lc.Move(175,85,60);
rc.Move(175,85,60);

break;
case 2:  // circles folded out
Serial.println("2 1 2");
lc.Move(85,175,60);
rc.Move(85,175,60);
break;
}
break;
}
break;
}
}
//***************  end mode function action  *****************************






//*************** OUT***********out***********************
void cbout()
{
 if(swt==0) digitalWrite(tled,HIGH);
 for(int i=1;i<=2;i++) {
     for(int j=2; j<=numslave;j++) { // set to cover all slaves
     for(int jj=0;jj<=24;jj++) cbsend[jj]=cb[((j-2)*50)+((i-1)*25)+jj];
 Wire.beginTransmission(j);
 Wire.write(cbsend, 25);
 Wire.endTransmission();
     }
 }
if(swt==5) digitalWrite(tled,LOW);
if(swt==8) swt=-1;
swt++;
}
//*******************  end of OUT  ****************************
//*****************IN************IN********************
void cbin() {
  if(swr==0) digitalWrite(rled,HIGH);
  for(int i=1;i<=2;i++) {
  for(int j=2;j<=numslave;j++) {
Wire.requestFrom(j,25);
for (int kk=0;kk<=24;kk++) cbb[kk]=Wire.read();
for(int jj=0;jj<=24;jj++) cb[(cbb[24]-24)+(j-2)*50+600+jj]= cbb[jj];
  }
  }
if(swr==5) digitalWrite(rled,LOW);
if(swr==8) swr=-1;
swr++;

}
//  ********************** end of IN  *****************************
void printcb () {
for(int i=400;i<=419;i++) {
Serial.print(cb[i]);
Serial.print("  ");
}
Serial.println();
for(int i=1000;i<=1019;i++) {
Serial.print(cb[i]);
Serial.print("  ");
}
Serial.println();
}
//*****************   4x20 LCD  Print  ****************************************
void lcdprint(){  
  // Serial.print("mode = ");
  // Serial.print(mode);
  // Serial.print("    function =  ");
  // Serial.print(function);
  // Serial.print("      index = ");
  // Serial.println(cb[603]);
  lcd.clear();
  lcd.print("m");
  lcd.print(mode);
  lcd.print("  f");
  lcd.print(function);
  lcd.print("  a");
  lcd.print(action);
  
  
}
//****************   end 4x20 LCD  Print  *************************
boolean schedule(int id){
unsigned long currentMillis = millis()%1000000;
if(currentMillis - schedules[id][1] > schedules[id][0]){
schedules[id][1] = currentMillis;
return true;
}
else
return false;
}
4  Using Arduino / Networking, Protocols, and Devices / I2C use 2 libraries on the same I2C bus ? on: April 04, 2013, 03:03:53 pm
Am building a big robot, 80% done. Eight Arduinos on the same I2C bus, one master, 7 slaves - works perfectly. Now I would like to add a PCA9685 (Adafruit) to the same I2C network, however the Adafruit library for it, assumes (I think) complete control of the I2C bus. I tried a few variations and conclude that the wire and adafruit libraries cannot coexist on the same bus. I know I can use a serial connection to a new Arduino which can then drive the PCA9685 with it's I2C bus. I would like to avoid that if possible.

I have seen the softi2c library but cannot find that it will run two buses concurrently.

Does anyone know of an alternative approach? Not looking for complete solutions, just ideas about things I may not have seen in perusing the net. Thanks
5  Using Arduino / Programming Questions / Re: Serial print problems with 1.0.1c - exact same code no problems with 0022 on: October 16, 2012, 02:16:28 pm
Here's the code, and output from the console.

Code:
#include <Servo.h>    // Use Servo library, included with IDE
#define term 13
Servo myServo;        // Create Servo object to control the servo

const uint8_t p8 = 4;
const uint8_t p7 = 12;
const uint8_t p6 = 11;
const uint8_t p5 = 10;
const uint8_t p4 = 9;
const uint8_t p3 = 8;
const uint8_t p2 = 7;
const uint8_t p1 = 6;

// For the list below, the subscript 0-127 is the decimal equivalent from // the encoder. The data elements are the sequence number associated with // the decimal equivalent produced by the encoder for the corresponding // sequence number.
byte gray [128][2]= {{1,56},{2,40},{3,55},{4,24},{6,39},{7,52},{8,8},{9,57},
{12,23},{14,36},{15,13},{16,120},{18,41},{19,54},{23,53},{24,7},
{28,20},{29,19},{30,125},{31,18},{32,104},{33,105},{36,25},{37,106},{38,38},
{41,58},{46,37},{47,14},{48,119},{49,118},{53,107},{56,4},{58,3},{60,109},
{61,108},{62,2},{63,1},{64,88},{66,89},{71,51},{72,9},{73,10},{74,90},
{76,22},{77,11},{79,12},{82,42},{83,43},{92,21},{94,126},{95,127},{96,103},
{98,102},{106,91},{112,116},{113,117},{116,115},{120,93},{121,94},{122,92},
{124,114},{125,95},{126,113},{127,0},{128,72},{129,71},{131,68},{132,73},
{135,29},{137,70},{139,69},{142,35},{143,34},{144,121},{146,122},{148,74},
{151,30},{152,6},{154,123},{158,124},{159,17},{163,67},{164,26},{166,27},
{167,28},{169,59},{175,15},{184,5},{188,110},{190,111},{191,16},{192,87},
{193,84},{195,45},{196,86},{197,85},{199,50},{203,46},{207,33},{209,83},
{211,44},{212,75},{215,31},{223,32},{224,100},{225,61},{226,101},{227,66},
{229,62},{231,49},{232,99},{233,60},{235,47},{239,48},{240,77},{241,82},
{242,78},{243,65},{244,76},{245,63},{247,64},{248,98},{249,81},{250,79},
{251,80},{252,97},{253,96},{254,112}};
//byte comnd[50]={0};
//byte& target = comnd[0];
//byte status[50]={0};
//byte& current = status[0];
int list [100] = {0};
int listptr =0;
int curangle =0;
int difference =0;
int rate =0;
int dir = 0;
int graycur =0;
int target =7;
int current;
int stop=1;
int stringPos =0;
int cntr=0;   //  check this later on for overflow
//char inString[6];
//int input;
//int c;
//char inByte;




void setup() {
Serial.begin(9600);
myServo.attach(2);  // Servo is connected to digital pin 2
pinMode(p8, INPUT);
pinMode(p7, INPUT);
pinMode(p6, INPUT);
pinMode(p5, INPUT);
pinMode(p4, INPUT);
pinMode(p3, INPUT);
pinMode(p2, INPUT);
pinMode(p1, INPUT);
digitalWrite(p8,HIGH);
digitalWrite(p7,HIGH);
digitalWrite(p6,HIGH);
digitalWrite(p5,HIGH);
digitalWrite(p4,HIGH);
digitalWrite(p3,HIGH);
digitalWrite(p2,HIGH);
digitalWrite(p1,HIGH);
}


void loop() {
if(stop==1) {
target = consoleinput();
if(target==0) goto end;
Serial.print("target = ");
Serial.println(target);
target = target / 2.8125;  // convert angle to encoder position 0-126
Serial.println("target = " + String(target));
graycur= GrayInput();
current = gray[graylookup(graycur)][1];
Serial.println("current = " + String(current));
if(abs(target-current)
if(target < current) dir = 1;
else dir = -1;
steer(dir,1);
list[0]= target;
list[1]= current;
listptr=2;
stop=2;
}
while(stop==2) {
cntr++;
graycur= GrayInput();
current = gray[graylookup(graycur)][1];
list[listptr]=current;
listptr++;
difference = target-current;
  if((cntr % 10)==0) Serial.println("graybin  gray  curnt   diff   target");
Serial.print(graycur,BIN);
Serial.print("    ");
Serial.print(graycur);
Serial.print("    ");
Serial.print(current);
Serial.print("       ");
Serial.print(difference);
Serial.print("    ");
Serial.println(target);   

//Serial.println("difference = " + String(difference));
//Serial.println(String(graycur)+"   "+String(current)+"   "+String(difference)+"   "+String(target));
if(abs(difference) < 9 && abs(difference)>2) {
rate = 1;
steer(dir, rate);
}
if(abs(difference) < 6 && current != 0) {
rate = 0;
stop=1;
target=0;
cntr=0;
steer(dir, rate);
//for(int j=0; j=listptr-1; j++) Serial.print(String(list[j])+"  ");
//Serial.println("   ");
listptr=0;
}
//Serial.println("dir = " + String(dir)+"  rate =  "+String(rate));
}
end: delay(1);
   }
 
    int consoleinput(){
   
char inString[6];
int input;
int c;
char inByte;
while(Serial.available()) {
inByte = Serial.read();
Serial.println("inByte = " + String(inByte));
//c = Serial.available();
//Serial.println("serial.available is  "+String(c));
if((inByte >= '0') && (inByte <= '9')){
inString[stringPos] = inByte;
stringPos ++;
}
if(inByte == 'x'){
    Serial.println(" \r received");
for(c=0; c<stringPos; c++) Serial.print(inString[c]);
Serial.println(" ");
input = atoi(inString); //convert string to int
Serial.println(input);
        for ( c = 0; c < stringPos; c++){
inString[c] = 0;
}
stringPos = 0;
return input;
}
    }
  }
 
 
 
 
  int steer(int dir, int rate) {  // 1300 is clockwise, 1500 stop, 1700 counter-clockwise
  // dir = 1 cw  dir = -1 ccw  rate = 0 stop, 1 slow, 2 fast   

myServo.writeMicroseconds(1500 +(70* rate*(0 - dir)));
    }
 
 
  // Return the current values of the input pins
byte GrayInput(){
    byte lbits = 0;
    if ( digitalRead(p8)) bitSet(lbits,7);
    if ( digitalRead(p7)) bitSet(lbits,6);
    if ( digitalRead(p6)) bitSet(lbits,5);
    if ( digitalRead(p5)) bitSet(lbits,4);
    if ( digitalRead(p4)) bitSet(lbits,3);
    if ( digitalRead(p3)) bitSet(lbits,2);
    if ( digitalRead(p2)) bitSet(lbits,1);
    if ( digitalRead(p1)) bitSet(lbits,0);
    return lbits;
}

int graylookup(int intarg) {
int n=0;
int j=0;
for(int i=1;i<=7;i++) {
j = 1<<(7-i);
if(intarg==gray[(j+n)][0]) goto end;
if(intarg>=gray[(j+n)][0])n = n+j ;
    }
    end: j=j+n;
  return j;
}





and here is the console output
Quote
target = 49
target = 17
current = 122
graybin  gray  curnt   diff   target
10010010    146    122       -105    17
graybin  gray  curnt   diff   target
10010010    146    122       -105    17
graybin  gray  curnt   diff   target
10010000    144    121       -104    17
graybin  gray  curnt   diff   target
10000    16    120       -103    17
graybin  gray  curnt   diff   target
110000    48    119       -102    17
graybin  gray  curnt   diff   target
110000    48    119       -102    17
graybin  gray  curnt   diff   target
110001    49    118       -101    17
graybin  gray  curnt   diff   target
110001    49    118       -101    17
graybin  gray  curnt   diff   target
1110001    113    117       -100    17
graybin  gray  curnt   diff   target
1110001    113    117       -100    17
graybin  gray  curnt   diff   target
1110001    113    117       -100    17
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>many lines deleted here
graybin  gray  curnt   diff   target
11101001    233    60       -43    17
graybin  gray  curnt   diff   target
11101001    233    60       -43    17
10101001    169    59       -42    17
101001    41    58       -41    17
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>many lines deleted here
101001    41    58       -41    17
101001    41    58 
 
at this point, a few lines of gibberish - doesn't copy or print here - is seen and the it stops completely.

Sometimes it works perfectly, I have tried to figure out what makes one pass work and another not. This code is about steering a large robot using an absolute encoder with 8bit gray code output to determine the position of the wheel. The actual drive is a gear motor with chain drive, the encoder providing the feedback. In the code you see here, I am using a continuous servo to drive the encoder to make it easier to develop and test the code.

The console input often drops the first and or second digit entered and other times works perfectly.
6  Using Arduino / Programming Questions / Serial print problems with 1.0.1c - exact same code no problems with 0022 on: October 15, 2012, 04:01:43 pm
Spent many hours trying to solve strange problems with serial.print. Changed .ino to .pde and all problems gone with 0022.

I looked at a list of open problems and didn't see anything close. Is there a maintenance pkg I may not be aware of?
7  Using Arduino / Networking, Protocols, and Devices / Re: XBEE looking for specific sketch on: September 16, 2012, 07:15:30 am
Found it !!

The beauty of this is that is uses XBEEs "out of the box" - no configuration necessary and it works very well.

http://bildr.org/2011/04/arduino-xbee-wireless/

Thanks to the guys at MIT.
8  Using Arduino / Networking, Protocols, and Devices / XBEE looking for specific sketch on: September 14, 2012, 04:28:12 pm
configuration is arduino-xbee-xbee-arduino. each arduino has one potentiometer and one led. turning the pot on one arduino dims the led on the other and vice versa. The xbees are used with default settings. I found this example a month or so ago and set up this configuration and it works perfectly. Now I want to use it as a base to modify for my project. I suspect I copied and pasted the code into the IDE and therefore didn't have a copy saved. I spent 3 hours today looking for it without success. If anyone knows of it, can you point me?? I would be most grateful.
Thanks
9  Using Arduino / Interfacing w/ Software on the Computer / Strange behavior mega2560 pins 15,16,17 (RX2, TX2,RX3) on: January 09, 2012, 03:06:24 pm
Adding IR receiver to busy mega2560. I used the referenced pins for digital input and pin 18 for an interrupt from the receiver. The interrupt service routine always worked as expected, the digital inputs worked erratically 5% of the time. Spent a lot of time chasing possible hardware problems and staring at the software too much. On a hunch, I moved the 4 lines over to pins 2 (interrupt 0) and 3,4,5 for data - now works perfectly. The wire library is being used for I2C which works flawlessly and the lcd library for a 16 x 2 display. If it has been stated somewhere, I haven't seen it, is it a no-no to use the RX2, etc pins when the wire services are turned on. I assumed these pins were available for general use. Like I said above, 5% of the time it did work as expected. Any thoughts?
10  Using Arduino / Project Guidance / Hand held rf remote control and receiver/decoder on: December 27, 2011, 10:10:40 am
Have searched a lot and no luck finding a hand held rf remote (10-15 buttons) and receiver/decoder module for and arduino controlled robot project. I know there's lots of ir options but I do not want that. Can anyone point me in the right direction?
11  Using Arduino / Networking, Protocols, and Devices / I2C mixed slave types on: November 30, 2011, 05:22:02 pm
I have an I2C netowrk woriking with one Arduino master and 6 Arduino slaves. I want to add a compass with and I2C interface such as https://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_2150248_-1 . Are there any hidden problems with having the Arduino master address the compass as well as the other slaves? Doesn't look like there should be, but would like to know more before I buy the compass. Thanks
12  Using Arduino / Programming Questions / Re: ROBOTC for Arduino on: October 28, 2011, 07:12:22 am
Nay sayers should read this   http://www.robotc.net/index.php
13  Using Arduino / Programming Questions / Re: ROBOTC for Arduino on: October 27, 2011, 11:22:15 am
Sorry, wasn't thinking   http://www.robotc.net/download/.
14  Using Arduino / Programming Questions / ROBOTC for Arduino on: October 27, 2011, 11:06:59 am
It's available and I have it. I'm very comfortable with Arduino C and the IDE, but I sure would like a brief "getting started" kind of help. It isn't at all obvious to me how to set ROBOTC for Arduino.
15  Using Arduino / Storage / List of Data Log Shield DS commands on: July 16, 2011, 01:26:35 pm
First time using the log shield and the examples are confusing to me. Is there a document with a listing of the commands and parameters ?   Thanks
Pages: [1] 2 3