Show Posts
Pages: [1] 2 3 ... 9
1  Topics / Device Hacking / Re: rc hack arduino as a variable resistor on: March 20, 2014, 02:21:19 pm
Grumpy Mike is correct.
I did this hack not too long ago and found that even with filtering the arduino could not control the RC transmitter (arruming your tapping into the pot/joystick leads.
The unfiltered PWM is digital and the radio is able to detect the highs and lows and get confused.
The pwm filtered approach made it better but by the time it was filtered enough it also caused significant lag in response.

Using a real digital resistor is the way to go.  I was able to get very clean results.  I believe I found ones that could do SPI and I2C
the if you need lots of digital pots the I2C didn't have enough addresses so I couldn't use it.
Also some arduino bords have digital pots built in I believe so if you only need 1 or 2 you might be able to get it done without any additional work.
2  Using Arduino / Networking, Protocols, and Devices / two devices sharing a serial line on: December 23, 2013, 01:02:41 pm
I was wondering if its possible to hook up two devices to the arduinos tx/rx lines.
I have a touchscreen lcd monitor from sparkfun that receives and sends info via serial pins 0/1
I also plan to use an xbee module to connect this device to a transmitting unit... also serial data.

Is it necessary to use a larger arduino with multiple serial ports or can I  hook them up to the same serial line...
The xbee transmits header lines and the screen is also looking for specific headers....

The reason I thought it could possibly work is that the xbee is only receiving data not transmitting so no data to get scrambled on the tx.  but when data comes in there will be packets from the xbee as well as serial commands coming from the lcd control library to the screen.

Will the packets stay intact or will they get scrambled if the arduino tries to talk to the monitor at the same time xbee data is coming in???

products:

https://www.sparkfun.com/products/11741

https://www.sparkfun.com/products/10854
3  Using Arduino / Programming Questions / variables fed into a function on: November 04, 2013, 12:16:20 pm
I have a simple question about functions.
Up until now I have been assigning say integer variables above the Setup() function and just calling the same variables within custom functions:

Ex:
Code:
int data=0;

void setup() {
Serial.begin(9600);
}

void loop()
{
addVariable();
Serial.print("variable is now: ");
Serial.println(data);
delay(10);
}

void addVariable()
{
 data++;
  }

I am not to sure I understand the reason for doing something like this instead:

Ex2:
Code:
int data=0;

void setup() {
Serial.begin(9600);
}

void loop()
{
addVariable(data);
Serial.print("variable is now: ");
Serial.println(data);
delay(10);
}

void addVariable(int data)
{
 data++;
  }

Is there an advantage with passing data into a function as opposed to accessing a global variable??
4  Topics / Education and Teaching / Re: What is the best way for intermediate arduino users to raise our skill level? on: October 28, 2013, 12:50:08 am
Ill post some sample code tomorrow from work.
I'm very open to all kinds if criticism as long as its for the better good...
If I can learn from you guys bring it on!
5  Products / Arduino Due / Re: Ultra simple library for Adafruit DAC breakout board wont run on DUE on: October 24, 2013, 08:03:48 am
Thanks for the info... I thought it might just be a syntax type issue not processor related... I think the solution is beyond my current knowledge level.

As for the DACs I need 10 total so the two internals don't do it:(
6  Products / Arduino Due / Ultra simple library for Adafruit DAC breakout board wont run on DUE on: October 23, 2013, 10:57:59 am
Hi I have to use some DAC's from Adafruit on a project but the DUE does not like the library...
It is such a short library code that I thought maybe one of you could help spot the problem for me.
Thanks in Advance!

Adafruit_MCP4725.cpp library code:
Code:
/**************************************************************************/
/*!
    @file     Adafruit_MCP4725.cpp
    @author   K.Townsend (Adafruit Industries)
@license  BSD (see license.txt)

I2C Driver for Microchip's MCP4725 I2C DAC

This is a library for the Adafruit MCP4725 breakout
----> https://www.adafruit.com/products/???

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

@section  HISTORY

    v1.0 - First release
*/
/**************************************************************************/
#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif

#include <Wire.h>

#include "Adafruit_MCP4725.h"

/**************************************************************************/
/*!
    @brief  Instantiates a new MCP4725 class
*/
/**************************************************************************/
Adafruit_MCP4725::Adafruit_MCP4725() {
}

/**************************************************************************/
/*!
    @brief  Setups the HW
*/
/**************************************************************************/
void Adafruit_MCP4725::begin(uint8_t addr) {
  _i2caddr = addr;
  Wire.begin();

}
 
/**************************************************************************/
/*!
    @brief  Sets the output voltage to a fraction of source vref.  (Value
            can be 0..4095)

    @param[in]  output
                The 12-bit value representing the relationship between
                the DAC's input voltage and its output voltage.
    @param[in]  writeEEPROM
                If this value is true, 'output' will also be written
                to the MCP4725's internal non-volatile memory, meaning
                that the DAC will retain the current voltage output
                after power-down or reset.
*/
/**************************************************************************/
void Adafruit_MCP4725::setVoltage( uint16_t output, bool writeEEPROM )
{
  uint8_t twbrback = TWBR;
  TWBR = 12; // 400 khz
  Wire.beginTransmission(_i2caddr);
  if (writeEEPROM)
  {
    Wire.write(MCP4726_CMD_WRITEDACEEPROM);
  }
  else
  {
    Wire.write(MCP4726_CMD_WRITEDAC);
  }
  Wire.write(output / 16);                   // Upper data bits          (D11.D10.D9.D8.D7.D6.D5.D4)
  Wire.write((output % 16) << 4);            // Lower data bits          (D3.D2.D1.D0.x.x.x.x)
  Wire.endTransmission();
  TWBR = twbrback;
}

Adafruit_MCP4725.h library code:
Code:
/**************************************************************************/
/*!
    @file     Adafruit_MCP4725.h
    @author   K. Townsend (Adafruit Industries)
@license  BSD (see license.txt)

This is a library for the Adafruit MCP4725 breakout board
----> https://www.adafruit.com/products/???

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

@section  HISTORY

    v1.0  - First release
*/
/**************************************************************************/

#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif

#include <Wire.h>

#define MCP4726_CMD_WRITEDAC            (0x40)  // Writes data to the DAC
#define MCP4726_CMD_WRITEDACEEPROM      (0x60)  // Writes data to the DAC and the EEPROM (persisting the assigned value after reset)

class Adafruit_MCP4725{
 public:
  Adafruit_MCP4725();
  void begin(uint8_t a);  
  void setVoltage( uint16_t output, bool writeEEPROM );

 private:
  uint8_t _i2caddr;
};

SIMPLISTIC TEST SKETCH (Only errors out compiling for DUE):
Code:
/**************************************************************************/
/*!
    @file     trianglewave.pde
    @author   Adafruit Industries
    @license  BSD (see license.txt)

    This example will generate a triangle wave with the MCP4725 DAC.   

    This is an example sketch for the Adafruit MCP4725 breakout board
    ----> https://www.adafruit.com/products/???
 
    Adafruit invests time and resources providing this open source code,
    please support Adafruit and open-source hardware by purchasing
    products from Adafruit!
*/
/**************************************************************************/
#include <Wire.h>
#include <Adafruit_MCP4725.h>

Adafruit_MCP4725 dac;

void setup(void) {
  Serial.begin(9600);
  Serial.println("Hello!");

  // For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VCC)
  // For MCP4725A0 the address is 0x60 or 0x61
  // For MCP4725A2 the address is 0x64 or 0x65
  dac.begin(0x62);
   
  Serial.println("Generating a triangle wave");
}

void loop(void) {
    uint32_t counter;
    // Run through the full 12-bit scale for a triangle wave
    for (counter = 0; counter < 4095; counter=counter+5)
    {
      dac.setVoltage(counter, false);
   
    }
    for (counter = 4095; counter > 0; counter=counter-5)
    {
      dac.setVoltage(counter, false);
     
    }
}

ERROR MESSAGE (sketch errors out only on DUE):

C:\Users\fxmech\Documents\Arduino\libraries\Adafruit_MCP4725\Adafruit_MCP4725.cpp: In member function 'void Adafruit_MCP4725::setVoltage(uint16_t, bool)':
C:\Users\fxmech\Documents\Arduino\libraries\Adafruit_MCP4725\Adafruit_MCP4725.cpp:67: error: 'TWBR' was not declared in this scope
7  Topics / Education and Teaching / What is the best way for intermediate arduino users to raise our skill level? on: October 23, 2013, 10:20:45 am
Hi,
I've been programming Arduino and Processing for a few years now.
I am self taught (with lots of help from this forum of course).
I started doing simple RGB light control which I introduced to my work and it quickly evolved into some pretty complex stuff involving GUI's, servo control, Sound generating, R/C control of parameters and sequences, Wireless communication, etc

The more I do the more that is expected so I feel its time to actually learn how to do it right.

Up until now I havent done any planning.  I design in my head and code as I go.

My code is very simplistic but the end product is very complex so I am starting to feel the hardware limitations probably because my code is not as efficient as it should be.

Also I have no experience outside of the main arduino environment so looking inside a library is like trying to read a foreign language.  anything involving HEX or binary is also very foreign to me.

I feel that its time to take a step backwards and learn fundamentals in my spare time but am not sure what te best way to go about it is.

Online tutorials and books have been a great source of information, but if they discuss subjects that are foreign to me, its very hard to fill in the blanks.

What I feel I need is a live course, tutoring, or maybe an online course where the basics are covered, such as base structuring, as well as advanced subjects such as non linear programming, working outside of the arduino toolsets, etc

In order to keep developing my skills at the pace my job requires, I have focused my attention specifically on learning things that apply specifically to the tasks I need to perform, but I feel I've missed some Key fundamental steps along the way.


Any ideas, links, etc would be extremely appreciated!!!
8  Using Arduino / Networking, Protocols, and Devices / Re: Trouble receiving Serial data correctly from two serial ports on: October 22, 2013, 07:29:09 pm
Ill post data tomorrow when Im back at work.
One thing that throws me off with serial communication is that even when I have the code working well and displaying good data on the screen (data in through Serial 2 and 3 and data out to Serial monitor via Serial0)
when I'm done troubleshooting and comment out the Serial 0 port lines... suddenly the data is not the same...
But I cant see why...  just know because it makes the device stop responding to the data coming in.

Is there a better way to troubleshoot serial data so as to not affect the functionality of the code????
9  Using Arduino / Networking, Protocols, and Devices / Re: Trouble receiving Serial data correctly from two serial ports on: October 22, 2013, 11:11:36 am
RX CODE:
Code:
// VR GLOVE RX SKETCH
// Based on Nick Gammons code www.gammon.com.au/forum/?id=11425

typedef enum { 
  NONE, GOT_A, GOT_B, GOT_C, GOT_D, GOT_E, GOT_F, GOT_AA, GOT_BB, GOT_CC, GOT_DD, GOT_EE, GOT_FF}
states;
states stateA = NONE;
states stateB = NONE;
unsigned int currentValueA;
unsigned int currentValueB;
int currentA;
int currentB;
int currentC;
int currentD;
int currentE;
int currentF;
int currentAA;
int currentBB;
int currentCC;
int currentDD;
int currentEE;
int currentFF;

void setup() {
  Serial.begin(57600);  //initialize serial for displaying data to screen
  Serial2.begin(57600); //initialize serial for receiving Sensor unit A data
  Serial3.begin(57600); //initialize serial for receiving Sensor unit B data
}

void loop()
{
  if (Serial2.available ())
    processIncomingByteA (Serial2.read ());
  if (Serial3.available ())
    processIncomingByteB (Serial3.read ());

  //DEBUG
  Serial.print("  currentA=");
  Serial.print(currentA);
  /*
Serial.print("  currentB=");Serial.print(currentB);
   Serial.print("  currentC=");Serial.print(currentC);
   Serial.print("  currentD=");Serial.print(currentD);
   Serial.print("  currentE=");Serial.print(currentE);
   Serial.print("  currentF=");Serial.print(currentF);
   Serial.print("  currentAA=");Serial.print(currentAA);
   Serial.print("  currentBB=");Serial.print(currentBB);
   Serial.print("  currentCC=");Serial.print(currentCC);
   Serial.print("  currentDD=");Serial.print(currentDD);
   Serial.print("  currentEE=");Serial.print(currentEE);
   Serial.print("  currentFF=");Serial.print(currentFF);
   */
  Serial.println();
  //delay(10);
}


void processA (const unsigned int valueA)
{
  currentA=valueA;
}

void processB (const unsigned int valueA)
{
  currentB=valueA;
}

void processC (const unsigned int valueA)
{
  currentC=valueA;
}

void processD (const unsigned int valueA)
{
  currentD=valueA;
}

void processE (const unsigned int valueA)
{
  currentE=valueA;
}

void processF (const unsigned int valueA)
{
  currentF=valueA;
}

void processAA (const unsigned int valueB)
{
  currentAA=valueB;
}

void processBB (const unsigned int valueB)
{
  currentBB=valueB;
}

void processCC (const unsigned int valueB)
{
  currentCC=valueB;
}

void processDD (const unsigned int valueB)
{
  currentDD=valueB;
}

void processEE (const unsigned int valueB)
{
  currentEE=valueB;
}

void processFF (const unsigned int valueB)
{
  currentFF=valueB;
}

void handlePreviousStateA ()
{
  switch (stateA)
  {
  case GOT_A:
    processA (currentValueA);
    break;
  case GOT_B:
    processB (currentValueA);
    break;
  case GOT_C:
    processC (currentValueA);
    break;
  case GOT_D:
    processD (currentValueA);
    break;
  case GOT_E:
    processE (currentValueA);
    break;
  case GOT_F:
    processF (currentValueA);
    break;

  }  // end of switch 

  currentValueA = 0;
}  // end of handlePreviousState


void handlePreviousStateB ()
{
  switch (stateB)
  {
  case GOT_AA:
    processAA (currentValueB);
    break;
  case GOT_BB:
    processBB (currentValueB);
    break;
  case GOT_CC:
    processCC (currentValueB);
    break;
  case GOT_DD:
    processDD (currentValueB);
    break;
  case GOT_EE:
    processEE (currentValueB);
    break;
  case GOT_FF:
    processFF (currentValueB);
    break;

  }  // end of switch 

  currentValueB = 0;
}  // end of handlePreviousState



void processIncomingByteA (const byte c)
{
  if (isdigit (c))
  {
    currentValueA *= 10;
    currentValueA += c - '0';
  }  // end of digit
  else
  {

    // The end of the number signals a state change
    handlePreviousStateA ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'A':
      stateA = GOT_A;
      break;
    case 'B':
      stateA = GOT_B;
      break;
    case 'C':
      stateA = GOT_C;
      break;
    case 'D':
      stateA = GOT_D;
      break;
    case 'E':
      stateA = GOT_E;
      break;
    case 'F':
      stateA = GOT_F;
      break;

    default:
      stateA = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit 

} // end of processIncomingByte



void processIncomingByteB (const byte c)
{
  if (isdigit (c))
  {
    currentValueB *= 10;
    currentValueB += c - '0';
  }  // end of digit
  else
  {

    // The end of the number signals a state change
    handlePreviousStateB ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'A':
      stateB = GOT_AA;
      break;
    case 'B':
      stateB = GOT_BB;
      break;
    case 'C':
      stateB = GOT_CC;
      break;
    case 'D':
      stateB = GOT_DD;
      break;
    case 'E':
      stateB = GOT_EE;
      break;
    case 'F':
      stateB = GOT_FF;
      break;

    default:
      stateB = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit 

} // end of processIncomingByte





10  Using Arduino / Networking, Protocols, and Devices / Re: Trouble receiving Serial data correctly from two serial ports on: October 22, 2013, 11:11:12 am
Here is an updated sketch set (RX/TX sketches)
I changed minor things like adding debug sections to both sketches and made it so it autocalibrates at startup (SETUP())

I tried a few tests without the delay but my data was completely wrong.

The delay in the Tx side was the only thing that corrected it.  With no delay it looked like the Rx end was not catching all of the line endings and id get data that should read:

A=1234
looking like
A=1234
A=123412...
A=1234123....
A=1234....
etc

The Tx delay started at 10 and by the time it was up to 20 the data was perfectly clean.

PaulS  maybe you can chime in here... I know you understand this better than me so there must be ore to it...  Im just going by trial and error...


TX CODE:
Code:

//VR GLOVE TX SKETCH


//limits for sensor range
int digit1UL;
int digit1LL;
int digit2UL;
int digit2LL;
int digit3UL;
int digit3LL;
int digit4UL;
int digit4LL;
int digit5UL;
int digit5LL;
int buttonA;
int buttonB;
int buttonC;
int thumbSensorReading1;
int indexSensorReading2;
int middleSensorReading3;
int ringSensorReading4;
int pinkySensorReading5;
int del=20; // delay if needed to keep serial from overflowing
int aux; //aux switch value (on/off)

void setup(){
  Serial.begin(57600);
  pinMode(3, INPUT); //Lower Limit
  pinMode(4, INPUT);// Upper limit
  pinMode(6, INPUT);// Aux switch

  buttonC=digitalRead(6);//Aux switch
  digit1UL = analogRead(A0)+50;
  digit1LL = digit1UL-100;
  digit2UL = analogRead(A0)+50;
  digit2LL = digit2UL-100;
  digit3UL = analogRead(A0)+50;
  digit3LL = digit3UL-100;
  digit4UL = analogRead(A0)+50;
  digit4LL = digit4UL-100;
  digit5UL = analogRead(A0)+50;
  digit5LL = digit5UL-100;
}

void loop(){
  // read all sensors
  buttonA=digitalRead(3);// Upper limit
  buttonB=digitalRead(4);//Lower limit
  buttonC=digitalRead(6);//Aux switch
  thumbSensorReading1 = analogRead(A0);
  indexSensorReading2 = analogRead(A1);
  middleSensorReading3 = analogRead(A2);
  ringSensorReading4 = analogRead(A3);
  pinkySensorReading5 = analogRead(A4);

  //calibrate unit
  if (buttonA==HIGH && buttonB==LOW){ //Button A pressed = high limit set
    digit1UL=thumbSensorReading1;
    digit2UL=indexSensorReading2;
    digit3UL=middleSensorReading3;
    digit4UL=ringSensorReading4;
    digit5UL=pinkySensorReading5;
    //Serial.println("BUTTON A IS HIGH");
  }

  if (buttonB==HIGH && buttonA==LOW){ //Button B pressed = low limit set
    digit1LL=thumbSensorReading1;
    digit2LL=indexSensorReading2;
    digit3LL=middleSensorReading3;
    digit4LL=ringSensorReading4;
    digit5LL=pinkySensorReading5;
    //Serial.println("BUTTON B IS HIGH");
  }

  if (buttonC==HIGH ) //Button C pressed = Aux ON
    aux=1024;
  else aux=0;


  //modify sensor values based on calibration parameters
  int adjustedReading1=map(thumbSensorReading1,digit1LL,digit1UL,1024,0);
  adjustedReading1=constrain(adjustedReading1,0,1024);
  int adjustedReading2=map(indexSensorReading2,digit2LL,digit2UL,1024,0);
  adjustedReading2=constrain(adjustedReading2,0,1024);
  int adjustedReading3=map(middleSensorReading3,digit3LL,digit3UL,1024,0);
  adjustedReading3=constrain(adjustedReading3,0,1024);
  int adjustedReading4=map(ringSensorReading4,digit4LL,digit4UL,1024,0);
  adjustedReading4=constrain(adjustedReading4,0,1024);
  int adjustedReading5=map(pinkySensorReading5,digit5LL,digit5UL,1024,0);
  adjustedReading5=constrain(adjustedReading5,0,1024);


  //OUTPUT DATA TO SERIAL PORT
  Serial.print('A'); //Header character to let receiver know what sensor the value received is associated with
  Serial.print(adjustedReading1); // send actual sensor value

  Serial.print('B');
  Serial.print(adjustedReading2);

  Serial.print('C');
  Serial.print(adjustedReading3);

  Serial.print('D');
  Serial.print(adjustedReading4);

  Serial.print('E');
  Serial.print(adjustedReading5);

  Serial.print('F'); // this is the auxillary switch (on or off)
  Serial.println(aux);
  delay(del);


  //DEBUG
  /*
   Serial.print("thumbSensorReading1=");
   Serial.print(thumbSensorReading1);
   Serial.print("   indexSensorReading2=");
   Serial.print(indexSensorReading2);
   Serial.print("   middleSensorReading3=");
   Serial.print(middleSensorReading3);
   Serial.print("   ringSensorReading4=");
   Serial.print(ringSensorReading4);
   Serial.print("   pinkySensorReading5=");
   Serial.println(pinkySensorReading5);
   Serial.print("   digit1UL=");
   Serial.print(digit1UL);
   Serial.print("   digit1LL=");
   Serial.print(digit1LL);
   Serial.print("   digit2UL=");
   Serial.print(digit2UL);
   Serial.print("   digit2LL=");
   Serial.print(digit2LL);
   Serial.print("   digit3UL");
   Serial.print(digit3UL);
   Serial.print("   digit3LL");
   Serial.print(digit3LL);
   Serial.print("   digit4UL=");
   Serial.print(digit4UL);
   Serial.print("   digit4LL=");
   Serial.print(digit4LL);
   Serial.print("   digit5UL=");
   Serial.print(digit5UL);
   Serial.print("   digit5LL=");
   Serial.println(digit5LL);
   Serial.print("   adjustedReading1="); //Header character to let receiver know what sensor the value received is associated with
   Serial.print(adjustedReading1); // send actual sensor value
   Serial.print("   adjustedReading2=");
   Serial.print(adjustedReading2);
   Serial.print("   adjustedReading3=");
   Serial.print(adjustedReading3);
   Serial.print("   adjustedReading4=");
   Serial.print(adjustedReading4);
   Serial.print("   adjustedReading5=");
   Serial.print(adjustedReading5);
   Serial.print("   adjustedReading5="); // this is the auxillary switch (on or off)
   Serial.print(aux);
   Serial.println();
   */
}











11  Using Arduino / Networking, Protocols, and Devices / Re: Trouble receiving Serial data correctly from two serial ports on: October 21, 2013, 07:18:06 pm
Thanks PaulS will correct it
12  Using Arduino / Networking, Protocols, and Devices / Re: Trouble receiving Serial data correctly from two serial ports on: October 21, 2013, 06:15:59 pm
For those of you with similar issues here is my final code... working good!!!
its based on Nick Gammons excellent tutorial and code examples which he linked earlier in this thread.

TX code:

Code:
//limits for sensor range
int digit1UL=330;
int digit1LL=330;
int digit2UL=330;
int digit2LL=330;
int digit3UL=330;
int digit3LL=330;
int digit4UL=330;
int digit4LL=330;
int digit5UL=330;
int digit5LL=330;

int del=10; // delay if needed to keep serial from overflowing
int aux; //aux switch value (on/off)

void setup(){
  Serial.begin(57600);
  pinMode(3, INPUT); //Lower Limit
  pinMode(4, INPUT);// Upper limit
  pinMode(6, INPUT);// Aux switch
}

void loop(){
  // read all sensors
  int buttonA=digitalRead(3);// Upper limit
  int buttonB=digitalRead(4);//Lower limit
  int buttonC=digitalRead(6);//Aux switch
  int thumbSensorReading1 = analogRead(A0);
  int indexSensorReading2 = analogRead(A1);
  int middleSensorReading3 = analogRead(A2);
  int ringSensorReading4 = analogRead(A3);
  int pinkySensorReading5 = analogRead(A4);

  //calibrate unit
  if (buttonA==HIGH && buttonB==LOW){ //Button A pressed = high limit set
    digit1UL=thumbSensorReading1;
    digit2UL=indexSensorReading2;
    digit3UL=middleSensorReading3;
    digit4UL=ringSensorReading4;
    digit5UL=pinkySensorReading5;
  }

  if (buttonB==HIGH && buttonA==LOW){ //Button B pressed = low limit set
    digit1LL=thumbSensorReading1;
    digit2LL=indexSensorReading2;
    digit3LL=middleSensorReading3;
    digit4LL=ringSensorReading4;
    digit5LL=pinkySensorReading5;
  }

  if (buttonC==HIGH ) //Button C pressed = Aux ON
    aux=1024;
  else aux=0;


  //modify sensor values based on calibration parameters
  int adjustedReading1=map(thumbSensorReading1,digit1LL,digit1UL,1024,0);
  adjustedReading1=constrain(adjustedReading1,0,1024);
  int adjustedReading2=map(indexSensorReading2,digit2LL,digit2UL,1024,0);
  adjustedReading2=constrain(adjustedReading2,0,1024);
  int adjustedReading3=map(middleSensorReading3,digit3LL,digit3UL,1024,0);
  adjustedReading3=constrain(adjustedReading3,0,1024);
  int adjustedReading4=map(ringSensorReading4,digit4LL,digit4UL,1024,0);
  adjustedReading4=constrain(adjustedReading4,0,1024);
  int adjustedReading5=map(pinkySensorReading5,digit5LL,digit5UL,1024,0);
  adjustedReading5=constrain(adjustedReading5,0,1024);


  //OUTPUT DATA TO SERIAL PORT
  Serial.print('A'); //Header character to let receiver know what sensor the value received is associated with
  Serial.print(adjustedReading1); // send actual sensor value
 
  Serial.print('B');
  Serial.println(adjustedReading2);
 
  Serial.print('C');
  Serial.println(adjustedReading3);
 
  Serial.print('D');
  Serial.println(adjustedReading4);
 
  Serial.print('E');
  Serial.println(adjustedReading5);
 
  Serial.print('F'); // this is the auxillary switch (on or off)
  Serial.println(aux);
  delay(del);
}







RX code:
Code:
int del=0;  //delay value if needed
typedef enum {  NONE, GOT_A, GOT_B, GOT_C, GOT_D, GOT_E, GOT_F, GOT_AA, GOT_BB, GOT_CC, GOT_DD, GOT_EE, GOT_FF} states;
states stateA = NONE;
states stateB = NONE;
unsigned int currentValueA;
unsigned int currentValueB;

void setup() {
  Serial.begin(115200);  //initialize serial for displaying data to screen
  Serial2.begin(57600); //initialize serial for receiving Sensor unit A data
  Serial3.begin(57600); //initialize serial for receiving Sensor unit B data
}

void loop()
{
  if (Serial2.available ())
    processIncomingByteA (Serial2.read ());
      if (Serial3.available ())
    processIncomingByteB (Serial3.read ());

  //delay(del); //delay if needed

}


void processA (const unsigned int valueA)
{
  Serial.print ("    1A = ");
  Serial.print (valueA);
}

void processB (const unsigned int valueA)
{
   Serial.print ("    1B = ");
  Serial.print (valueA);
}

void processC (const unsigned int valueA)
{
   Serial.print ("    1C = ");
  Serial.print (valueA); 
}

void processD (const unsigned int valueA)
{
  Serial.print ("    1D = ");
  Serial.print (valueA);
}

void processE (const unsigned int valueA)
{
  Serial.print ("    1E = ");
  Serial.print (valueA);
}

void processF (const unsigned int valueA)
{
  Serial.print ("    1F = ");
  Serial.print (valueA);
}

void processAA (const unsigned int valueB)
{
  Serial.print ("    2A = ");
  Serial.print (valueB);
}

void processBB (const unsigned int valueB)
{
   Serial.print ("    2B = ");
  Serial.print (valueB);
}

void processCC (const unsigned int valueB)
{
   Serial.print ("    2C = ");
  Serial.print (valueB); 
}

void processDD (const unsigned int valueB)
{
  Serial.print ("    2D = ");
  Serial.print (valueB);
}

void processEE (const unsigned int valueB)
{
  Serial.print ("    2E = ");
  Serial.print (valueB);
}

void processFF (const unsigned int valueB)
{
  Serial.print ("    2F = ");
  Serial.print (valueB);
}

void handlePreviousStateA ()
{
  switch (stateA)
  {
  case GOT_A:
    processA (currentValueA);
    break;
  case GOT_B:
    processB (currentValueA);
    break;
  case GOT_C:
    processC (currentValueA);
    break;
      case GOT_D:
    processD (currentValueA);
    break;
      case GOT_E:
    processE (currentValueA);
    break;
      case GOT_F:
    processF (currentValueA);
    break;
     
  }  // end of switch 

  currentValueA = 0;
}  // end of handlePreviousState


void handlePreviousStateB ()
{
  switch (stateB)
  {
  case GOT_AA:
    processAA (currentValueB);
    break;
  case GOT_BB:
    processBB (currentValueB);
    break;
  case GOT_CC:
    processCC (currentValueB);
    break;
      case GOT_DD:
    processDD (currentValueB);
    break;
      case GOT_EE:
    processEE (currentValueB);
    break;
      case GOT_FF:
    processFF (currentValueB);
    break;
     
  }  // end of switch 

  currentValueB = 0;
}  // end of handlePreviousState



void processIncomingByteA (const byte c)
{
  if (isdigit (c))
  {
    currentValueA *= 10;
    currentValueA += c - '0';
  }  // end of digit
  else
  {

    // The end of the number signals a state change
    handlePreviousStateA ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'A':
      stateA = GOT_A;
      break;
    case 'B':
      stateA = GOT_B;
      break;
    case 'C':
      stateA = GOT_C;
      break;
          case 'D':
      stateA = GOT_D;
      break;
    case 'E':
      stateA = GOT_E;
      break;
    case 'F':
      stateA = GOT_F;
      break;

    default:
      stateA = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit 
 
} // end of processIncomingByte



void processIncomingByteB (const byte c)
{
  if (isdigit (c))
  {
    currentValueB *= 10;
    currentValueB += c - '0';
  }  // end of digit
  else
  {

    // The end of the number signals a state change
    handlePreviousStateB ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'A':
      stateB = GOT_AA;
      break;
    case 'B':
      stateB = GOT_BB;
      break;
    case 'C':
      stateB = GOT_CC;
      break;
          case 'D':
      stateB = GOT_DD;
      break;
    case 'E':
      stateB = GOT_EE;
      break;
    case 'F':
      stateB = GOT_FF;
      break;

    default:
      stateB = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit 
 
} // end of processIncomingByte



The Tx code is loaded into each of the two arduinos serving as part of the transmitters.
It spits out a letter (A-F) which represents a label for each of 5 sensors and a switch (F)
followed by the value of each sensor (for switch F it either spits out a 0 or a 1024 for ON/OFF)

The RX end reads Serial port 2 and 3 and processes the incoming data and prints it to Serial0 at double the baud rate.
It prints  something like (1F= 1024 or 2A=247)
the first 1 or 2 tells us what port the data was received from.  The letter is the specific sensor label and then the last numbers are the values for that sensor on that port.

You should be able to easily modify the code to suit your needs!!!!
13  Using Arduino / Networking, Protocols, and Devices / Re: Trouble receiving Serial data correctly from two serial ports on: October 15, 2013, 06:45:31 pm
Ok your code is nice!!!

What I am doing is building a set of VR gloves taking analog sensor data from 10 fingers.
Each hand has its own transmitter.. each receives sensor data calibrates it maps it and sends it out as a serial stream wirelessly to a single receiving unit that reads each transmitter on a separate serial port.  I built in an option to go to wired mode in case of interference so I can check my data both ways and they both seem very similar... so interference is not an issue right now.

The reason for all of the prefixes is that each letter represented a finger.
I initially had A,B,C,D,E,F,G,H,I,J,K,&L as my prefixes,
but in the new code I used only A-F on both gloves that way if any piece of hardware ever fails the transmitters can be interchangeable.

In the receiving sketch I still separated the two by calling an 'A' reading from hand #2 'AA' , 'B' reading 'BB', etc
but ultimately it works!!!  just don't quite grasp why the data packets are not alternating from right hand to left hand and back evenly....
14  Using Arduino / Networking, Protocols, and Devices / Re: Trouble receiving Serial data correctly from two serial ports on: October 15, 2013, 06:36:46 pm
Didn't see you replied Nick,
Ill check out your latest sample code.... what I just posted is not taking into account your last post smiley

Paul, always nice hearing from you smiley-wink
15  Using Arduino / Networking, Protocols, and Devices / Re: Trouble receiving Serial data correctly from two serial ports on: October 15, 2013, 06:30:39 pm
Final-ish code:

Receiving from both serial ports, no mixed up data, but only issue is that its not read port A, read port B, read port A, etc
its pretty random how it samples the two ports.

I tried it in both wired and wireless configurations and still get un even sampling...

All I did was take Nicks code and duplicate it....

Code:
int del=0;  //delay value if needed
typedef enum {  NONE, GOT_A, GOT_B, GOT_C, GOT_D, GOT_E, GOT_F, GOT_AA, GOT_BB, GOT_CC, GOT_DD, GOT_EE, GOT_FF} states;
states stateA = NONE;
states stateB = NONE;
unsigned int currentValueA;
unsigned int currentValueB;

void setup() {
  Serial.begin(115200);  //initialize serial for displaying data to screen
  Serial2.begin(57600); //initialize serial for receiving Sensor unit A data
  Serial3.begin(57600); //initialize serial for receiving Sensor unit B data
}

void loop()
{
  if (Serial2.available ())
    processIncomingByteA (Serial2.read ());
      if (Serial3.available ())
    processIncomingByteB (Serial3.read ());

  //delay(del); //delay if needed

}


void processA (const unsigned int valueA)
{
  Serial.print ("1A = ");
  Serial.println (valueA);
}

void processB (const unsigned int valueA)
{
   Serial.print ("1B = ");
  Serial.println (valueA);
}

void processC (const unsigned int valueA)
{
   Serial.print ("1C = ");
  Serial.println (valueA);  
}

void processD (const unsigned int valueA)
{
  Serial.print ("1D = ");
  Serial.println (valueA);
}

void processE (const unsigned int valueA)
{
  Serial.print ("1E = ");
  Serial.println (valueA);
}

void processF (const unsigned int valueA)
{
  Serial.print ("1F = ");
  Serial.println (valueA);
}

void processAA (const unsigned int valueB)
{
  Serial.print ("2A = ");
  Serial.println (valueB);
}

void processBB (const unsigned int valueB)
{
   Serial.print ("2B = ");
  Serial.println (valueB);
}

void processCC (const unsigned int valueB)
{
   Serial.print ("2C = ");
  Serial.println (valueB);  
}

void processDD (const unsigned int valueB)
{
  Serial.print ("2D = ");
  Serial.println (valueB);
}

void processEE (const unsigned int valueB)
{
  Serial.print ("2E = ");
  Serial.println (valueB);
}

void processFF (const unsigned int valueB)
{
  Serial.print ("2F = ");
  Serial.println (valueB);
}

void handlePreviousStateA ()
{
  switch (stateA)
  {
  case GOT_A:
    processA (currentValueA);
    break;
  case GOT_B:
    processB (currentValueA);
    break;
  case GOT_C:
    processC (currentValueA);
    break;
      case GOT_D:
    processD (currentValueA);
    break;
      case GOT_E:
    processE (currentValueA);
    break;
      case GOT_F:
    processF (currentValueA);
    break;
    
  }  // end of switch  

  currentValueA = 0;
}  // end of handlePreviousState


void handlePreviousStateB ()
{
  switch (stateB)
  {
  case GOT_AA:
    processAA (currentValueB);
    break;
  case GOT_BB:
    processBB (currentValueB);
    break;
  case GOT_CC:
    processCC (currentValueB);
    break;
      case GOT_DD:
    processDD (currentValueB);
    break;
      case GOT_EE:
    processEE (currentValueB);
    break;
      case GOT_FF:
    processFF (currentValueB);
    break;
    
  }  // end of switch  

  currentValueB = 0;
}  // end of handlePreviousState



void processIncomingByteA (const byte c)
{
  if (isdigit (c))
  {
    currentValueA *= 10;
    currentValueA += c - '0';
  }  // end of digit
  else
  {

    // The end of the number signals a state change
    handlePreviousStateA ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'A':
      stateA = GOT_A;
      break;
    case 'B':
      stateA = GOT_B;
      break;
    case 'C':
      stateA = GOT_C;
      break;
          case 'D':
      stateA = GOT_D;
      break;
    case 'E':
      stateA = GOT_E;
      break;
    case 'F':
      stateA = GOT_F;
      break;

    default:
      stateA = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit  
  
} // end of processIncomingByte



void processIncomingByteB (const byte c)
{
  if (isdigit (c))
  {
    currentValueB *= 10;
    currentValueB += c - '0';
  }  // end of digit
  else
  {

    // The end of the number signals a state change
    handlePreviousStateB ();

    // set the new state, if we recognize it
    switch (c)
    {
    case 'A':
      stateB = GOT_AA;
      break;
    case 'B':
      stateB = GOT_BB;
      break;
    case 'C':
      stateB = GOT_CC;
      break;
          case 'D':
      stateB = GOT_DD;
      break;
    case 'E':
      stateB = GOT_EE;
      break;
    case 'F':
      stateB = GOT_FF;
      break;

    default:
      stateB = NONE;
      break;
    }  // end of switch on incoming byte
  } // end of not digit  
  
} // end of processIncomingByte






SAMPLE DATA:

2E = 1024
2F = 0
2A = 1024
1A = 1024
2B = 1024
1B = 1024
2C = 1024
1C = 1024
2D = 1024
1D = 1024
2E = 1024
1E = 1024
2F = 0
1F = 0
1A = 1024
1B = 1024
1C = 1024
1D = 1024
1E = 1024
1F = 0
2A = 1024
2B = 1024
2C = 1024
1A = 1024
Pages: [1] 2 3 ... 9