14 -

interface and application programming

Since I've tried Arduino serial to processing before, I wanted to establish my board to processing sketch. I thought that would be easy to bridge those two. However, there are so many factors can cause problems.

Goal

My goal in this week is that reading data of a potentiometer by using my INPUT Board, and send the data through serial communication protocol. Processing sketch should be able to read from and write data to my INPUT board.

Working progress

- Because I use Arduino IDE, I started from trying stable communication from arduino to processing. There is a simple Example sketch in Arduino IDE named "Serial Call and Response in ASCII" (you can find in Example > 4 Communications). It worked prefectly. Processing sketch can get data from Arduino UNO and print correctly.

- I swtiched to my INPUT board. Serial protocol occured error. That is because Arduino UNO can communicate to computer (Mac Air) through hardware tx rx pin, but my INPUT board (with ATtiny45 embedded) can not. Based on this, I use SoftwareSerial library instead of build in Serial.

Here is my code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(0,1);

The code was correctly uploaded to my INPUT board, but Serial Monitor was unable to show anything:

Without showing data correctly, I found there are some problems on my board design.

Debugging

What I did:

- My original INPUT board sheme from week 11scheme

board

- Arduino IDE serial monitor printed lots of gibberish might be synchronize problem between INPUT board and my computer, so I burned the bootloader with setting as: Tools > Clock > 8 MHz (internal).

- Modify INPUT board. The picture below is the new scheme. I connect INPUT board's pin0 to FTDI rx, and pin 1 to FTDI tx. These 2 pins will be used for porting data to FTDI. Acctually I can redefined Attiny45's rx and rx,

#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); // RX, TX

- pinout and my code:

 

#include <SoftwareSerial.h>

SoftwareSerial mySerial(0, 1); // RX, TX
int sensorPin = A3; // input pin for the potentiometer
int ledPin = 2; // LED pin
int sensorValue = 0; // variable to store the value coming from the sensor
char val; // Data received from the serial port

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

void loop(){

sensorValue = analogRead(sensorPin);
mySerial.println(sensorValue);

// If data is available to read,
if (mySerial.available()){
val = mySerial.read(); // read it and store it in val
}
if (val == '1') {
digitalWrite(ledPin, HIGH); // turn the LED on
}else{
digitalWrite(ledPin, LOW); // otherwise turn it off
}
delay(30);
}

Programming in processing:

// processing serial test

import processing.serial.*;
Serial myPort;
String val;
int valtoInt;

void setup(){
frameRate(30);
size(480, 360);
String portName = Serial.list()[4]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);

}

void draw(){

background(#ff9900);

// read serial value and change ellipse size
if(myPort.available()>0){
val = myPort.readStringUntil('\n');
}
if(val != null){
val = trim(val);
valtoInt = int(val);
//println(val);
}
ellipse(width/2, height/2, valtoInt, valtoInt);

// led control
if (keyPressed) {
if (key == 'b' || key == 'B') {
myPort.write('1');
}else if(key == 'n' || key == 'N'){
myPort.write('0');
}
}

}

source files:

sketch_150520a_02 / sketch_may25a_FA_week14

Video:

:)