Show Posts
|
|
Pages: [1] 2
|
|
3
|
Using Arduino / Programming Questions / Re: trouble with ELSE statement in servo project
|
on: May 06, 2013, 08:02:24 pm
|
Okay here is the whole code: XBOX CONTROLLER CONNECTIVITY WTH USB SHIELD This Started off as an Example sketch for the Xbox Wireless Reciver library - developed by Kristian Lauszus For more information about Xbox controller connectivity to the USB host shield see the blog post: http://blog.tkjelectronics.dk/2012/12/xbox-360-receiver-added-to-the-usb-host-library/ or send him an e-mail: kristianl@tkjelectronics.com
This has been modified by me Tim Bates(timcki) It supports up to four controllers wirelessly using the microsoft PC Dongle (I only use one) Since this will be used to move a pan and tilt camera mount, I have used the VarSpeedServo.h library to controll the servo speed so you dont vomit while watching the video */
#include <XBOXRECV.h> // XboX library #include <VarSpeedServo.h> // servo Speed library USB Usb; XBOXRECV Xbox(&Usb); // Create instances of type Servo. servo1 is the steering servo and servo2 is the ESC. VarSpeedServo servo1; // Create physical servo 1 VarSpeedServo servo2; // Create physical servo 2
//************************************************************************************************************************** void setup(){ pinMode(3, OUTPUT); // Assign pin 3 as an output pinMode(5, OUTPUT); // Assign pin 5 as an output servo1.attach(5,555,1555); // signal to servo on digital pin 5 servo2.attach(3,555,1555); // signal to servo2 on digital pin 3 servo1.slowmove(90,100); // tells servo1 where to go at startup and how fast to get there servo2.slowmove(90,100); // tells servo2 where to go at startup and how fast to get there { Serial.begin(115200); // Starts the serial communication if (Usb.Init() == -1) { // Starts the Xbox receiver library Serial.print(F("\r\nOSC did not start")); while(1); //halt } Serial.print(F("\r\nXbox Wireless Receiver Library Started")); } } //***************************************************************************************************************************
void loop() { Usb.Task();
if(Xbox.XboxReceiverConnected) { for(uint8_t i=0;i<4;i++)
{ if(Xbox.Xbox360Connected[i]) { if(Xbox.getButtonPress(i,L2) || Xbox.getButtonPress(i,R2)) { Serial.print("L2: "); Serial.print(Xbox.getButtonPress(i,L2)); Serial.print("\tR2: "); Serial.println(Xbox.getButtonPress(i,R2)); Xbox.setRumbleOn(i,Xbox.getButtonPress(i,L2),Xbox.getButtonPress(i,R2)); } if(Xbox.getAnalogHat(i,LeftHatX) > 7500 || Xbox.getAnalogHat(i,LeftHatX) < -7500 || Xbox.getAnalogHat(i,LeftHatY) > 7500 || Xbox.getAnalogHat(i,LeftHatY) < -7500 || Xbox.getAnalogHat(i,RightHatX) > 7500 || Xbox.getAnalogHat(i,RightHatX) < -7500 || Xbox.getAnalogHat(i,RightHatY) > 7500 || Xbox.getAnalogHat(i,RightHatY) < -7500) {
///////////////////////////////////////////////////////////////////////////////////////////////////////// if(Xbox.getAnalogHat(i,LeftHatX) > 7500 || Xbox.getAnalogHat(i,LeftHatX) < -7500) { servo1.slowmove(map(Xbox.getAnalogHat(i,LeftHatX), 0, 1024, 0, 180),60); // servo1.slowmove(90,100); Serial.print(F("LeftHatX: ")); Serial.print(Xbox.getAnalogHat(i,LeftHatX)); Serial.print("\t"); } else { servo1.write(90); } if(Xbox.getAnalogHat(i,LeftHatY) > 7500 || Xbox.getAnalogHat(i,LeftHatY) < -7500) { servo2.slowmove(map(Xbox.getAnalogHat(i,LeftHatY), 0, 1024, 180, 0),60); Serial.print(F("LeftHatY: ")); Serial.print(Xbox.getAnalogHat(i,LeftHatY)); Serial.print("\t"); } else { servo2.write(90); } if(Xbox.getAnalogHat(i,RightHatX) > 7500 || Xbox.getAnalogHat(i,RightHatX) < -7500) { Serial.print(F("RightHatX: ")); Serial.print(Xbox.getAnalogHat(i,RightHatX)); Serial.print("\t"); } if(Xbox.getAnalogHat(i,RightHatY) > 7500 || Xbox.getAnalogHat(i,RightHatY) < -7500) { Serial.print(F("RightHatY: ")); Serial.print(Xbox.getAnalogHat(i,RightHatY)); } Serial.println(); //servo1.write(90); //servo2.write(90); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(Xbox.getButtonClick(i,UP)) { servo1.slowmove(90,100); servo2.slowmove(135,100); Xbox.setLedOn(i,LED1); Serial.println(F("Up")); } if(Xbox.getButtonClick(i,DOWN)) { servo1.slowmove(90,100); servo2.slowmove(90,100); Xbox.setLedOn(i,LED4); Serial.println(F("Down")); Serial.println(F("Starting Position")); } if(Xbox.getButtonClick(i,LEFT)) { servo1.slowmove(45,100); servo2.slowmove(90,100); Xbox.setLedOn(i,LED3); Serial.println(F("Left")); } if(Xbox.getButtonClick(i,RIGHT)) { servo1.slowmove(135,100); servo2.slowmove(90,100); Xbox.setLedOn(i,LED2); Serial.println(F("Right")); }
if(Xbox.getButtonClick(i,START)) { Xbox.setLedMode(i,ALTERNATING); Serial.println(F("Start")); } if(Xbox.getButtonClick(i,BACK)) { Xbox.setLedBlink(i,ALL); Serial.println(F("Back")); } if(Xbox.getButtonClick(i,L3)) Serial.println(F("L3")); if(Xbox.getButtonClick(i,R3)) Serial.println(F("R3"));
if(Xbox.getButtonClick(i,L1)) Serial.println(F("L1")); if(Xbox.getButtonClick(i,R1)) Serial.println(F("R1")); if(Xbox.getButtonClick(i,XBOX)) { Xbox.setLedMode(i,ROTATING); Serial.print(F("Xbox (Battery: ")); Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3 Serial.println(F(")")); } if(Xbox.getButtonClick(i,SYNC)) Serial.println(F("Sync"));
if(Xbox.getButtonClick(i,A)) Serial.println(F("A")); if(Xbox.getButtonClick(i,B)) Serial.println(F("B")); if(Xbox.getButtonClick(i,X)) Serial.println(F("X")); if(Xbox.getButtonClick(i,Y)) Serial.println(F("Y")); } } } delay(15); }
The goal is to have the joystick move the camera left and right, when I let go it will return to a home position. additionally I would like a couple of buttons to go to the 45 degrees left and right. Thanks for any help. I thought I had a handle on this coding thing now I am lost again:(
|
|
|
|
|
4
|
Using Arduino / Programming Questions / trouble with ELSE statement in servo project
|
on: May 05, 2013, 11:38:32 pm
|
I am building a pan and tilt camera mount controlled with an Xbox controller. I am using these two libraries: XBOXRECV.h // XboX library VarSpeedServo.h // Variable servo speed library Okay here is the code snippet: //if(Xbox.getAnalogHat(i,LeftHatX) > 31500 || Xbox.getAnalogHat(i,LeftHatX) < -31500 || Xbox.getAnalogHat(i,LeftHatY) > 7500 || Xbox.getAnalogHat(i,LeftHatY) < -7500 || Xbox.getAnalogHat(i,RightHatX) > 7500 || Xbox.getAnalogHat(i,RightHatX) < -7500 || Xbox.getAnalogHat(i,RightHatY) > 7500 || Xbox.getAnalogHat(i,RightHatY) < -7500) { if(Xbox.getAnalogHat(i,LeftHatX) > 7500 || Xbox.getAnalogHat(i,LeftHatX) < -7500) { servo1.slowmove(map(Xbox.getAnalogHat(i,LeftHatX), 0, 1024, 0, 179),60); Serial.print(F("LeftHatX: ")); Serial.print(Xbox.getAnalogHat(i,LeftHatX)); Serial.print("\t"); } else /////////////////////////HERE { servo1.slowmove(90,90); } if(Xbox.getAnalogHat(i,LeftHatY) > 7500 || Xbox.getAnalogHat(i,LeftHatY) < -7500) { servo2.slowmove(map(Xbox.getAnalogHat(i,LeftHatY), 0,1024 , 179, 0),60); Serial.print(F("LeftHatY: ")); Serial.print(Xbox.getAnalogHat(i,LeftHatY)); Serial.print("\t"); } else //////////////////////////////////////////////////// HERE { servo2.slowmove(90,90); } if(Xbox.getAnalogHat(i,RightHatX) > 7500 || Xbox.getAnalogHat(i,RightHatX) < -7500) { Serial.print(F("RightHatX: ")); Serial.print(Xbox.getAnalogHat(i,RightHatX)); Serial.print("\t"); } if(Xbox.getAnalogHat(i,RightHatY) > 7500 || Xbox.getAnalogHat(i,RightHatY) < -7500) { Serial.print(F("RightHatY: ")); Serial.print(Xbox.getAnalogHat(i,RightHatY)); } Serial.println(); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(Xbox.getButtonClick(i,UP)) { servo1.slowmove(90,100); servo2.slowmove(135,100); Xbox.setLedOn(i,LED1); Serial.println(F("Up")); } if(Xbox.getButtonClick(i,DOWN)) { servo1.slowmove(90,100); servo2.slowmove(90,100); Xbox.setLedOn(i,LED4); Serial.println(F("Down")); Serial.println(F("Starting Position")); } if(Xbox.getButtonClick(i,LEFT)) { servo1.slowmove(45,100); servo2.slowmove(90,100); Xbox.setLedOn(i,LED3); Serial.println(F("Left")); } if(Xbox.getButtonClick(i,RIGHT)) { // servo1.slowmove(135,100); //servo2.slowmove(90,100); Xbox.setLedOn(i,LED2); Serial.println(F("Right")); } If I use the else statement the servos will return to the "Home" position but none of the other buttons on the controller will function after the else statement If I omit the else statement the servos stay in an arbitrary positions but the button functions work looking for a nudge in the right direction I can post the whole code if you need it.
|
|
|
|
|
8
|
Using Arduino / Motors, Mechanics, and Power / Knob sketch and a generic digital servo problems (beginner)
|
on: April 24, 2013, 09:30:48 pm
|
|
Playing around with the "knob" sketch from the servo library,
I am getting some "jitter" from the servo. when the pot is at 0 it is stable, when the pot is maxed the servo is also stable. When the pot is somewhere in between the servo will move to that position, however it will shake or jitter.
The servo is a generic digital servo I got from a R/C Helicopter shop. actual model is : spektrum DS821
the servo works fine with a R/C receiver.
I have powered the servo from both the arduino and an external power supply both with the same results also I get the same results from both an UNO and a Leonardo.
Do I need an analog servo ? Or do I need a better (compatible) servo from Sparkfun or Adafruit?
Thanks
|
|
|
|
|
11
|
Using Arduino / Networking, Protocols, and Devices / Trying to learn about SPI...
|
on: January 31, 2012, 11:51:39 pm
|
|
Okay, I've got a 7 segment led running from a shift register using spi... It counts from 0 to 9 and starts over
Now I want to add a button to count up or down...
Can I add an analog button to the arduino on another pin or do I have to put the button on the SPI bus?
|
|
|
|
|
12
|
Using Arduino / Programming Questions / replace delay(xxxx) with button press??
|
on: September 18, 2011, 11:52:46 pm
|
I have a 7 segment led running through a 74hc595 counting 0-9 with a small pause in between each next incremental digit. I would like to change this by adding a button press to go to the next digit up Yes I am trying to make a scoreboard... can delay(xxxx) be replaced by by adding instead the code snippet from the button tutorial I think i am having trouble understanding how to pause the shift register 74hc595 and restart it. I found this sketch on the net but I am stuck trying to modify it... int dataPin = 11; int clockPin = 12; int latchPin = 8; int len = 10; //holders for infromation you're going to pass to shifting function byte dataRED; byte dataArrayRED[10]; byte off = 0x00; byte decimalpoint = 0x80; void setup() { //set pins to output because they are addressed in the main loop pinMode(latchPin, OUTPUT); //Arduino doesn't seem to have a way to write binary straight into the code //so these values are in HEX. Decimal would have been fine, too. dataArrayRED[0] = 0x3f; //00111111 - 0 dataArrayRED[1] = 0x06; //00000110 - 1 dataArrayRED[2] = 0x5B; //01011011 - 2 dataArrayRED[3] = 0x4F; //01001111 - 3 dataArrayRED[4] = 0x66; //01100110 - 4 dataArrayRED[5] = 0x6D; //01101101 - 5 dataArrayRED[6] = 0x7D; //01111101 - 6 dataArrayRED[7] = 0x07; //00000111 - 7 dataArrayRED[8] = 0x7F; //01111111 - 8 dataArrayRED[9] = 0x67; //01100111 - 9 } void loop() { int count = 100; int val = 10; //this is for how many digits for( int i = 0; i<count; i++ ) { digitalWrite(latchPin, 0); int digit2 = i % val; int digit1 = ( i - digit2 ) / val; shiftOut(dataPin, clockPin, dataArrayRED[ digit2 ]); shiftOut(dataPin, clockPin, dataArrayRED[ digit1 ]); digitalWrite(latchPin, 1); delay(30); //code for button here############################# delay(10); } } // the heart of the program void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { // This shifts 8 bits out MSB first, //on the rising edge of the clock, //clock idles low //internal function setup int i=0; int pinState; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, OUTPUT); //clear everything out just in case to //prepare shift register for bit shifting digitalWrite(myDataPin, 0); digitalWrite(myClockPin, 0); //for each bit in the byte myDataOut… //NOTICE THAT WE ARE COUNTING DOWN in our for loop //This means that %00000001 or "1" will go through such //that it will be pin Q0 that lights. for (i=7; i>=0; i--) { digitalWrite(myClockPin, 0); //if the value passed to myDataOut and a bitmask result // true then... so if we are at i=6 and our value is // %11010100 it would the code compares it to %01000000 // and proceeds to set pinState to 1. if( myDataOut & (1<<i) ) { pinState= 1; } else { pinState= 0; } //Sets the pin to HIGH or LOW depending on pinState digitalWrite(myDataPin, pinState); //register shifts bits on upstroke of clock pin digitalWrite(myClockPin, 1); //zero the data pin after shift to prevent bleed through digitalWrite(myDataPin, 0); } //stop shifting digitalWrite(myClockPin, 0); }
|
|
|
|
|
15
|
Using Arduino / Programming Questions / Trying to make/understand font file for adding to a sketch
|
on: April 22, 2011, 06:19:10 pm
|
Let me start with Coding is not my strong point.. I have a sketch for a 8x8 led matrix to display scrolling text. (I did not write it, just borrowed it  ) (Thank you Andrew who ever you are) the letters displayed on the matrix are defined in the sketch (see below) I can see font .h files added to other peoples sketches, I assume the whole alphabet + numbers and symbols is defined in the file. In this sketch the letters are a 5x7 font defined in decimal form EX: #define R {127, 72, 76, 74, 49} Is there a better way to do this? Every time I change the sketch to display something new I have to add a new #define of the symbol,letter or whatever. Can I make a Complete font file that can be called by importing a library within a sketch? what is the best format for defining letters,numbers, and symbols? I have seen the letter "A" defined different ways, hex, decimal, binary..... how do you call the letters, to be used from a font file to the sketch? Remember, coding is not my strong point Thank Tim /* * Show messages on an 8x8 led matrix (cathode rows), * scrolling from right to left. * * Pinouts & code by Andrew */
// pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1) int pins[17]= {-1, 5, 4, 3, 2, 14, 15, 16, 17, 8, 9, 10, 11, 12, 13, 6, 7}; // (for some reason I ended up with different pinouts that Andrew's)
// col[xx] of leds = pin yy on led matrix int cols[8] = {pins[13], pins[3], pins[4], pins[10], pins[06], pins[11], pins[15], pins[16]};
// row[xx] of leds = pin yy on led matrix int rows[8] = {pins[9], pins[14], pins[8], pins[12], pins[1], pins[7], pins[2], pins[5]};
// Letter definitions based on 5 bit-wise columns (5 x 7 font)
#define SM {98,97,9,97,98} // Smiley #define LE {0,251,251,0,0} //Thick Exclamation #define SP {0, 0, 0, 0, 0} //Space #define EX {0, 125, 0, 0, 0} // ! #define PE {1,0,0,0,0} //. #define A {31, 36, 68, 36, 31} #define B {127, 73, 73, 73, 54} #define C {62, 65, 65, 65, 34} #define D {127, 65, 65, 34, 28} #define E {127, 73, 73, 65, 65} #define F {127, 72, 72, 72, 64} #define G {62, 65, 65, 69, 38} #define H {127, 8, 8, 8, 127} #define I {0, 65, 127, 65, 0} #define J {2, 1, 1, 1, 126} #define K {127, 8, 20, 34, 65} #define L {127, 1, 1, 1, 1} #define M {127, 32, 16, 32, 127} #define N {127, 32, 16, 8, 127} #define O {62, 65, 65, 65, 62} #define P {127, 72, 72, 72, 48} #define Q {62, 65, 69, 66, 61} #define R {127, 72, 76, 74, 49} #define S {50, 73, 73, 73, 38} #define T {64, 64, 127, 64, 64} #define U {126, 1, 1, 1, 126} #define V {124, 2, 1, 2, 124} #define W {126, 1, 6, 1, 126} #define X {99, 20, 8, 20, 99} #define Y {96, 16, 15, 16, 96} #define Z {67, 69, 73, 81, 97}
int dispSpeed = 10; // Constrols scroll speed (1 minimum, way too fast) byte bit[8] = {128, 64, 32, 16, 8, 4, 2, 1}; // Used for bit comparisons byte colVals[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // Hold display columns (initaly blank)
// Define display string here const int charNum = 25; // Number of letters in display string //byte string[charNum][5] = {LE,SP,SP}; byte string[charNum][5] = {T,I,M,SP,I,S,SP,T,H,E,SP,G,R,E,A,T,E,S,T,LE,SP,SM,SP,SP}; //byte string[charNum][5] = {A,R,D,U,I,N,O,SP,R,O,C,K,S,EX,SP,SP};
void setup() { Serial.begin(9600); // for troubleshooting for (int i = 1; i <= 16; i++) { // sets the pins as output pinMode(pins[i], OUTPUT); } for (int col = 0; col < 8; col++) { // set up cols and rows digitalWrite(cols[col], HIGH); } for (int row = 1; row < 8; row++) { digitalWrite(rows[row], HIGH); } Serial.println(availableMemory()); // 670 bytes for charNum = 15 }
void loop() {
for (int ltr = 0; ltr < charNum; ltr++){// For each letter in string array for (int y = 0; y < 6; y++){ // For each columin in letter + one space shiftLeft(); // shifts display columns left if (y < 5){ colVals[7] = string[ltr][y]; } // add new letter column on right else {colVals[7] = 0; } // or empty space between for (int x = 0; x < dispSpeed; x++){// loop to refresh display x times ton control scrolling display(); } } } }
void shiftLeft(){ for (int x = 0; x < 7; x++){ colVals[x] = colVals[x + 1]; } }
void display() {
for (int col = 0; col < 8; col++){ for (int row = 0; row < 8; row++){ if (colVals[col] & bit[row]){ digitalWrite(rows[row], HIGH);} } digitalWrite(cols[col], LOW); // Turn on column delay(1); // Delay for POV for (int row = 0; row < 8; row++){ digitalWrite(rows[row], LOW); } digitalWrite(cols[col], HIGH); // And off } }
// this function will return the number of bytes currently free in RAM // written by David A. Mellis // based on code by Rob Faludi http://www.faludi.com int availableMemory() { int size = 1024; byte *buf; while ((buf = (byte *) malloc(--size)) == NULL); free(buf); return size; }
|
|
|
|
|