Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Problem Connecting 2 XBees on: November 05, 2013, 07:19:45 pm
Hi,

I'm trying to set up two XBees to communicate to each other to do some simple if, then statements to generate actions.  Essentially, I would like to press one button on the Arduino connected to an XBee, and have it light another LED connected to a separate Arduino wired to another XBee.  I've tried the example from sparkFun's website (http://bildr.org/2011/04/arduino-xbee-wireless/) and quadruple checked my wiring, but I'm just not getting any responses from the Arduinos.  

I'm using the Arduino Pro mini 3.3 V, and two of the XBee Series 1.  I'm trying to avoid reconfiguring the sensors, and would be fine with the standard setup on the XBees.  Also I would like to use this without a shield, so I'm wiring directly to Rx, Tx, Gnd, and Vcc (3.3v).

I also tried to write my own very simply code here:

Code:
int buttonPin = 12;
int ledPin = 13;

int buttonState;
int buttonPressed;

void setup(){

  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);


  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

void loop(){

  buttonState = digitalRead(buttonPin);

  if(buttonState == LOW){
    digitalWrite(ledPin, HIGH);
    Serial.println(1);  
  }

  else{
    Serial.print(0);
    digitalWrite(ledPin, LOW);
  }

  buttonPressed = Serial.read();

  if (buttonPressed == 1){
    digitalWrite(ledPin, HIGH);
  }
  else{
    digitalWrite(ledPin, LOW);
  }
}


This is the Bildr code:

Code:
//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Turning the pot on one Arduino, dims the LED on all the others on the network
//////////////////////////////////////////////////////////////////

#define potPin 3
#define ledPin 11


int inByte = -1;
char inString[6];
int stringPos = 0;
int lastValue = 0;


void setup()  {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
   inByte = Serial.read();

   //only send new value if it is different than last sent value.
   //Min value change of 5 neded. Just so we dont flood the network
   int potVal = analogRead(potPin);
   if( abs(potVal - lastValue) > 5){
     Serial.println(potVal);  
     lastValue = potVal;
   }

  //if there is any numerical serial available, store that
  if((inByte >= '0') && (inByte <= '9')){
    inString[stringPos] = inByte;
    stringPos ++;
  }

  //if there is a line end character, this string is done, take value and write to LED pin
  //clear the string when done
  if(inByte == '\r'){
    int brightness = atoi(inString); //convert string to int

    //incoming will be a range of 0-1023, we need 0-255
    brightness = map(brightness, 0, 1023, 0, 255);
    analogWrite(ledPin, brightness);


    //clear the values from inString
    for (int c = 0; c < stringPos; c++){
      inString[c] = 0;
    }
    stringPos = 0;
  }

}
2  Using Arduino / Programming Questions / Re: Communicating with I2C Slave that has a Sensor connected to the I2C pins on: October 06, 2013, 05:24:27 pm
I see.  Thanks for the info
3  Using Arduino / Programming Questions / Re: Communicating with I2C Slave that has a Sensor connected to the I2C pins on: October 06, 2013, 05:17:47 pm
Ideally, I'd like to use the slave arduino to communicate with the sensor (which is set up to use I2C), then have  the slave arduino send the sensor data to the master arduino using I2C.
4  Using Arduino / Programming Questions / Communicating with I2C Slave that has a Sensor connected to the I2C pins on: October 06, 2013, 04:28:54 pm
Hi,

I'm trying to create a modular garment that takes Arduino swatches and interfaces them together using the I2C.  I'm building a main (Master) swatch that has an Arduino Pro Mini 3.3V, LiPo Battery, and SD Card Reader.  From there I have a sensor swatch that uses the Arduino Pro Mini 3.3V and the Adafruit TSL2561 Light Sensor - http://learn.adafruit.com/tsl2561/

I'm trying to read the data from the swatch with the light sensor, then I would like the Arduino on that swatch to send the data to my main swatch so that it can record it.  Problem I'm running into is that the light sensor swatch is using the I2C to integrate the sensor to that Arduino, and when I open the Serial Monitor on my Master Arduino, the data is not right (this is what I get: ÿÿÿÿÿÿÿÿÿÿ........).

Any ideas on how I can send the data to my main Arduino without adding the light sensing code to the Master Arduino?

Here's what I'm running on the Slave Arduino:

Code:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561.h>

/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor),
 which provides a common 'type' for sensor data and some helper functions.
 
 To use this driver you will also need to download the Adafruit_Sensor
 library and include it in your libraries folder.
 
 You should also assign a unique ID to this sensor for use with
 the Adafruit Sensor API so that you can identify this particular
 sensor in any data logs, etc.  To assign a unique ID, simply
 provide an appropriate value in the constructor below (12345
 is used by default in this example).
 
 Connections
 ===========
 Connect SCL to analog 5
 Connect SDA to analog 4
 Connect VDD to 3.3V DC
 Connect GROUND to common ground
 
 I2C Address
 ===========
 The address will be different depending on whether you leave
 the ADDR pin floating (addr 0x39), or tie it to ground or vcc.
 The default addess is 0x39, which assumes the ADDR pin is floating
 (not connected to anything).  If you set the ADDR pin high
 or low, use TSL2561_ADDR_HIGH (0x49) or TSL2561_ADDR_LOW
 (0x29) respectively.
 
 History
 =======
 2013/JAN/31  - First version (KTOWN)
 */



Adafruit_TSL2561 myLightSensor = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 12345);


uint16_t broadband = 0;    //variables for the raw sesor data
uint16_t infrared = 0;



//Constants and Variables for keeping track of time:

// macros from DateTime.h
/* Useful Constants */
#define SECS_PER_MIN  (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY  (SECS_PER_HOUR * 24L)

/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN) 
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY) 

int days;
int hours;
int minutes;
int seconds;

int motorPin = 5;

void setup(void)
{
  Wire.begin(2);                // join i2c bus with address #2
  Serial.begin(9600);
  Serial.println("Light Sensor Test");
  Serial.println("");

  /* Initialise the sensor */
  if(!myLightSensor.begin())
  {
    /* There was a problem detecting the ADXL345 ... check your connections */
    Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
    while(1);
  }

  /* Display some basic information on this sensor */
  displaySensorDetails();

  /* Setup the sensor gain and integration time */
  configureSensor();

  /* We're ready to go! */
  Serial.println("");

  pinMode(13, OUTPUT);


}




void loop(void)

 
 
  /* Gets a new reading from the sensor */
  sensors_event_t event;
  myLightSensor.getEvent(&event);

  /* Gets the raw sensor data from each sensor -note this is not neccessary if you are only reading Lux */
  myLightSensor.getLuminosity (&broadband, &infrared);

  /* Display the results (light is measured in lux) */
  if (event.light)
  {
    Serial.print(event.light);                             // 'event.light' is the number of Lux measured in SI
    Serial.print(" lux  |  ");
    Serial.print(infrared);                                // 'infrared' is the raw sensor data coming from the Infrared Sensor
    Serial.print(" infrared  |  ");
    Serial.print(broadband);                               // 'broadband' is the raw sensor data coming from the Full Spectrum Light Sensor
    Serial.print( " full spectrum  |  ");

    int lux = event.light;

    //Send data to Master Arduino with SD card
    Wire.write(lux);                             // 'event.light' is the number of Lux measured in SI
    Wire.write(" lux  |  ");
    Wire.write(infrared);                                // 'infrared' is the raw sensor data coming from the Infrared Sensor
    Wire.write(" infrared  |  ");
    Wire.write(broadband);                               // 'broadband' is the raw sensor data coming from the Full Spectrum Light Sensor
    Wire.write( " full spectrum  |  ");



  }
  else
  {
    /* If event.light = 0 lux the sensor is probably saturated
     and no reliable data could be generated! */
    Serial.println("Sensor overload");
  }

  //function that displays the amount of time the arduino has been on.  Shouldn't need to change this function
  time(millis() / 1000);

  delay(250);
}






/**************************************************************************/
/*
    Displays some basic information on this sensor from the unified
 sensor API sensor_t type (see Adafruit_Sensor for more information)
 */
/**************************************************************************/
void displaySensorDetails(void)
{
  sensor_t sensor;
  myLightSensor.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       ");
  Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   ");
  Serial.println(sensor.version);
  Serial.print  ("Unique ID:    ");
  Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    ");
  Serial.print(sensor.max_value);
  Serial.println(" lux");
  Serial.print  ("Min Value:    ");
  Serial.print(sensor.min_value);
  Serial.println(" lux");
  Serial.print  ("Resolution:   ");
  Serial.print(sensor.resolution);
  Serial.println(" lux"); 
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

/**************************************************************************/
/*
    Configures the gain and integration time for the TSL2561
 */
/**************************************************************************/
void configureSensor(void)
{
  /* You can also manually set the gain or enable auto-gain support */
  // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
  // tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  myLightSensor.enableAutoGain(true);          /* Auto-gain ... switches automatically between 1x and 16x */

  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  //myLightSensor.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  myLightSensor.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // myLightSensor.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

  /* Update these values depending on what you've set above! */
  Serial.println("------------------------------------");
  Serial.print  ("Gain:         ");
  Serial.println("Auto");
  Serial.print  ("Timing:       ");
  Serial.println("13 ms");
  Serial.println("------------------------------------");
}



void time(long val){ 

  days = elapsedDays(val);
  hours = numberOfHours(val);
  minutes = numberOfMinutes(val);
  seconds = numberOfSeconds(val);

  // digital clock display of current time
  Serial.print("time since active = ");
  Serial.print(days,DEC); 
  printDigits(hours); 
  printDigits(minutes);
  printDigits(seconds);
  Serial.println(); 

}

void printDigits(byte digits){
  // utility function for digital clock display: prints colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits,DEC); 
}


And the Master:

Code:

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  Wire.requestFrom(2, 12);    // request 6 bytes from slave device #2

  while(Wire.available())    // slave may send less than requested
  {
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}



Thanks for the help, I need this up and running by 9am :-)
5  Using Arduino / Programming Questions / Re: invalid conversion from 'int*' to 'long int' on: March 27, 2013, 11:34:05 pm
Ahhh!  Thanks for the help!  It was asking for the second array to be called for.  Thanks so much, I've been staring at that for too long!
6  Using Arduino / Programming Questions / Re: invalid conversion from 'int*' to 'long int' on: March 27, 2013, 11:04:00 pm
Not quite sure I understand you correctly.  This line:

Code:
long velocity = map(encoderPos,targetEncoderPos[currentPatternIndex]/2,targetEncoderPos,maxVelocity,minVelocity);


is generating the errors as well; I thought I was calling for each element in the array?  I'm incrementing the array in the beginning of the sketch:

Code:
currentPatternIndex = (currentPatternIndex + 1) % numPatterns;

7  Using Arduino / Programming Questions / invalid conversion from 'int*' to 'long int' on: March 27, 2013, 10:36:54 pm
I'm stumped here.  I got some help with this code to do a little bit of math to control motor speed using a rotary encoder.  I have a target encoder position that I set, and the arduino does the math for the motor velocity and sends it there.  When I just have one number it works fine.  When I try to implement a set of numbers using an array for Arduino to cycle through, I get the error:

invalid conversion from 'int*' to long int'


MFA_Code__after_baker_fix_.ino: In function 'void loop()':
MFA_Code__after_baker_fix_:164: error: invalid conversion from 'int*' to 'long int'
MFA_Code__after_baker_fix_:164: error: initializing argument 3 of 'long int map(long int, long int, long int, long int, long int)'
MFA_Code__after_baker_fix_:178: error: invalid operands of types 'int [4]' and 'int' to binary 'operator/'


Any ideas?  This is a bit beyond my skill set.



Code:

#include <PWM.h>


const int encoderPin  = 2;       // set to the Arduino's internal interrupter.  Cannont be changed
const int pwmPin    = 9;        // pwm signal sent to the H-Bridge that controls the motor speed
const int motorDir = 10;          // pin for setting direction of motor.  analogWrite 0/255
int motorSpeed = 0;

int32_t frequency = 30000; //frequency (in Hz)  (this quiets the motor noise)


int maxVelocity = 255;              // represents the top speed of the motor
int minVelocity = 0;                // represents the starting speed of the motor




const int numPatterns = 4;
int currentPatternIndex = 0;

int targetEncoderPos[numPatterns] = {        // represents the distance the motor has to travel.  This will be the end point
  30, 60, 80, 40};                              // stay under 90.  The limit switch is at 97

int peakForwardWait[numPatterns] = {
  1000, 2000, 3000, 4000 };

int peakBackwardWait[numPatterns] = {
  1000, 2000, 3000, 4000 };

int waitForForward[numPatterns] = {
  1000, 2000, 3000, 4000 };

int waitForBackward[numPatterns] = {
  3000, 4000, 5000, 4000 };


const int NUM_STATES = 8;

const int WAITING_FOR_FORWARD  = 0;
const int FORWARD_0            = 1;
const int FORWARD_WAIT         = 2;
const int FORWARD_1            = 3;
const int WAITING_FOR_BACKWARD = 4;
const int BACKWARD_0           = 5;
const int BACKWARD_WAIT        = 6;
const int BACKWARD_1           = 7;

int directionStatus = WAITING_FOR_FORWARD;

long nextEvent = 0;

long encoderKickStart = 300;        // since we're relying on the encoder to move to determine timing actions, we need to tell the arduino that
// it has moved before we start or it will never move. 

volatile int encoderPos = encoderKickStart;


void setup() {

  Serial.begin(9600);


  InitTimersSafe();

  //sets the frequency for the specified pin
  bool success = SetPinFrequencySafe(pwmPin, frequency);

  //if the pin frequency was set successfully, turn pin 13 on
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);   
  }



  pinMode(encoderPin, INPUT);
  //turn on pullup resistor
  digitalWrite(encoderPin, HIGH);
  //encoder pin on interrupt 0 (pin 2)
  attachInterrupt(0, doEncoder, RISING);


}


void loop() {

  long now = millis();



          if(directionStatus == WAITING_FOR_FORWARD) {
            if(now > nextEvent) {                  //implement array values in here.  setting the value of all peaks/waves for sequence.  Change the array index here
              currentPatternIndex = (currentPatternIndex + 1) % numPatterns;
              directionStatus = FORWARD_0;
              Serial.println("FORWARD_0");
            }
            else {
              Serial.println("WAITING_FOR_FORWARD");
            }
          }
          else if(directionStatus == WAITING_FOR_BACKWARD) {
            if(now > nextEvent) {
              directionStatus = BACKWARD_0;
              Serial.println("BACKWARD_0");
            }
            else {
              Serial.println("WAITING_FOR_BACKWARD");
            }
          }
          else if(directionStatus == FORWARD_WAIT) {
            if(now > nextEvent) {
              directionStatus = FORWARD_1;
              encoderPos = targetEncoderPos[currentPatternIndex]/2;
              Serial.println("FORWARD_1");
            }
            else {
              Serial.println("FORWARD_WAIT");
            }
          }
          else if(directionStatus == BACKWARD_WAIT) {
            if(now > nextEvent) {
              directionStatus = BACKWARD_1;
              encoderPos = targetEncoderPos[currentPatternIndex]/2;
              Serial.println("BACKWARD_1");
            }
            else {
              Serial.println("BACKWARD_WAIT");
            }
          }
          else {
            // where are we on the slope?   
            if(encoderPos >= targetEncoderPos[currentPatternIndex] - encoderKickStart) {
              if(directionStatus == FORWARD_1) {
                nextEvent = now + waitForBackward[currentPatternIndex]; 
                directionStatus = WAITING_FOR_BACKWARD;
                Serial.println("WAITING_FOR_BACKWARD");

                encoderPos = encoderKickStart;
              }
              else if(directionStatus == BACKWARD_1) {
                nextEvent = now + waitForForward[currentPatternIndex];
                directionStatus = WAITING_FOR_FORWARD;
                Serial.println("WAITING_FOR_FORWARD");
                encoderPos = encoderKickStart;
              }
              else {
                Serial.println("XXXX problem");
              }
            }
            else if(encoderPos > targetEncoderPos[currentPatternIndex] / 2 - encoderKickStart) {
              if(directionStatus == FORWARD_0) {
                nextEvent = now + peakForwardWait[currentPatternIndex];
                directionStatus = FORWARD_WAIT;
                Serial.println("FORWARD_WAIT");
              }
              else if(directionStatus == BACKWARD_0) {
                nextEvent = now + peakBackwardWait[currentPatternIndex];
                directionStatus = BACKWARD_WAIT;
                Serial.println("BACKWARD_WAIT");
              }
              else {
                //  the down slope
                long velocity = map(encoderPos,targetEncoderPos[currentPatternIndex]/2,targetEncoderPos,maxVelocity,minVelocity);
                if(directionStatus == BACKWARD_1) {
                  backward(velocity);
                }
                else if(directionStatus == FORWARD_1) {
                  forward(velocity);
                }
                else {
                  Serial.println("!!!!!!");
                }
              }
            }
            else {
              // the up slope
              long velocity = map(encoderPos,0,targetEncoderPos / 2,minVelocity,maxVelocity);
              if(directionStatus == BACKWARD_0) {
                backward(velocity);
              }
              else if(directionStatus == FORWARD_0) {
                forward(velocity);
              }
              else {
                Serial.println("?????????");
              }
            }
          }

          // 
          //  Serial.print(encoderPos);
          //  Serial.print(" / ");
          //  Serial.print(targetEncoderPos);
          //  Serial.print(" = ");
          //  Serial.print(normPosition);
          //  Serial.print(" : ");
          //  Serial.print(mappedNormPosition);
          //  Serial.print(" : ");
          //  Serial.println(velocity);

//        }
//      }
//    }
//  }
}




void doEncoder() {
  encoderPos++;
}

void forward(int val) {
  analogWrite(motorDir, 255);
 //   pwmWrite(pwmPin, val);
  analogWrite(pwmPin, val);
}

void backward(int val) {
  analogWrite(motorDir, 0);
  // pwmWrite(pwmPin, val);
  analogWrite(pwmPin, val);


}

[code]
[/code]
8  Using Arduino / Programming Questions / Re: PWM to limit current to bipolar stepper on: March 27, 2013, 10:30:43 pm
You might just want a lower current wall wart.  The Arduino analogWrite will change your average current, but it will more directly change your voltage. So if you need a 12v power supply for the stepper, the PWM from Arduino will drop the voltage and give your problems with the stepper.  Or you could build a current limiting circuit on your device, but that will probably also get hot.  If you're just testing and you have no other power supply, try hooking up a few other devices to your supply to drop the current.
9  Using Arduino / General Electronics / Re: Problem with motor noise & Pololu motor driver on: March 20, 2013, 05:58:26 pm
So the frequency change worked great for getting rid of the motor noise, however with the higher frequency, I loose motor torque.  Any ideas?
10  Using Arduino / General Electronics / Re: Problem with motor noise & Pololu motor driver on: March 20, 2013, 01:40:52 pm
Changing the PWM Frequency helped tremendously!  I used this library:

http://arduino.cc/forum/index.php/topic,117425.msg883455.html#msg883455

and bumped up the frequency to 6.5kHz --> no noise whatsoever, and the motor is spinning smoother on lower rates than with the analogWrite function.

Thanks for the info, saved me a lot of time!



11  Using Arduino / General Electronics / Problem with motor noise & Pololu motor driver on: March 19, 2013, 11:12:00 pm
Hi,

I've got Arduino attached to Pololu motor driver:

http://www.pololu.com/catalog/product/755

Controlling this 12v DC motor at varying voltages between 4 and 12 with 3.5 peak Amps:

http://www.sciplus.com/p/CAR-SEAT-MOTOR_49248

The motor/driver/Arduino combination is all working as expected, but the noise generated in the motor by the PWM frequency is piercing.  If I wasn't using the Pololu driver, I would simply put a capacitor in line with the motor, but I can't do that here.  When I tried, there is some kind of short that on small capacitors doesn't seem to have any effect but sparking, but larger ones cause noise in the Arduino.  Neither have any affect on the motor noise.  What I think the motor driver is doing is alternating the positive/negative output to create an average PWM flow in either of two directions.  Any ideas of how I can loose the noise?  I've been scouring the net for hours and can't seem to find a solution.

Thanks,

Joe
12  Using Arduino / General Electronics / Re: I have an unusual problem powering muscle wires on: May 09, 2011, 07:30:47 pm
I'll give it a try, thanks
13  Using Arduino / General Electronics / I have an unusual problem powering muscle wires on: May 08, 2011, 05:30:15 pm
I have this art piece that I'm working on that uses 8 short pieces of muscle wire. I have power going through relays that are controlled through the arduino. When have the power going to the muscle wires through a 3v battery pack, the piece works perfectly, but when I switch over to a 3v wall adapter it doesn't work and I get a zero volt reading coming from the wall adapter. The wall adapter  works on it's own , just not with the muscle wires, and I've tried changing the adapter for a new one, but the issue persists. Please help
14  Forum 2005-2010 (read only) / Syntax & Programs / Two loops/delay timers on: April 28, 2010, 09:47:05 pm
Hi,

I'm trying to figure a way out to have two separate timers going in arduino.  I have 9 outputs that are all on a sequence of on/off delays in the loop.  I want to have a 10th output that is independent of the on/off delay loop of the first 9 so that while the first 9 are repeating the loop, the 10th output is set to its own rhythm.  Is this possible or do I have to get a second Arduino?
Pages: [1]