- First, I must admit I was quite tired lately, and I didn't actually wanted to omit the additional NPN transistor you suggested: I just didn't notice the difference between our circuits :/ ^^
- Then, I must say I'm pretty happy with 123d Circuits ( once you know how it works & that it 'll gets additional functionalities, plus the kindness of one of their team's dude I've been chatting with )
- Finally, I had the chance to get my hands on a Kymera Wand ( http://www.thewandcompany.com/ ) and I did some silly quick & dirty code to control the sourcing-sinking transisor / ljoy Y axis with it, & thought it may interest some readers
Kymera Wand code:
Code: Select all
/*
== Arduino Micro - sourcing-sinking test ==
R: pins 3,5,6,9,10,11 are the PWM-capable on USBboarduino
R: pins 3,5,6,9,10,11,13 are the PWM-capable pins on Arduino Micro
The "sourcing sinking transistors" stuff is to simulate a joystick axis
The "Kymera Wand mix" stands for the handling of IR codes to be used as alternative to serial cmds
The following code is intented to receive IR codes from any IR remote ( including the Kymera wand ;p )
The processing is can still be done on the laptop, but we also directly parse the IR codes received
The 'states' hold the value corresponding to the action associated
aka: states[4] = "255" => state 5: run forward
states[3] = "192" => state 4: walk forward
states[2] = "128"||"0" => state 3: steady
states[1] = "64" => state 2: walk backward
states[0] = "1" => state 1: run backward
@StephaneAG - 2015
*/
// lib necessary to receive IR codes
#include <IRremote.h>
// == IR, Kymera Wand states & vars ==
// IR
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
// Kymera
int states[] = {1, 64, 128, 192, 255}; // the said-states
int currState = 2; // 3rd state
int currKymVal = 128; // the value to be used as ljoy Y axis value
// == pin configuration ==
// Joysticks
// LJOY Y AXIS
const int ljoyY_dirPin = 7; // #D7 for DIR on Arduino Micro - source or sink
const int ljoyY_pwmPin = 6; // #D6 for PWM on Arduino Micro - how much to source or sink
// == states ==
// timeout=None
// chunks LJOYY
// ex 128
// formerly ljoyYAxis
// default: 0/ 128
int read_chunk_LJOYY = 0; // chunk #1 ( ljoyYAxis )
void setup() {
// init serial
Serial.begin(9600); // may adjust the baud later for efficiency ( .. )
// == IR ==
irrecv.enableIRIn(); // Start the receiver
// == pins modes ==
// Joysticks - desactivated
pinMode(ljoyY_dirPin, OUTPUT);
pinMode(ljoyY_pwmPin, OUTPUT);
//initJoystickAxis(ljoyY_pwmPin);
analogWrite(ljoyY_pwmPin, 255);
Serial.println("booted !");
// == debug pins mapping ==
// R: the debug voltages are reset on first serial command received
// Joysticks
}
void loop() {
// == apply last settings ==
// we moved the code present in the fcn handling a serial buffer stuf
// it was doing the following after encountering a "\n" in the said-buffer
//handleJoystickAxis( read_chunk_LJOYY, ljoyY_dirPin, ljoyY_pwmPin ); // updates the left joystick Y axis
// == reset ==
// we reset the controls to their defaults before receiving any command from the serial
// we do so to bypass the need to catch key releases on the device controlling the Arduino
// aka, no need for callbacks or trap keys if we use a simple bash script ..
// although, for analog values, it could be nice to have the increase/decrease gradually to their default ( smoothly )
// last though: we could also have left them "as is" until new serial call ( .. )
read_chunk_LJOYY = 0; // chunk #1 ( ljoyYAxis )
// == read from the Kymera Wand ==
if (irrecv.decode(&results)) {
// increase or decrease the current state based on the Kym IR codes
if( results.value == 0x8038A602 && currState < 4 ) currState++;
else if( results.value == 0x80382601 && currState > 0 ) currState--;
// get the value corresponding to that state
currKymVal = states[currState];
// use that value as if it was coming from the serial conn, to update LJOYY chunk
read_chunk_LJOYY = currKymVal;
handleJoystickAxis( read_chunk_LJOYY, ljoyY_dirPin, ljoyY_pwmPin ); // updates the left joystick Y axis
// ACK
Serial.print("Kymera/");
Serial.print(results.value, HEX);
Serial.print("/");
Serial.print(currState);
Serial.print("/");
Serial.print(currKymVal);
Serial.println("");
// Receive the next value
irrecv.resume();
}
// == read from the Serial conn ==
/*
while(Serial.available() > 0){ // read any serial available
// == readings ==
// LJOYY
read_chunk_LJOYY = Serial.parseInt(); // chunk #1 ( ljoyYAxis )
// look for a line ending
if( Serial.read() == '\n' ){
// == update ==
// R: constrain is used to keep in the 0-255 range
// Joysticks
//
// = theorically / expected by Xbox Controller S =
// fully forward 4.6V
// nothing 2.3V
// fully backward 0.0V
//handleJoystickAxis( read_chunk_LJOYY, ljoyY_dirPin, ljoyY_pwmPin ); // updates the left joystick Y axis
// == ack ==
Serial.print("ljoyY:"); Serial.print(read_chunk_LJOYY); Serial.println("");
}
}
*/
// == other update ==
//
//Serial.println("Hy world !");
//delay(2000); // temporary delay for debug
}
/* == helper fcn(s) == */
// init a joystick axis
void initJoystickAxis( int pwmPin ){
analogWrite(pwmPin, 0);
}
// accept a value from 0 to 256 & 2 pins to handle a joystick axis
// - maps the value to a direction & a pwm value
// - updates the 2 pins to reflect the value passed
void handleJoystickAxis(int value, int dirPin, int pwmPin){
if( value == 128 ){
Serial.print("value is 128 => don't source nor sink .."); Serial.println("");
analogWrite(pwmPin, 0); // whatever the direction, don't source nor sink
}
else if( value > 128 && value <= 256 ) { // forward => source
Serial.print("value greater than 128 => sourcing .."); Serial.println("");
digitalWrite( dirPin, LOW ); // source
analogWrite( pwmPin, map( value, 128, 256, 256, 0 ) ); // 128~256 => 0~256 for pwm forward
}
else if( value < 128 && value >= 0 ) { // backward => sink
Serial.print("value smaller than 128 => sinking .."); Serial.println("");
digitalWrite( dirPin, HIGH ); // sink
analogWrite( pwmPin, map( value, 0, 128, 256, 0 ) ); // 0~128 => 256~0 for pwm backward
}
}
Can't wait to receive my 1st PCB ;)
+