Show Posts
|
|
Pages: [1] 2 3
|
|
2
|
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. #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; }
|
|
|
|
|
3
|
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
|
|
|
|
|
4
|
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. #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 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.
|
|
|
|
|
7
|
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
|
|
|
|
|
8
|
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?
|
|
|
|
|
13
|
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 / Project Guidance / Re: tlc5940 with 75 rgb LEDs
|
on: July 10, 2011, 06:57:30 am
|
|
For the wiring you might consider using one 5940 to drive all three colors in 5 physically consecutive sconces. Part of the reasoning is to keep the 5940s separate from each other. I have learned the hard way that the 5 lines that interconnect the 5940s radiate (400khz which is the pwm frequency in the 5940) and can generate "cross-talk" like interference which in turns causes very erratic behavior of the leds with no relation to you program.
Driving the 3 colors from the same 5940 is really transparent since all you're doing is specifying a point 0-47.
The 5940 is a current sink, therefore common anode would be best. Also, depending on the current draw of the leds, you might simplify things by connecting directly to the 5940 with transistors. If they are 25 ma or less, then the load will be under the max of 130 ma.
|
|
|
|
|