Show Posts
Pages: 1 [2] 3 4 ... 13
16  Using Arduino / Networking, Protocols, and Devices / MAG3110 magnetometer I2C problem on: January 31, 2013, 01:13:14 pm
I'm having trouble setting up this magnetometer:

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

I checked the wiring thoroughly and even made sure the ID matches the one in the datasheet. Yet I can't see anything happening on the serial monitor. Interestingly, when I remove the clock wire, occasionally the Arduino starts spitting out data (the three axes). Is it possible that the clock signal is skewed? This is the sketch provided by Sparkfun:

Code:
/*
  MAG3110 Breakout Example Code
 
  by: Aaron Weiss, aaron at sparkfun dot com
      SparkFun Electronics 2011
  date: 9/6/11
  license: beerware, if you use this code and happen to meet me, you
           can by me a beer

  The code reads the raw 16-bit x, y, and z values and prints them
  out. This sketch does not use the INT1 pin, nor does it poll for
  new data.

*/

#include <Wire.h>

#define MAG_ADDR  0x0E //7-bit address for the MAG3110, doesn't change

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

void loop()
{
  print_values();
  delay(5);
}

void config(void)
{
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x11);              // cntrl register2
  Wire.send(0x80);              // send 0x80, enable auto resets
  Wire.endTransmission();       // stop transmitting
 
  delay(15);
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x10);              // cntrl register1
  Wire.send(1);                 // send 0x01, active mode
  Wire.endTransmission();       // stop transmitting
}

void print_values(void)
{
  Serial.print("x=");
  Serial.print(readx());
  Serial.print(","); 
  Serial.print("y=");   
  Serial.print(ready());
  Serial.print(",");       
  Serial.print("z=");   
  Serial.println(readz());     
}

int readx(void)
{
  int xl, xh;  //define the MSB and LSB
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x01);              // x MSB reg
  Wire.endTransmission();       // stop transmitting
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
  while(Wire.available())    // slave may send less than requested
  {
    xh = Wire.receive(); // receive the byte
  }
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x02);              // x LSB reg
  Wire.endTransmission();       // stop transmitting
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
  while(Wire.available())    // slave may send less than requested
  {
    xl = Wire.receive(); // receive the byte
  }
 
  int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
  return xout;
}

int ready(void)
{
  int yl, yh;  //define the MSB and LSB
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x03);              // y MSB reg
  Wire.endTransmission();       // stop transmitting
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
  while(Wire.available())    // slave may send less than requested
  {
    yh = Wire.receive(); // receive the byte
  }
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x04);              // y LSB reg
  Wire.endTransmission();       // stop transmitting
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
  while(Wire.available())    // slave may send less than requested
  {
    yl = Wire.receive(); // receive the byte
  }
 
  int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
  return yout;
}

int readz(void)
{
  int zl, zh;  //define the MSB and LSB
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x05);              // z MSB reg
  Wire.endTransmission();       // stop transmitting
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
  while(Wire.available())    // slave may send less than requested
  {
    zh = Wire.receive(); // receive the byte
  }
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x06);              // z LSB reg
  Wire.endTransmission();       // stop transmitting
 
  delayMicroseconds(2); //needs at least 1.3us free time between start and stop
 
  Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
  while(Wire.available())    // slave may send less than requested
  {
    zl = Wire.receive(); // receive the byte
  }
 
  int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
  return zout;
}
17  Using Arduino / Networking, Protocols, and Devices / Re: WIFI shield TCP connection problem on: January 31, 2013, 01:02:06 pm
Thanks very much, I managed to setup a server on the Arduino. I can broadcast messages however when the Max patch sends integers (formatted like this: send "23" ), they appear as random set of characters on the server side. How are TCP messages formatted?
18  Using Arduino / Networking, Protocols, and Devices / Re: WIFI shield TCP connection problem on: January 31, 2013, 08:55:01 am
THanks! I've tried the local IP address (192.168.0.2) and different ports yet I still cannot see the two connecting.  The Max patch is a client too, could that be the problem?
19  Using Arduino / Networking, Protocols, and Devices / WIFI shield TCP connection problem on: January 31, 2013, 08:16:04 am
I'm trying to set up my WIFI shield to send and receive messages over TCP sockets. There's a Max MSP patch as well with a TCP client object that seems to work well. Yet I cannot get the two, the WIFI shield and Max to talk to each other over TCP. What confuses me is that which IP address should I provide in the Arduino code? The router's, the shield's or the localhost IP?

Code:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <SPI.h>


char ssid[] = "nwtwork";
//password of your WPA Network
char pass[] = "password";

WiFiClient client;
IPAddress server(); //shield IP address
void setup()
{
  Serial.begin(9600);
  Serial.println("** Scan Networks **");
  byte numSsid = WiFi.scanNetworks();
  Serial.print("SSID List:");
  Serial.println(numSsid);
  WiFi.begin(ssid, pass);
  //delay(5000);
  Serial.println("Connected to wifi");
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  Serial.print("Shield IP Address: ");
  Serial.println(ip);
  client.connect(server,7120);
  if(client.connected()){
    Serial.println("CLIENT AVAILABLE");
  }
  else {
    Serial.println("CLIENT NOT AVAILABLE");
  }
}

void loop () {
  client.write("test");
  delay(1000);
  int in = client.read();
  Serial.println(in);
}

Also, do the port number on both sides have to match?


20  Using Arduino / Networking, Protocols, and Devices / Re: I2C - how does that work? on: January 23, 2013, 05:34:21 am
Thank you very much! Now it all makes sense. So much to learn...
21  Using Arduino / Networking, Protocols, and Devices / I2C - how does that work? on: January 23, 2013, 02:27:24 am
I can't wrap my head around how I2C works with Arduino. I was checking examples and tutorials where two Arduinos are connected using I2C and analog 4 and 5 are connected, as well as ground, like here:

http://arduino.cc/en/Tutorial/MasterWriter

Reading multiple tutorials I don't doubt that it works yet it seems to defy logic to connect two analog inputs. How on Earth can anyone send bytes over two connected inputs??? How does the master send anything over an analog input? Thank you for your answers in advance!
22  Using Arduino / Programming Questions / Re: Control flow troubles on: December 10, 2012, 04:23:35 pm
Thank you gents!
23  Using Arduino / Programming Questions / Re: Control flow troubles on: December 10, 2012, 04:09:03 pm
I do appreciate your effort and promise next time I paste my code I'll autoformat it beforehand.
24  Using Arduino / Programming Questions / Re: Control flow troubles on: December 10, 2012, 04:02:28 pm
I appreciate your help and am grateful but I feel a but uncomfortable with your schooling.
25  Using Arduino / Programming Questions / Re: Control flow troubles on: December 10, 2012, 03:49:35 pm
Quote
t's more what you failed to do. That is, you failed to put each { on a new line. You failed to use Tools + Auto Format to fix the indenting.

Code that bounces
            all over the place is
    very hard to read. The
        Tools + Auto Format
menu item
                           fixes that problem.

I do use Auto Format it's just I can't stand the bracket taking up an extra line. Never thought it can be a problem.
26  Using Arduino / Programming Questions / Re: Control flow troubles on: December 10, 2012, 03:47:07 pm
Oh, cr*p, I copied it from an other sketch. Makes sense. Thanks you!
27  Using Arduino / Programming Questions / Re: Control flow troubles on: December 10, 2012, 03:27:42 pm
Quote
Printing numbers to the serial monitor is an exercise in frustration unless you label them.

Code:
  Serial.println("Enter address in decimal starting with the letter a and finishing it with a period!");
    if(aChar == 'r') //beginning of the string
a != r!

Not sure what you meant by that. Can you explain that please?
28  Using Arduino / Programming Questions / Re: Control flow troubles on: December 10, 2012, 03:25:27 pm
I tried what you suggested and now it won't work. What did I do wrong?

Code:
char addressBit[15];
char dataBit[10];
int arrayIndex, i;
boolean startedA, endedA = false;
boolean once = true;

int CE  = 47;
int OE  = 48;
int WE  = 49;

int AP[13] = {
  22,24,26,28,30,32,34,36,38,40,42,44,46};
int DP[8] =  {
  23,25,27,29,31,33,35,37};

void setup(){
  Serial.begin(9600);
  Serial.println("Enter address in decimal starting with the letter a and finishing it with a period!");
  Serial.println("Then enter data in decimal between d and comma!");
  for (i=2;i<10;i++) {
    pinMode(AP[i],OUTPUT); // address pins
  }
  for (i=9;i<14;i++) {
    pinMode(DP[i],INPUT); // data pins
  }
  // Setup Control Pins
  pinMode(CE, OUTPUT);
  pinMode(WE, OUTPUT);
  pinMode(OE, OUTPUT);
  pinMode(45, OUTPUT);

  // Setup Chip
  digitalWrite(CE, HIGH);
  digitalWrite(WE, LOW);
  digitalWrite(OE, HIGH);
}

void loop(){
  digitalWrite(45, HIGH);
  while(Serial.available() > 0)
  {
    char aChar = Serial.read();
    if(aChar == 'r') //beginning of the string
    {
      startedA = true;
      arrayIndex = 0;
      addressBit[arrayIndex] = '\0';
    }
    else if(aChar == '/') //end of string
    {
      endedA = true;
    }
    else if(startedA)
    {
      addressBit[arrayIndex] = aChar;
      arrayIndex++;
      addressBit[arrayIndex] = '\0';
    }
  } 
  if(startedA && endedA){ // string filled up
    int incomingByteA = atoi(addressBit); // Convert the string to an integer
    for(int j=0; j<13; j++){
      digitalWrite (AP[j], bitRead (incomingByteA,j));  // extract relevant bit and output on relevant pin.
      int a = bitRead (incomingByteA,j);
      //Serial.println(a);
    }
   
    // Get ready for the next time
    startedA = false;
    endedA = false;
    arrayIndex = 0;
    addressBit[arrayIndex] = '\0';
  }
 
   digitalWrite(WE,LOW);      // Write Disabled
    delay(1);
    digitalWrite(CE,HIGH);      // Chip enabled
    delay(1);
    digitalWrite(OE,HIGH);       // Reads Enabled
    delay(1);
if(once){
  for(int i=0; i<8; i++){
    int data = digitalRead(DP[i]);
    Serial.print(data);
  }
  once = false;
  }
  Serial.println(" ");
  delay(300);
}


29  Using Arduino / Programming Questions / Re: Control flow troubles on: December 10, 2012, 02:58:34 pm
I cleaned up the code a bit and still get strange results on the serial monitor.

Code:
char addressBit[15];
char dataBit[10];
int arrayIndex, i;
boolean startedA, endedA, flow = false;

int CE  = 47;
int OE  = 48;
int WE  = 49;

int AP[13] = {
  22,24,26,28,30,32,34,36,38,40,42,44,46};
int DP[8] =  {
  23,25,27,29,31,33,35,37};

void setup(){
  Serial.begin(9600);
  Serial.println("Enter address in decimal starting with the letter a and finishing it with a period!");
  Serial.println("Then enter data in decimal between d and comma!");
  for (i=2;i<10;i++) {
    pinMode(AP[i],OUTPUT); // address pins
  }
  for (i=9;i<14;i++) {
    pinMode(DP[i],INPUT); // data pins
  }
  // Setup Control Pins
  pinMode(CE, OUTPUT);
  pinMode(WE, OUTPUT);
  pinMode(OE, OUTPUT);
  pinMode(45, OUTPUT);

  // Setup Chip
  digitalWrite(CE, HIGH);
  digitalWrite(WE, LOW);
  digitalWrite(OE, HIGH);
}

void loop(){
  digitalWrite(45, HIGH);
  while(Serial.available() > 0)
  {
    char aChar = Serial.read();
    if(aChar == 'r') //beginning of the string
    {
      startedA = true;
      arrayIndex = 0;
      addressBit[arrayIndex] = '\0';
    }
    else if(aChar == '/') //end of string
    {
      endedA = true;
    }
    else if(startedA)
    {
      addressBit[arrayIndex] = aChar;
      arrayIndex++;
      addressBit[arrayIndex] = '\0';
    }
  } 
  if(startedA && endedA){ // string filled up
    int incomingByteA = atoi(addressBit); // Convert the string to an integer
    for(int j=0; j<13; j++){
      digitalWrite (AP[j], bitRead (incomingByteA,j));  // extract relevant bit and output on relevant pin.
      int a = bitRead (incomingByteA,j);
      //Serial.println(a);
    }
   
    // Get ready for the next time
    startedA = false;
    endedA = false;
    arrayIndex = 0;
    addressBit[arrayIndex] = '\0';
  }
 
   digitalWrite(WE,LOW);      // Write Disabled
    delay(1);
    digitalWrite(CE,HIGH);      // Chip enabled
    delay(1);
    digitalWrite(OE,HIGH);       // Reads Enabled
    delay(1);

  for(int i=0; i<8; i++){
    int data = digitalRead(DP[i]);
    Serial.print(data);
  }
  Serial.println(" ");
  delay(300);
}

Anyone could verify if it works? Just looking at the serial monitor doesn't help. Still struggle with that last for loop, I wish I knew how to execute it one!
30  Using Arduino / Programming Questions / Re: Control flow troubles on: December 10, 2012, 01:16:15 pm
Thank you, that's a very good point! I'm having trouble with the control flow again. This time a for loop gets executed continuously (as it's supposed to) but cannot make it run only once. Tried the break statement with no success.

Code:
char addressBit[10];
char dataBit[6];
int arrayIndex, dataIndex,i;
boolean startedA, endedA, startedD, endedD = false;

int CE  = 47;
int OE  = 48;
int WE  = 49;

int AP[13] = {22,24,26,28,30,32,34,36,38,40,42,44,46};
int DP[8] =  {23,25,27,29,31,33,35,37};

void setup(){
  Serial.begin(9600);
  Serial.println("Enter address in decimal starting with the letter a and finishing it with a period!");
  Serial.println("Then enter data in decimal between d and comma!");
  for (i=2;i<10;i++) {
    pinMode(AP[i],OUTPUT); // address pins
  }
  for (i=9;i<14;i++) {
    pinMode(DP[i],INPUT); // data pins
  }
  // Setup Control Pins
  pinMode(CE, OUTPUT);
  pinMode(WE, OUTPUT);
  pinMode(OE, OUTPUT);
  pinMode(45, OUTPUT);
  
  // Setup Chip
  digitalWrite(CE, HIGH);
  digitalWrite(WE, LOW);
  digitalWrite(OE, HIGH);
}

void loop(){
  digitalWrite(45, HIGH);
  while(Serial.available() > 0)
  {
    char aChar = Serial.read();
    if(aChar == 'r') //beginning of the string
    {
      startedA = true;
      arrayIndex = 0;
      addressBit[arrayIndex] = '\0';
    }
    else if(aChar == '/') ///end of string
    {
      endedA = true;
    }
    else if(startedA)
    {
      addressBit[arrayIndex] = aChar;
      arrayIndex++;
      addressBit[arrayIndex] = '\0';
    }
  }  
if(startedA && endedA){
    int incomingByteA = atoi(addressBit); // Convert the string to an integer
    for(int j=0; j<13; j++){
      digitalWrite (AP[j], bitRead (incomingByteA,j));  // extract relevant bit and output on relevant pin.
      int a = bitRead (incomingByteA,j);
      Serial.println(a);
    }
    Serial.println(" ");
    // Get ready for the next time
    startedA = false;
    endedA = false;
    arrayIndex = 0;
    addressBit[arrayIndex] = '\0';
  }
   delay(1);
    digitalWrite(WE,LOW);      // Write Disabled
    delay(1);
    digitalWrite(CE,HIGH);      // Chip enabled
    delay(1);
    digitalWrite(OE,HIGH);       // Reads Enabled
    delay(1);
  
for(int i=0; i<8; i++){       /// FOR LOOP
  int DD = digitalRead(DP[i]);
  Serial.println(DD);
  }
 
  delay(100);
}

It's the last bit that gives me headache:

Code:

for(int i=0; i<8; i++){       /// FOR LOOP
  int DD = digitalRead(DP[i]);
  Serial.println(DD);
  }

It seems that this bit prints whatever is coming from the serial as opposed to print the state of the pins. Where did I go wrong? Any suggestions would be appreciated!
Pages: 1 [2] 3 4 ... 13