Show Posts
Pages: [1] 2
1  Forum 2005-2010 (read only) / Syntax & Programs / Re: weird Serial.print() problem on: September 03, 2006, 05:57:20 pm
One more question, when you say I need extra hardware to burn the bootloader, does that mean I can't just directly use the "Burn Bootloader" command from Arduino 0004?  

Will this hardware also be necessary when the new libraries come out with Arduino 0005, or are these uploaded with the program?
2  Forum 2005-2010 (read only) / Syntax & Programs / Re: weird Serial.print() problem on: September 03, 2006, 05:55:20 pm
Hmm, I think that might be the problem because the uploader reports a program size a little over 6100 bytes.  I will try to optimize the code and see how it goes.  

Is program size related to the number of characters in the code?  So, if I follow Nick's advice and use functions and arrays, will it reduce the program size?

Thanks for the help, I'll let you guys know how it works out.
3  Forum 2005-2010 (read only) / Syntax & Programs / weird Serial.print() problem on: September 02, 2006, 12:18:12 pm
Hello all,

I plugged 12 switches in my Arduino controller and I need to know which are on and which are off.  I am sending this info using Serial.print()  Now, I don't want to hog the serial port and send data continuously because that will make the program receiving this data freak out.  So I created variables for each switch, and I check whether or not the value changes at every loop() iteration and if it does, I send the new value.  Here is the code:

Code:
int rPin = 0;
int aPin = 2;
int bPin = 3;
int cPin = 4;
int dPin = 5;

int hPin = 6;
int ePin = 7;
int sPin = 8;
int oPin = 9;
int gPin = 10;

int nAPin = 11;
int nBPin = 12;
int nCPin = 13;

// variables
int aLock = 0;
int bLock = 0;
int cLock = 0;
int dLock = 0;

int hToggle = 0;
int eToggle = 0;
int sToggle = 0;
int oToggle = 0;
int gToggle = 0;

int nSelect = 0;

int val = 0;


void setup() {
  pinMode(rPin, INPUT);
  
  pinMode(aPin, INPUT);
  pinMode(bPin, INPUT);
  pinMode(cPin, INPUT);
  pinMode(dPin, INPUT);
  
  pinMode(hPin, INPUT);
  pinMode(ePin, INPUT);
  pinMode(sPin, INPUT);
  pinMode(oPin, INPUT);
  pinMode(gPin, INPUT);
  
  pinMode(nAPin, INPUT);
  pinMode(nBPin, INPUT);
  pinMode(nCPin, INPUT);
  
  // open the serial port
  Serial.begin(19200);
}


void loop() {
  //if (digitalRead(rPin) == LOW) {
  //  Serial.print('R');
  //}
  
  // report changes for handle locks
  val = digitalRead(aPin);
  if (aLock != val) {
    aLock = val;
    Serial.print('A');
    Serial.print(aLock);  
  }
  
  val = digitalRead(bPin);
  if (bLock != val) {
    bLock = val;
    Serial.print('B');
    Serial.print(bLock);  
  }
  
  val = digitalRead(cPin);
  if (cLock != val) {
    cLock = val;
    Serial.print('C');
    Serial.print(cLock);  
  }
  
  val = digitalRead(dPin);
  if (dLock != val) {
    dLock = val;
    Serial.print('D');
    Serial.print(dLock);  
  }
  
  // report changes for option toggles
  /*val = digitalRead(hPin);
  if (hToggle != val) {
    hToggle = val;
    Serial.print('H');
    Serial.print(hToggle);
  }
  
  val = digitalRead(ePin);
  if (eToggle != val) {
    eToggle = val;
    Serial.print('E');
    Serial.print(eToggle);
  }
 
  val = digitalRead(sPin);
  if (sToggle != val) {
    sToggle = val;
    Serial.print('S');
    Serial.print(sToggle);
  }
  
  val = digitalRead(oPin);
  if (oToggle != val) {
    oToggle = val;
    Serial.print('O');
    Serial.print(oToggle);
  }*/
  
  val = digitalRead(gPin);
  if (gToggle != val) {
    gToggle = val;
    Serial.print('G');
    Serial.print(gToggle);
  }
  
  // report changes for handle selection
  if (digitalRead(nAPin) == LOW) {
    val = 1;
  } else if (digitalRead(nBPin) == LOW) {
    val = 2;
  } else if (digitalRead(nCPin) == LOW) {
    val = 3;
  } else {
    val = 4;
  }
  if (nSelect != val) {
    nSelect = val;
    Serial.print('N');
    Serial.print(nSelect);
  }
  
  delay(200);
}

I have a major problem.  As soon as I do the whole if (changed) { print() } thing more than 8 times, it stops working.  Nothing goes through the serial port.  That's why some lines are commented in the code above.  Now I know it's not a variable name or hardware problem, because if I comment any other 4 if-statements it still works.  As soon as I take out the comments or only comment out 3 statements, nothing gets printed to the serial port.  

On top of that, the following code, which prints out all the values without checking if they're changed or not, works perfectly:

Code:
int rPin = 0;
int aPin = 2;
int bPin = 3;
int cPin = 4;
int dPin = 5;

int hPin = 6;
int ePin = 7;
int sPin = 8;
int oPin = 9;
int gPin = 10;

int nAPin = 11;
int nBPin = 12;
int nCPin = 13;

int val = 0;

void setup() {
  pinMode(rPin, INPUT);
  
  pinMode(aPin, INPUT);
  pinMode(bPin, INPUT);
  pinMode(cPin, INPUT);
  pinMode(dPin, INPUT);
  
  pinMode(hPin, INPUT);
  pinMode(ePin, INPUT);
  pinMode(sPin, INPUT);
  pinMode(oPin, INPUT);
  pinMode(gPin, INPUT);
  
  pinMode(nAPin, INPUT);
  pinMode(nBPin, INPUT);
  pinMode(nCPin, INPUT);
  pinMode(nDPin, INPUT);
  
  // open the serial port
  Serial.begin(19200);
}


void loop() {
  Serial.print('R');
  Serial.print(digitalRead(rPin));
  
  Serial.print('A');
  Serial.print(digitalRead(aPin));
  Serial.print('B');
  Serial.print(digitalRead(bPin));
  Serial.print('C');
  Serial.print(digitalRead(cPin));
  Serial.print('D');
  Serial.print(digitalRead(dPin));
  
  Serial.print('H');
  Serial.print(digitalRead(hPin));
  Serial.print('E');
  Serial.print(digitalRead(ePin));
  Serial.print('S');
  Serial.print(digitalRead(sPin));
  Serial.print('O');
  Serial.print(digitalRead(oPin));
  Serial.print('G');
  Serial.print(digitalRead(gPin));
  
  Serial.print('N');
  if (digitalRead(nAPin) == LOW) {
    Serial.print(1);
  } else if (digitalRead(nBPin) == LOW) {
    Serial.print(2);
  } else if (digitalRead(nCPin) == LOW) {
    Serial.print(3);
  } else {
    Serial.print(4);
  }
  
  Serial.println();
  
  delay(100);
}

Has anyone encountered this problem?  I have no idea what to do!  Does it have something to do with delay()?  Is there some buffer that I am exceeding?

Thanks in advance for the help
4  Forum 2005-2010 (read only) / Syntax & Programs / Re: how to use a speaker? on: June 04, 2006, 10:35:23 am
great! that fixed it

i just wrapped the play sound code in a loop:

Code:
 ...
  for (int i=0; i < 75; i++) {
    digitalWrite(speakerOut,HIGH);
    delayMicroseconds(wallTone);
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(wallTone);
  }
  ...
5  Forum 2005-2010 (read only) / Syntax & Programs / how to use a speaker? on: June 03, 2006, 10:22:48 pm
Hi all,

I wrote a little program that plays a note on the speaker whenever it receives input, but I am having a problem.  The program does not play a note, but a "clicking" sound.  I based myself on the play_melody example to do this (which works perfectly), but since I don't really know what I'm doing smiley-wink I can't see why it doesn't work.

Both calls give a different "click", so the tone value does do something.

Code:
int speakerOut = 5;
int paddleTone = 1136;
int wallTone = 956;
int inData = 0;
  
 
void setup() {
  // set the pin modes
  pinMode(speakerOut, OUTPUT);
  
  // begin sending over serial port
  Serial.begin(19200);
}


void loop() {
  if (Serial.available() > 0) {
    // read the incoming data
    inData = Serial.read();
    
    if (inData == 60) {
      // paddle collision
      digitalWrite(speakerOut,HIGH);
      delayMicroseconds(paddleTone);
      digitalWrite(speakerOut, LOW);
      delayMicroseconds(paddleTone);
    } else if (inData == 62) {
      // wall collision
      digitalWrite(speakerOut,HIGH);
      delayMicroseconds(wallTone);
      digitalWrite(speakerOut, LOW);
      delayMicroseconds(wallTone);
    }
  }
}

Thanks for the help!
6  Forum 2005-2010 (read only) / Interfacing / faceCloth_Live: Processing+Arduino on: September 15, 2006, 03:54:58 pm
Hey all,

Here is the link to an installation I made last week: http://facecloth.prisonerjohn.com/live

It is a Processing application that is controlled by a custom-built gamepad which uses an Arduino board.  There are a bunch of pictures and sketches and a small video too.  I had a lot of fun building it and it was really easy to do (a lot easier than with a BASIC stamp!), so I thought I would share it with you.

Comments and suggestions are appreciated smiley

Elie
7  Forum 2005-2010 (read only) / Interfacing / Re: processing interfacing on: June 03, 2006, 10:33:26 pm
Sorry for replying so late, I totally forgot about this.  

I just tested my program again and it works.  I don't know what was wrong, but I'm suspecting it's something like reading from the wrong serial port.

Anyways, thanks for the help.

I'm having another problem with this code that you can check out here:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1149391368
8  Forum 2005-2010 (read only) / Interfacing / Re: processing interfacing on: May 26, 2006, 10:06:19 am
i know the collisions work because the ball bounces back smiley
but i will try again with the console when i get home later and see what i get
9  Forum 2005-2010 (read only) / Interfacing / Re: processing interfacing on: May 26, 2006, 07:50:42 am
No, that's not the problem, it's just another case of copying/pasting too quickly.

Do Arduino and Processing automatically know what is incoming and what is outgoing data?  Is it possible the signal the Processing code is sending is being read by Processing too or does it just read whatever is being "Serial.written" from other sources?
10  Forum 2005-2010 (read only) / Interfacing / Re: processing interfacing on: May 25, 2006, 07:25:35 pm
anyone?...
11  Forum 2005-2010 (read only) / Interfacing / Re: processing interfacing on: May 21, 2006, 08:21:54 pm
Arduino:
Code:
/****************************************************
*  ezPong_arduino                                                    
*                                                  
*  copyleft 2006 | Elie Zananiri                    
*  prisonerjohn@gmail.com                          
*                                                  
*  based on the following sketches                  
*   pwm_sound/play_melody                          
*   serial_comm/analog_read_send                    
*****************************************************/



// program variables
int potIn = 2;
int value = 0;
int speakerOut = 5;
int tone = 1915;
int inData = 0;
  
 
void setup() {
  // set the pin modes
  pinMode(potIn, INPUT);
  pinMode(speakerOut, OUTPUT);
  
  // begin sending over serial port
  Serial.begin(19200);
}


void loop() {
  // read the value using analog input
  value = analogRead(potIn);

  // send value over the serial port...
  printInteger(value);
  // ..and a separator signal
  printByte(10);

  delay(10);
  
  
  if (Serial.available() > 0) {
    // read the incoming date
    inData = Serial.read();
    
    // check if the incoming data is the 'play sound' code
    if (inData == 60) {
      Serial.print(inData, DEC);
      
       // play a sound through the speaker
      digitalWrite(speakerOut, HIGH);
      delayMicroseconds(tone);
      digitalWrite(speakerOut, LOW);
      delayMicroseconds(tone);      
    
      // pause
      digitalWrite(speakerOut, 0);
      delayMicroseconds(500);
    }
  }
}
12  Forum 2005-2010 (read only) / Interfacing / Re: processing interfacing on: May 21, 2006, 08:20:30 pm
Processing:
Code:
import processing.serial.*;


/****************************************************
*  ezPong_processing                                
*                                                  
*  copyleft 2006 | Elie Zananiri                    
*  prisonerjohn@gmail.com                          
*                                                  
*  based on the Arduino meets Processing tutorials  
*****************************************************/



// serial connection variables
Serial port;
String portName = "/dev/cu.usbserial-3B1";  
int baudRate = 19200;
int value = 0;
String buffer="";
int potValue = 0;  

// visuals variables
Ball ball;
int ballSize = 10;
Paddle paddle;
int paddleWidth = 80;
int paddleTop = 25;
int paddlePos;


void setup() {
  size(600, 400);
  framerate(30);
  smooth();
  
  // establish serial port connection      
  port = new Serial(this, portName, baudRate);
  
  // create ball
  ball = new Ball(random(ballSize, width-ballSize), random(ballSize, height-ballSize), ballSize, color(200, 0, 0));
  // create paddle
  paddle = new Paddle(random(paddleWidth, width-paddleWidth), height-paddleTop, paddleWidth, ballSize, color(200, 200, 200));
}


void serialEvent(int serial){
  if (serial == 10) {
    // if serial is line break store and clear the buffer      
    potValue = 1023-int(buffer);
    buffer = "";
  } else if (serial == 60) {
    // if serial event is beep signal clear the buffer
    buffer = "";
  } else {
    // add an event to the buffer
    buffer += char(serial);
  }
  
  // convert potValue to the correct range for the paddle position
  paddlePos = (potValue*(width-ballSize-paddleWidth))/1023+ballSize/2;
  // move the paddle
  paddle.move(paddlePos);
}


void draw() {
  // listen to serial port and trigger serial event  
  while (port.available() > 0) {
    value = port.read();
    serialEvent(value);
  }
 
  // draw graphics
  // erase bg using a semi-transparent square to have a motion blur effect
  drawBG();
  // move and draw the ball
  ball.step();
  // draw the paddle
  paddle.draw();
}


void drawBG() {
  // draw a semi-transparent rect over the entire canvas to create a motion blur effect
  noStroke();
  fill(200, 200, 100, 150);
  rect(0, 0, width, height);
}



/**
 * class Ball
 */
class Ball {
  // position variables
  float x;
  float y;
  
  // speed variables
  float dX;
  float dY;
  int frame;
  
  // graphics variables
  int size;
  color col;
  
  
  // constructor
  public Ball(float startX, float startY, int theSize, color theColor) {
    x = startX;
    y = startY;
    
    size = theSize;
    col = theColor;
    
    // randomly set a starting speed
    dX = ceil(random(0, 5));
    dY = ceil(random(0, 5));
    frame = 0;
  }
 
 
  public void move() {
    x += dX;
    y += dY;  
  }
  
  
  public void accelerate() {
    dX *= 1.01;
    dY *= 1.01;
  }
  
  
  public void draw() {
    stroke(0);
    fill(col);
    ellipse(x, y, size, size);  
  }
  
  
  public void checkCollisions() {
    // check for right wall collision
    if ((x+size/2) > width) {
      x = width-size-1;
      dX *= -1;
    // check for left wall collision
    } else if ((x-size/2) < 0) {
      x = size+1;
      dX *= -1;
    }
    
    // check for paddle collision
    if ((y+(size/2) > paddle.y) && (x > paddle.x) && (x < (paddle.x+paddle.w))) {
      y = paddle.y-size-1;
      dY *= -1;
      // send signal to serial
      //port.write(60);
    // check for bottom wall collision
    } else if (y+(size/2) > height) {
      y = height-size-1;
      dY *= -1;
    // check for top wall collision
    } else if (y-(size/2) < 0) {
      y = size+1;
      dY *= -1;
    }
  }
  
  
  public void step() {
    move();
    // accelerate ball speed every 30 frames i.e. every second
    if (++frame%30 == 0) {
      accelerate();
    }
    checkCollisions();
    
    draw();
  }
}



/**
 * class Paddle
 */
class Paddle {
  // position variables
  float x;
  float y;
  
  // graphics variables
  int w;
  int h;
  color col;
  
  
  // constructor
  public Paddle(float startX, float startY, int theWidth, int theHeight, color theColor) {
    x = startX;
    y = startY;
    
    w = theWidth;
    h = theHeight;
    col = theColor;
  }
  
  
  public void move(float newX) {
    // assign the received x position
    x = newX;  
  }
  
  
  public void draw() {
    // set the fill color
    noStroke();
    fill(col);
    // draw the left rounded edge
    ellipse(x, y, h, h);  
    // draw the center
    rect(x, y-h/2, w, h);
    // draw the right rounded edge
    ellipse(x+w, y, h, h);
    
    // set the stroke color
    stroke(0);
    // add the outline around the paddle shape
    line(x, y-h/2, x+w, y-h/2);
    line(x, y+h/2, x+w, y+h/2);
    arc(x, y, h, h, PI/2, -PI/2);
    arc(x+w, y, h, h, -PI/2, PI/2);
  }
}  
13  Forum 2005-2010 (read only) / Interfacing / processing interfacing on: May 21, 2006, 08:19:54 pm
Hi again smiley

I am now trying to set up a communication between Processing and Arduino.  I created a simple pong game to do this where the paddle is controlled by a potentiometer connected to the board.  

This part works really well, but I want to add communication in the other direction too:  I want Processing to send a signal when the paddle is hit, and when the Arduino board receives this signal, it plays a sound on a connected speaker.

How can I tell the Arduino what is incoming and what is outgoing information?

I am attaching the code I've got so far (in the following posts since I'm running out of space), but the speaker element doesn't work.

Thanks for the help!
14  Forum 2005-2010 (read only) / Interfacing / Re: flash interfacing on os x on: May 19, 2006, 08:12:27 am
>>are you compiling it with arduino4?
 
yes i am
15  Forum 2005-2010 (read only) / Interfacing / Re: flash interfacing on os x on: May 18, 2006, 07:29:36 pm
hey
the baud rate was the problem, i changed it to 19200 and it works great
thanks for the help!

also, I forgot to mention this, but I get the following error when I compile the Arduino source:
 In function 'void ArduinoConversation(int)':
error: invalid conversion from 'const char*' to 'unsigned char*

i just took out "unsigned" from the sendStringToFlash() function and it compiles properly.
beltran, you may want to look into that...
Pages: [1] 2