Pages: [1]   Go Down
Author Topic: Problem with Tutorial.  (Read 761 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino IDE 1.0.3
Processing 2.0b7
Windows 7 (Non-Admin Account, programming work space account).
Wiring has been checked and then rechecked then rechecked.
Arduino UNO board.
Sensors are two photo-resistors from a radioshack assortment  bag.
Switch is momentary push button recycled from TV board.

Tutorial:
Serial Call and Response (handshaking)
http://www.arduino.cc/en/Tutorial/SerialCallResponse

Problem:
Under Serial Monitor a long string of A's that grows.(Which seems correct.)
Function of the switch seems unknown when pressed.
Processing produces just a gray block, No shape.

The code on Arduino;

Code:
/*
  Serial Call and Response
 Language: Wiring/Arduino
 
 This program sends an ASCII A (byte of value 65) on startup
 and repeats that until it gets some data in.
 Then it waits for a byte in the serial port, and
 sends three sensor values whenever it gets a byte in.
 
 Thanks to Greg Shakar and Scott Fitzgerald for the improvements
 
   The circuit:
 * potentiometers attached to analog inputs 0 and 1
 * pushbutton attached to digital I/O 2
 
 Created 26 Sept. 2005
 by Tom Igoe
 modified 24 April 2012
 by Tom Igoe and Scott Fitzgerald

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/SerialCallResponse

 */

int firstSensor = 0;    // first analog sensor
int secondSensor = 0;   // second analog sensor
int thirdSensor = 0;    // digital sensor
int inByte = 0;         // incoming serial byte

void setup()
{
  // start serial port at 9600 bps:
  Serial.begin(9600);
 

  pinMode(2, INPUT);   // digital sensor is on digital pin 2
  establishContact();  // send a byte to establish contact until receiver responds
}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    // read first analog input, divide by 4 to make the range 0-255:
    firstSensor = analogRead(A0)/4;
    // delay 10ms to let the ADC recover:
    delay(10);
    // read second analog input, divide by 4 to make the range 0-255:
    secondSensor = analogRead(1)/4;
    // read  switch, map it to 0 or 255L
    thirdSensor = map(digitalRead(2), 0, 1, 0, 255); 
    // send sensor values:
    Serial.write(firstSensor);
    Serial.write(secondSensor);
    Serial.write(thirdSensor);               
  }
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.print('A');   // send a capital A
    delay(300);
  }
}


Code in Processing;
Code:
/*
Processing sketch to run with this example:

// This example code is in the public domain.

import processing.serial.*;

int bgcolor;                 // Background color
int fgcolor;                 // Fill color
Serial myPort;                       // The serial port
int[] serialInArray = new int[3];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
int xpos, ypos;                  // Starting position of the ball
boolean firstContact = false;        // Whether we've heard from the microcontroller

void setup() {
  size(256, 256);  // Stage size
  noStroke();      // No border on the next thing drawn

  // Set the starting position of the ball (middle of the stage)
  xpos = width/2;
  ypos = height/2;

  // Print a list of the serial ports, for debugging purposes:
  println(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  background(bgcolor);
  fill(fgcolor);
  // Draw the shape
  ellipse(xpos, ypos, 20, 20);
}

void serialEvent(Serial myPort) {
  // read a byte from the serial port:
  int inByte = myPort.read();
  // if this is the first byte received, and it's an A,
  // clear the serial buffer and note that you've
  // had first contact from the microcontroller.
  // Otherwise, add the incoming byte to the array:
  if (firstContact == false) {
    if (inByte == 'A') {
      myPort.clear();          // clear the serial port buffer
      firstContact = true;     // you've had first contact from the microcontroller
      myPort.write('A');       // ask for more
    }
  }
  else {
    // Add the latest byte from the serial port to array:
    serialInArray[serialCount] = inByte;
    serialCount++;

    // If we have 3 bytes:
    if (serialCount > 2 ) {
      xpos = serialInArray[0];
      ypos = serialInArray[1];
      fgcolor = serialInArray[2];

      // print the values (for debugging purposes only):
      println(xpos + "\t" + ypos + "\t" + fgcolor);

      // Send a capital A to request new sensor readings:
      myPort.write('A');
      // Reset serialCount:
      serialCount = 0;
    }
  }
}
*/

*All  codes shown are of the Author from the tutorial link.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6935
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Problem:
Under Serial Monitor a long string of A's that grows.(Which seems correct.)
Function of the switch seems unknown when pressed.
Processing produces just a gray block, No shape.

Do you actually know what to expect?

I read your arduino code so I think it should send some values to processing after establishing contact but do yo know how to establish contact?
Logged


Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First thing I'd do is to install version 1.5.1 of Processing, and try that. Processing 2.0+ is still in Beta, as far as I can tell, and does not play well with Arduino.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First thing I'd do is to install version 1.5.1 of Processing, and try that. Processing 2.0+ is still in Beta, as far as I can tell, and does not play well with Arduino.

That seemed to have been a step in the right direction, I now have a black square displayed however no shape inside it.

I only got the square after I deleted this line of code:

Code:
  myPort = new Serial(this, portName, 9600);

I get an error message of this:
Code:
Error 0x5 at ..\src\termios.c(892): Access is denied.


Display 0 does not exist, using the default display instead.
WARNING:  RXTX Version mismatch
Jar version = RXTX-2.2pre1
native lib Version = RXTX-2.2pre2
[0] "COM3"
gnu.io.PortInUseException: Unknown Application
at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:467)
at processing.serial.Serial.<init>(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at sketch_130112a.setup(sketch_130112a.java:53)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.<init>()
at processing.serial.Serial.errorMessage(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at sketch_130112a.setup(sketch_130112a.java:53)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I only got the square after I deleted this line of code:
I only got the black square after I decided not to listen to the Arduino.

For something other than black, the color comes from the Arduino. No Arduino == no color.

Quote
gnu.io.PortInUseException: Unknown Application
What else is using the port? You don't have the Serial Monitor open, too, do you?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I figured out that I didn't need to delete the code when I decided to disconnect the Arduino from my 4-port USB hub and hook it directly into the computer.
 
Also I was using a different version tutorial it seems
http://arduino.cc/en/Tutorial/SerialCallResponseASCII

My only problem now is now that the sensors are always reading Zero
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Sensors are two photo-resistors from a radioshack assortment  bag.
How are they wired? Most photo-resistors need to be part of a voltage divider network:
+ -- -/\/\/\/\-+-/\/\/\/\ -- Gnd

One resistor is the photo-resistor. The other is a standard resistor with a value roughly equal to the phots-resistor in total darkness. The + connects to the analog pin.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just got it to work.

From what I've learned you can't have serial monitor running at the same time as you try to run it.

The problem was, that even though the main board appeared wired to the breadboard, the exposed wire tip was not making contact internally in the header.
It was surprising because these are the wires that made for this purpose ,10 cm length and shrink tubing on the ends.

I discovered the problem when I did a quick short of the breadboard rails with an LED to see if any current existed.
Logged

Pages: [1]   Go Up
Jump to: