Show Posts
|
|
Pages: 1 ... 9 10 [11] 12
|
|
153
|
Using Arduino / Programming Questions / Re: code question
|
on: December 04, 2011, 09:38:48 pm
|
|
although I am sure you could find this with some searching.
i is a variable set up to inside the loop (in this case an an int type) to keep track of how many times the for loop has been repeated thus far.
int i=0 //sets an initial value i<20 // if this statement is true run the loop again i++ // add one to i
and yes. it will run 20 times.
first time i will be 0 second time i will be 1 ........ ........ ........ twentieth time i will be 19 then on the twenty-first time i will be 20 which makes i < 20 false and the loop exits.
the end.
|
|
|
|
|
154
|
Using Arduino / Programming Questions / Re: Complete noob starting out, need help PLEASE
|
on: November 29, 2011, 10:33:07 pm
|
|
if you wrote a sketch in java I would be impressed. arduino are programmed in c++....
I would say start out with a plan on what you want the final product to do. them once you have an idea of the final build you can come nack and say.
"I have an arduino uno and would like to set up an automatic aquarium that would............................. What would everyone recommend to get this project started?"
you have to know what you wa t to accomplish before you start with anything else or you will work yourself in circles usually.
|
|
|
|
|
155
|
Using Arduino / General Electronics / Re: Breadboards....I don't understand.
|
on: November 29, 2011, 11:56:16 am
|
|
the arduino is a microcontroller.
is the breadboard you have solderless breadboard (white with lots of divets to push components into) or standard breadboard (usually a yellowish color with just a bunch of through holes)?
pictures would be great to make sure everyone understand what you have to work with.
|
|
|
|
|
156
|
Using Arduino / General Electronics / ps2 controller interface using fets
|
on: November 29, 2011, 11:39:31 am
|
I have been trying to interface a ps2 controller to my mega (1280) using bill porters ps2x library. I eventually manged to get a hard wired controller working. It was a pelican brand from wally world. it didn't seem to like 5 volts and I couldnt find a relaitivly simple way to convert without having to buy the tsx0104 as shown here. http://www.wegmuller.org/arduino/Arduino-Playstation_gameport.html doing some more research on level shifting I found what these guys were doing with an I2C circuit. http://www.google.com/m/url?ei=CQLVTvCZB6SuNZUr&q=http://ics.nxp.com/support/documents/interface/pdf/an97055.pdf&ved=0CBoQFjAD&usg=AFQjCNEFePVS1vee0VS57JEjTsMddGSPGA so what I ended up doing was merging these two ideas and getting it working. the hard wire model worked fine with only level shifting the command, attention, and clock line. the data line was wired directly to the arduino. I used a 2n6660 fet. tied the gate to 3.3 volts. the source tied to the controller with a 10k resistor pulling the line up to 3.3. the drain is tied directly to the arduino with no pull up. I went ahead and shifted all 4 lines when I made the shield just to be sure I wouldn't have any issues with wireless controllers. I made a quick hand drawn diagram and scanned it in for reference. It only shows one circuit but that same thing is repeated 4 times. 1 for each com line. It has been tested with the two controller i have. One wireless Pelican, One wired Pelican.
|
|
|
|
|
157
|
Using Arduino / Programming Questions / Re: ps2x in functions?
|
on: November 21, 2011, 12:30:13 pm
|
|
I see your point. In most cases i would or if I would be storing these variables inside of the function where it is needed.... which I suppose would be possible.
in this case i am using these Numbers in a call. so I would be specify in that call all needed information:
source of control, which would be my controller assignments as I know them desitantion of command, the channel on the ssc32 high and low limits, to stop motion before physical impact speed limit of joint.
so if I am always passing this into a function as a value directly from the controller I am not seeing how it would benefit the flow of the program to name is as the distination rather than the source....
maybe its just the way we see the flow in our minds.
in either case, the help is much appreciated. "
|
|
|
|
|
158
|
Using Arduino / Programming Questions / Re: ps2x in functions?
|
on: November 20, 2011, 09:50:01 pm
|
|
in this case. no.
its a ps2 controller. left x is side to side left y is up and down. same for right joystick. in my sketch they will do a few different things at different times so if I name them for the function it would be more confusing than if I refer to them as the x and y axis of the left and right joystick.
|
|
|
|
|
159
|
Using Arduino / Programming Questions / Re: ps2x in functions?
|
on: November 20, 2011, 10:55:22 am
|
understood. So i would end up with something like this in my full code..... (abridged version) int RX; int RY; int LX; int LY; int azimuth = 128;
void loop () { ps2x.read_gamepad(false, 0);
RX = ps2x.Analog(PSS_RX); RY = ps2x.Analog(PSS_RY); LX = ps2x.Analog(PSS_LX); LY = ps2x.Analog(PSS_LX);
analogMotion (RX, aximuth, 600, 2400, 1);
}
So my joint values are stored as int and refreshed each loop. but passed as integers.
|
|
|
|
|
160
|
Using Arduino / Programming Questions / Re: ps2x in functions?
|
on: November 19, 2011, 11:54:28 pm
|
|
I think i see what you are saying. But if
ps2x.Analog(PSS_RX) is an int value between 0 and 255
and analogStick = ps2x.Analog(PSS_RX)
Wouldn't map(analogStick, 0, 127, jointSpeed, 0) == map(ps2x.Analog(PSS_RX), 0, 127, jointSpeed, 0) as it does in the first set that is tested and working?
i think my error is coming in where i don't know how to make the call properly reference what value i should read form the controller for that iteration of the function. In this case the arduino would see ps2x.Analog(PSS_RX) as a string rather than a 'variable' from the library........ am i understanding your statement properly?
the same issue presents with the second portion. I am trying to replace "joint" with "azimuth" but i am getting a string instead of a variable reference.......
If i am correct in understanding you.. how would you recommend i make my call to be more functional? I am just trying to refine my coding skills so i am looking for more efficient ways to do things.
Thanks for your help.
|
|
|
|
|
161
|
Using Arduino / Programming Questions / ps2x in functions?
|
on: November 19, 2011, 09:56:50 pm
|
I have an arm from Lynxmotion i am controlling with my Mega1280 using an SSC-32 from lynxmotion. The general control scheme in my working code looks like this if (ps2x.Analog(PSS_RX) >= 0 && ps2x.Analog(PSS_RX) <= 127){ // If (Right joystick is left of center) do the below items azimuthVal = map (ps2x.Analog(PSS_RX),0,127,jointSpeed,0); // Adjusts the speed at which the joint moves relative to the amount of joystick used | To change the jointSpeed modify the value of the variable azimuthPos = azimuthPos - azimuthVal; // Take the current position of the Azmiuth joint and subtract the value needed to adjust it to the new position if (azimuthPos <= 600){ // If the value makes it to the lower limit of the possible swing of the servo azimuthPos = 600; // stop the counter at this value } Serial1.print("#1 P"); // Making string for SSC-32: Azimuth is Servo #1, P indicates the positioning wanted Serial1.println(azimuthPos); // Positioning value needed to move the servo to }
if (ps2x.Analog(PSS_RX) >= 129 && ps2x.Analog(PSS_RX) <= 255){ // Same as above but move joystick to the right instead of left azimuthVal = map (ps2x.Analog(PSS_RX),255,129,jointSpeed,0); azimuthPos = azimuthPos + azimuthVal; if (azimuthPos >= 2400){ azimuthPos = 2400; } Serial1.print("#1 P"); // Azimuth Servo is #1 Serial1.println(azimuthPos); } I have this set up for all of the servos on the arm. I call them azimuth, shoulder, elbow, wrist, and jaw. All is well with that program. It's just quite a few repetitions of the above code. I have done a proof of concept now and was looking to get better at coding so i decided to see if i could condense the long form version of the program and make a function for analog functions and a function for the digital functions. i was hoping for something like: analogMotion(ps2x.Analog(PSS_RX), azimuth, 600, 2400, 1);
void analogMotion(String analogStick,String joint,int lowStop,int highStop,int servoChannel){ if (analogStick >= 0 && analogStick <= 127){ jointVal = map(analogStick, 0, 127, jointSpeed, 0); joint = joint - jointVal; if (joint <= lowStop){ joint = lowStop; } Serial1.print(35, BYTE); //prints # to Serial Serial1.print(servoChannel, BYTE); //prints the proper servo channel number for ssc-32 Serial1.print(32, BYTE); // prints a space Serial1.print(80, BYTE); // prints the letter P Serial1.println(joint); //Print the numeric value for the servo position and a <CR> } if (analogStick >= 129 && analogStick <=255){ jointVal = map(analogStick, 255, 129, jointSpeed, 0); joint = joint + jointVal; if(joint >= highStop){ joint = highStop; } Serial1.print(35, BYTE); //prints # to Serial Serial1.print(servoChannel, BYTE); //prints the proper servo channel number for ssc-32 Serial1.print(32, BYTE); // prints a space Serial1.print(80, BYTE); // prints the letter P Serial1.println(joint); //Print the numeric value for the servo position and a <CR> } }
I have tried different types where you see String but can't seem to make it work. Any suggestions on how i could use a function for my movements and call out a certain button in the ps2x library properly? I can't seem to find a way to drop something like ps2x.Analog(PSS_RX) into the call of the function. Any help is appreciated.......
|
|
|
|
|
163
|
Using Arduino / Programming Questions / Re: please help??
|
on: February 21, 2011, 09:56:59 am
|
Ok. As an 'ok' programmer at each Labview and Arduino i will attempt to at least shine a little light on your issue to help you understand what's happening. The 0 to 5 volts you are referring to is not a PWM Output in the way you are describing it. It is an Analog Input. Labview will not output those analog inputs the way you expect and give you fine control of the LED unless you use a floating point variable and the use the map command to limit that number into the 0 to 255 range which makes it make it a bit more of a pain than it needs to be. Labview, without seperate external digital to analog conversion hardware, cannot output an analog 0 to 5 volts directly. You will be wanting Labview to output a Serial string containing the PWM value you wish to output. You then set up your arduino to take the number provided by this serial string and set it to a variable, that variable will then be written as an analog.write to the pin controlling your LED. so if your VI has a slider you want to be set to adjust the brightness of an LED. You set the slider min to 0 and max to 255 (Range of an 8 bit PWM signal). The set point of that slider then get output via serial string on the proper comm port. I have a bit of code i was playing with while in a labview class a few years ago, It need a little tweaking for proper reading of the serial string but you may be able to get a general idea of whats happening. (This VI has 3 sliders Red, Green, and Blue. The output serial string is in the format "Rxxx,Gxxx,Bxxx" where xxx is the color value 000 to 255. int red = 5; // Set pins for output int green = 7; int blue = 6;
char testVal = 0;
int buf1 = 0; // Buffer spaces for 000 to 255 int buf2 = 0; int buf3 = 0;
int redVal = 0; // To hold LED value int blueVal = 0; int greenVal = 0;
void setup (){ pinMode (red, OUTPUT); pinMode (blue, OUTPUT); pinMode (green, OUTPUT); digitalWrite (red, LOW); //LED's all off digitalWrite (blue, LOW); digitalWrite (green, LOW); Serial.begin(9600); }
void loop () { if (Serial.available () > 0 ){ // Any chars wating to be read? testVal = Serial.read (); if (testVal == 'r'){ buf1 = Serial.read(); // the Serial.read command brings the characters in as their ASCII equivilant so is stores as an intiger buf1 = buf1 - 48; // 1 becomes 49, 2 becomes 50, 7 becomes 55, etc. this line subtracts 48 to give the digit it's numeric value redVal = buf1; buf2 = Serial.read(); buf2 = buf2 - 48; redVal = (buf1 * 10) + buf2; // Converts two individual numbers into a two digit value. For example 2 then 5 would become 25
buf3 = Serial.read(); buf3 = buf3 - 48; redVal = ((buf1*10) + buf2)*10 + buf3; // Converts two individual numbers into a three digit value. For example 25 then 3 would become 253 } Serial.print("Red:"); // Serial Print for DEBUG and as feedback to Labview VI Display box Serial.println(redVal); if (testVal == 'b'){ buf1 = Serial.read(); buf1 = buf1 - 48; blueVal = buf1; buf2 = Serial.read(); buf2 = buf2 - 48; blueVal = (buf1 * 10) + buf2;
buf3 = Serial.read(); buf3 = buf3 - 48; blueVal = ((buf1*10) + buf2)*10 + buf3; } Serial.print("Blue:"); Serial.println(blueVal); if (testVal == 'g'){ buf1 = Serial.read(); buf1 = buf1 - 48; greenVal = buf1; buf2 = Serial.read(); buf2 = buf2 - 48; greenVal = (buf1 * 10) + buf2;
buf3 = Serial.read(); buf3 = buf3 - 48; greenVal = ((buf1*10) + buf2)*10 + buf3; } Serial.print("Green:"); Serial.println(greenVal); } analogWrite (red , redVal); // Ouput color value to pins analogWrite (blue , blueVal); analogWrite (green , greenVal); delay(50); // Delay 50ms }
|
|
|
|
|
164
|
Using Arduino / Project Guidance / Re: Car led system or CARduino
|
on: February 11, 2011, 05:28:28 pm
|
I have an ongoing project i work on in my free time similar to this. Here is a link to Tundra Geeks website where i have some videos, even though you can't see them too well. http://tundrageeks.com/forum/showthread.php?t=2486&page=1I think i have made some minor mods to the code since this, i have some functions in here that weren't even being called at this time. The LED's are in each front door handle of my truck, each rear door handle and on the passenger and (soon to be) driver foot well. /*
Program to do all kind of goofy crap with the LED's in my door handles
*/
const int fg = 4; // Assign each color for each handle ex: fg = Front Green and rr = Rear Red const int fb = 3; // Interior light are tied to front door handles const int fr = 2; const int rg = 7; const int rb = 6; const int rr = 5;
const int colorIn = 15; const boolean intlights = 48;
const boolean alarmIn = 52;
float colorValue = 0; //setting variables for later int repeatCount = 1; int alarmOffCount = 1; int trueValue = 0; int sampleTotal = 0; long totalValue = 0; int randomCheck;
void setup () {
pinMode (fg, OUTPUT); // Assign all pins pinMode (fb, OUTPUT); pinMode (fr, OUTPUT); pinMode (rg, OUTPUT); pinMode (rb, OUTPUT); pinMode (rr, OUTPUT);
pinMode (colorIn, INPUT); pinMode (intlights, INPUT); pinMode (alarmIn, INPUT);
digitalWrite (intlights, HIGH); //Set inputs to maintain high digitalWrite (alarmIn, HIGH);
digitalWrite (fg, LOW); // Turn all LED's off initially digitalWrite (fb, LOW); digitalWrite (fr, LOW); digitalWrite (rg, LOW); digitalWrite (rb, LOW); digitalWrite (rr, LOW);
randomSeed(analogRead(0));
Serial.begin(9600);
}
void loop () {
/* Removed because no random patterns are needed int totalPatterns = 8; //total numbers of patterns to pick from during random selection
int randomValue = random(1, totalPatterns + 1); //generate a random value for the next pattern to run
if (randomCheck == randomValue && randomCheck <= totalPatterns - 1){ //if the last pattern is the same as the one about to run go to next pattern randomValue = randomValue + 1; } else if (randomCheck == randomValue && randomCheck == totalPatterns) { //if the last pattern was the last one in the series then go back one instead randomValue = randomValue - 1; }
randomCheck = randomValue; //set a varible to make sure the same pattern doesn't repeat */
if ( digitalRead (alarmIn) == HIGH && alarmOffCount == 0 ) { alarmOffCount = 1; ledFade (fr, fb, fg, rr, rg, rb, 35, .5, 5); // white fade when truck is unlocked }
while (digitalRead (intlights) == LOW && digitalRead (alarmIn) == HIGH){ //code for interior lights to change colors selected by 5K pot
totalValue = 0; //reset variables at beginning of loop trueValue = 0;
for (int i=1; i<=50 ; i++){ //take 50 readings from Analog Input pin trueValue = analogRead(colorIn); totalValue = trueValue + totalValue; sampleTotal = i; }
colorValue = totalValue/sampleTotal; //get average of 50 readings pulled in from loop
Serial.println(colorValue); // Print average color value to serial port
intLights (0, 175, 0, rr, 0, fr); // takes a value from the pot (0-1023) and uses that value to change the colors on the led intLights (165, 345, rr, rb, fr, fb); intLights (335, 515, rb, rg, fb, fg); intLights (505, 685, rg, rr, fg, fr); intLights (675, 855, 0, rb, 0, fb); intLights (845, 1015, 0, rg, 0, fg);
}
while ( digitalRead (alarmIn) == LOW ) { // While viper is armed pin holds to low and the 4 color fade cycle repeats 100 times
alarmOffCount = 0;
for ( repeatCount=1 ; repeatCount<100 ; repeatCount++ ) { ledFade (fr, 0, 0, rr, 0, 0, 50, 2, 5); //red fade ledFade (0, fg, 0, 0, rg, 0, 30, .5, 5); //green fade ledFade (0, 0, fb, 0, 0, rb, 30, 1, 5); //blue fade ledFade (fr, fb, fg, rr, rg, rb, 30, .5, 5); // white fade
if ( digitalRead (alarmIn) == HIGH ) { break; } } }
//Notes to refer to when making calls //ledStrobe (int f1, int f2, int f3, int r1, int r2, int r3, int strobeLow, int strobeHigh, float strobeStep) //ledFade (int f1, int f2, int f3, int r1, int r2, int r3, int delayTime, float fadeIncrementOn, float fadeIncrementOff)
}
void intLights (int low, int high, int LEDlower1, int LEDraise1, int LEDlower2, int LEDraise2){ //takes the value from the pot and turns it into led values
if(colorValue == 0) { digitalWrite(rr, LOW); digitalWrite(fr, LOW);
digitalWrite(rg, LOW); digitalWrite(fg, LOW);
digitalWrite(rb, LOW); digitalWrite(fb, LOW); }
else if(colorValue >1000){ digitalWrite(rr, HIGH); digitalWrite(fr, HIGH);
digitalWrite(rg, HIGH); digitalWrite(fg, HIGH);
digitalWrite(rb, HIGH); digitalWrite(fb, HIGH); }
else if (colorValue > low && colorValue < high){
int LED1 = map (colorValue, low, high, 255, 10); int LED2 = map (colorValue, low, high, 10, 255); int LED3 = map (colorValue, low, high, 255, 10); int LED4 = map (colorValue, low, high, 10, 255);
analogWrite (LEDlower1, LED1); analogWrite (LEDraise1, LED2); analogWrite (LEDlower2, LED3); analogWrite (LEDraise2, LED4);
} }
void ledStrobe (int f1, int f2, int f3, int r1, int r2, int r3, int strobeLow, int strobeHigh, float strobeStep){
for (float strobeSpeed = strobeLow; strobeSpeed <=strobeHigh; strobeSpeed +=strobeStep){ //shift pf and df from flash speed at strobeLow to flash speed at strobeHigh digitalWrite (f1, HIGH); digitalWrite (f2, HIGH); digitalWrite (f3, HIGH); delay(strobeSpeed); digitalWrite (f1, LOW); digitalWrite (f2, LOW); digitalWrite (f3, LOW); delay(strobeSpeed); }
for (float strobeSpeed = strobeHigh; strobeSpeed >=strobeLow; strobeSpeed -=strobeStep){ //shift pf and df from flash speed at strobeHigh to flash speed at strobeLow digitalWrite (f1, HIGH); digitalWrite (f2, HIGH); digitalWrite (f3, HIGH); delay(strobeSpeed); digitalWrite (f1, LOW); digitalWrite (f2, LOW); digitalWrite (f3, LOW); delay(strobeSpeed); }
for (float strobeSpeed = strobeLow; strobeSpeed <=strobeHigh; strobeSpeed +=strobeStep){//shift pr and dr from flash speed at strobeLow to flash speed at strobeHigh digitalWrite (r1, HIGH); digitalWrite (r2, HIGH); digitalWrite (r3, HIGH); delay(strobeSpeed); digitalWrite (r1, LOW); digitalWrite (r2, LOW); digitalWrite (r3, LOW); delay(strobeSpeed); }
for (float strobeSpeed = strobeHigh; strobeSpeed >=strobeLow; strobeSpeed -=strobeStep){//shift pr and dr from flash speed at strobeHigh to flash speed at strobeLow digitalWrite (r1, HIGH); digitalWrite (r2, HIGH); digitalWrite (r3, HIGH); delay(strobeSpeed); digitalWrite (r1, LOW); digitalWrite (r2, LOW); digitalWrite (r3, LOW); delay(strobeSpeed); }
}
void ledFade (int f1, int f2, int f3, int r1, int r2, int r3, int delayTime, float fadeIncrementOn, float fadeIncrementOff){
for (float fadeValue = 0; fadeValue <=255 ; fadeValue +=fadeIncrementOn) { //fade door led's to on analogWrite (f1, fadeValue); analogWrite (f2, fadeValue); analogWrite (f3, fadeValue); analogWrite (r1, fadeValue); analogWrite (r2, fadeValue); analogWrite (r3, fadeValue); delay(delayTime);
if ( digitalRead (alarmIn) == HIGH ){ //check to see if alarm is still armed digitalWrite (fg, LOW); // Turn all LED's off digitalWrite (fb, LOW); digitalWrite (fr, LOW); digitalWrite (rg, LOW); digitalWrite (rb, LOW); digitalWrite (rr, LOW); repeatCount=1; loop (); } }
for (float fadeValue = 255; fadeValue >=0 ; fadeValue -=fadeIncrementOff) { //fade door led's to off analogWrite (f1, fadeValue); analogWrite (f2, fadeValue); analogWrite (f3, fadeValue); analogWrite (r1, fadeValue); analogWrite (r2, fadeValue); analogWrite (r3, fadeValue); delay(delayTime);
if ( digitalRead (alarmIn) == HIGH ){ //check to see if alarm is still armed digitalWrite (fg, LOW); // Turn all LED's off digitalWrite (fb, LOW); digitalWrite (fr, LOW); digitalWrite (rg, LOW); digitalWrite (rb, LOW); digitalWrite (rr, LOW); repeatCount=1; loop (); } }
digitalWrite (fg, LOW); // Turn all LED's off digitalWrite (fb, LOW); digitalWrite (fr, LOW); digitalWrite (rg, LOW); digitalWrite (rb, LOW); digitalWrite (rr, LOW);
if ( digitalRead (alarmIn) == HIGH ){ //check to see if alarm is still armed repeatCount=1; loop (); }
delay(1000);
if ( digitalRead (alarmIn) == HIGH ){//check to see if alarm is still armed repeatCount=1; loop (); }
}
|
|
|
|
|
165
|
Using Arduino / Programming Questions / Re: Analog to PWM question
|
on: February 11, 2011, 05:12:29 pm
|
I did this to smooth out my inputs form a pot for some RGB led's under the door handles of my truck. The the Serial print at the end was just for when i was playing with the code so i could watch it and see how smooth it is. totalValue = 0; //reset variables at beginning of loop trueValue = 0;
for (int i=1; i<=50 ; i++){ //take 50 readings from Analog Input pin trueValue = analogRead(colorIn); totalValue = trueValue + totalValue; sampleTotal = i; }
colorValue = totalValue/sampleTotal; //get average of 50 readings pulled in from loop
Serial.println(colorValue); // Print average color value to serial port Then I just mapped colorValue down to the 0-255 range and wrote it to the proper pin. Hope this might help you out.
|
|
|
|
|