Posts Tagged ‘Arduino’
Arduino to Matlab – Read in sensor data!
Matlab is by far the best software I have ever seen when it comes to plotting data and showing it in a visual format. I figured it would be awesome if I could get Arduino and Matlab to work together!
Here’s the code that I came up with that works reasonably fast. It doesn’t wait for the buffer to be filled to then store it to a variable. Here, it is asynchronous communication.
This is a basic screenshot of what you will get, minus the green stars.
Here is the code. Let me know if you use, it would be neat to see what everyone would come up with!
*Note: The code highlighting for Matlab was buggy, so the below is using C code highlighting. Once you paste it into Matlab, it will be fine.
-
% Basic Arduino and Matlab
-
% Communication with an external hardware device
-
% ———————————————-
-
%
-
% Erin Kennedy
-
% Jan. 18, 2010
-
%
-
-
-
clear all; clc; close all;
-
-
-
% Try-catch is to prevent Matlab from crashing when the program is finished
-
try
-
-
-
% Initialize serial port
-
s = serial(‘/dev/tty.usbserial-A4001lVG’);
-
%set(s, ‘ Terminator’, ‘LF’); % Default terminator is \n
-
set(s,‘BaudRate’, 9600);
-
set(s,‘DataBits’, 8);
-
set(s,‘StopBits’, 1);
-
fopen(s);
-
s.ReadAsyncMode = ‘continuous’;
-
-
-
% Various variables
-
numberOfDatas = 50;
-
data = zeros(1, numberOfDatas);
-
i = 1;
-
-
-
% Main graph figure
-
figure(1);
-
hold on;
-
title(‘Incomming Data from External Device’);
-
xlabel(‘Data Number’);
-
ylabel(‘Analog Voltage (0-1023)’);
-
-
-
% Start asynchronous reading
-
readasync(s);
-
-
-
while(i<=numberOfDatas)
-
-
-
% Get the data from the serial object
-
data(i) = fscanf(s, ‘%d’);
-
-
% Plot the data
-
figure(1);
-
plot(i, data(i), ‘m*’);
-
-
% Ensure there are always 10 tick marks on the graph
-
if(i>10)
-
xlim([i-10 i]);
-
set(gca,‘xtick’,[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
-
end
-
-
% Draw and flush
-
drawnow;
-
-
%Increment the counter
-
i=i+1;
-
-
-
end
-
-
-
% Give the external device some time…
-
pause(3);
-
-
return;
-
-
catch
-
-
% Some of these crash the program – it depends. The serial port is left
-
% open, which is not good.
-
stopasync(s);
-
fclose(s); % bad
-
%delete(s);
-
%clear s;
-
-
fprintf(1, ‘Sorry, you"re going to have to close out of Matlab to close the serial port\n‘);
-
-
return
-
-
end
The code for the Arduino is this:
-
//
-
// BubbleBoy -> Matlab
-
// ——————-
-
//
-
// Read LDR data, print them to Serial (where Matlab will receive them)
-
//
-
-
int LDRpin = 0;
-
-
void setup() {
-
-
Serial.begin(9600);
-
pinMode(LDRpin, INPUT);
-
-
}
-
-
void loop() {
-
-
int photocellReading = analogRead(LDRpin);
-
-
Serial.println(photocellReading, DEC);
-
delay(200);
-
-
}
Enjoy, happy matlabbing!
Friday Night Robotics – MusicBox!
This Friday I had the most awesome idea of a weekend project ever! A 21st century MusicBox, using an Arduino and have blinking LEDs!
I had a Sparkfun box lying around, which is an ideal size for an Arduino and a WaveShield.

I wanted to have LEDs outlining the box, so I went to work on it:


Just as a disclaimer- I designed it wrong, so the LEDs don’t work well. If you’re looking to follow my steps, DON’T DO IT!


Then, installing them into the box:

It looks nice!

Once all of the connections are made, it’s pretty tight in there:

The problem though, is that I designed it so that all the LEDs are in series. Since al LEDs aren’t created equally, some suck up more power and therefore can’t share it with the others. This is what happened:

It’s pretty sad!
But, I think that I can fix it because I soldered the resistors together, not the actual LEDs together.
I was too excited to start this project, so I didn’t bother to plan
I guess planning would have been better, but it would also have been too boring.
I also worked on a NXT LED blinky thingy. There are these HiTechnic Protoboards that you can get, and basically you attach them to one of the sensor inputs, and you can control power to certain ports and such.
In this case, there’s 6 output pins that you can control. Sounds like an opportunity to use LEDs to me!

This is what the setup looks like:

It goes like this: NXT -> HiTechnic ProtoBoard Sensor Adapter -> 6 LEDs
In order to output instructions to the HiTechnic ProtoBoard, you need ‘drivers’, or headers. They’re located here.
Here is the code for the LEDs, in RobotC:
-
#pragma config(Sensor, S1, HTPB, sensorI2CCustom9V)
-
//*!!Code automatically generated by ‘ROBOTC’ configuration wizard !!*//
-
-
/*
-
Crazy LEDs!
-
Erin K
-
Oct. 9th, 2009
-
*/
-
-
#include "drivers/common.h"
-
#include "drivers/HTPB-driver.h"
-
-
byte theLEDs[] = { 0×01, 0×02, 0×04, 0×08, 0×10, 0×20 };
-
-
task main() {
-
-
// Setup all the digital IO ports as outputs (0xFF)
-
if (!HTPBsetupIO(HTPB, 0xFF)) StopAllTasks();
-
wait1Msec(200);
-
-
while(true) {
-
-
// The delay time
-
int theTime = 50;
-
-
// LEDs going up
-
for(int i=0; i<6; i++) {
-
if (!HTPBwriteIO(HTPB, theLEDs[i])) nxtDisplayTextLine(5, "ERR WRITE");
-
wait1Msec(theTime);
-
}
-
-
// LEDs going down
-
for(int i=5; i>=0; i–) {
-
if (!HTPBwriteIO(HTPB, theLEDs[i])) nxtDisplayTextLine(5, "ERR WRITE");
-
wait1Msec(theTime);
-
}
-
-
alive();
-
-
}
-
-
}
This is what the code does:
To wrap up this Friday Night Robotics, I checked out the Adafruit Ask an Engineer chat. It was pretty cool! I learnt about how LEDs work, and how much it costs to create a Teenyduino! Everyone should check it out, Saturday at 10:00PM EST.
The only things that I didn’t get to do that I wanted to was play with MANOI and the iRobot Create. I’m kinda worried that MANOI’s batteries are drying out as they haven’t been exercised in a while
EEP!
Friday Night Robotics – Hula Hooping Motion?
Building on last week’s RGB LEDs, it was time to add some motions to MANOI that would suit the carnival-ish theme.

A significant amount of time was spent on brainstorming ideas on how to make MANOI walk. The key will be the first step, as it has to get the rhythm started. The following steps will maintain the rhythm. Another point to look at would be shoes for MANOI. For walking, MANOI uses these ‘flip flops’ :

For skating, MANOI has roller skates that are made out of lego. These shoes are attached by velcro. It would be interesting to see if I could use some ShapeLock to make new shoes. (Shoes, Omg shoes. Shoes. Let’s buy some shoes!) By using ShapeLock, I may be able to eliminate some of the tilting that the velcro introduces, and thus eliminating another reason why the robot may fall.
In any case, I want to build on MANOI’s hip and leg motions like I did with the arm motions. I started with a simple swaying function, but Boom from the Robotics themed floor suggested an awesome suggestion- tilting the body to make it go forward and backward. In the right sequence, this would make a hula hoop motion!
It was interesting the way we did it. Instead of just telling the hip motors to tilt forward, we also told the shin motors to tilt backward. This maintained the center of balance.

We added onto this with arm motions. When MANOI tilts backwards, the arms go up. We tried keeping the arms up for tilting forward, but it shifted the center of balance too much.

Here are two videos of the motion:
There are more photos on Flickr!
I’m going to blog more about the Robotics themed floor later, as well as the Autonomous Robotics Club!
I also adopted an iRobot Create recently, and I’m controlling it with an Arduino. It’s fun!
Oh yeah, and MANOI fell about 3 feet from a desk when its battery ran out. It survived just fine, and nothing was broken!
Friday Night Robotics – Wild Thing
This actually wasn’t on a Friday (Friday I was making an iPhone app), but rather Saturday.
Since the Sanguino requires too many female headers (which cost money, and I don’t have any), I decided to switch up to the Arduino MEGA. It fits in the box that is already there, so it is all good.
Originally I thought that the Wave Shield would work on the MEGA, but it turned out that it didn’t. What I ended up doing instead, though was using one of the MEGA’s extra Serial lines to send a command to another Arduino to start playing music (on the Wave Shield).
I used my iHome speaker thing that I bought at Walmart for $20. Pretty good value, and it’s purple.

It was pretty fun. However, I only used the motions that I created a while ago when MANOI’s knees were broken. A blog post is coming shortly about the previous FNR where I fixed the gears
I even attached a wild duck to MANOI’s head for the occasion:

Here is a video, it is not too good:
See, the problem in the video (when I had to shake it) was that the TX line to the SSC was loose. It does this from time to time. Especially after switching boards!
Update on MANOI’s code
I updated MANOI’s code a little bit. I think it will work better, it’s more organized for sure!
This program “lifts” MANOI’s broken leg.
-
#include <stdio.h>
-
-
int HOME0 = 1800;
-
int HOME1 = 1500;
-
int HOME2 = 1000;
-
int HOME3 = 1300;
-
int HOME4 = 1300;
-
int HOME5 = 1600;
-
int HOME6 = 1900;
-
int HOME16 = 1550;
-
int HOME17 = 1200;
-
int HOME18 = 900;
-
int HOME19 = 1600;
-
int HOME20 = 1600;
-
int HOME21 = 1500;
-
int HOME22 = 1200;
-
int HOME23 = 1000;
-
int HOME24 = 1580;
-
int HOME25 = 1600;
-
-
void setup() {
-
Serial.begin(9600);
-
-
int homeFrame[18] = {
-
HOME0,
-
HOME1,
-
HOME2,
-
HOME3,
-
HOME4,
-
HOME5,
-
HOME6,
-
HOME16,
-
HOME17,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21,
-
HOME22,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
-
setFrame(homeFrame, 100, 500);
-
-
Serial.println("Hello world!");
-
}
-
-
void loop() {
-
for(int i=0; i<500; i+=50) {
-
int liftLeftLeg[17] = {
-
HOME0,
-
HOME1,
-
HOME2,
-
HOME3,
-
HOME4,
-
HOME5,
-
HOME6,
-
HOME16,
-
HOME17+i,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21,
-
HOME22,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
setFrame(liftLeftLeg, 500, 5000);
-
}
-
}
-
-
void setFrame(int theFrame[], int moveTime, int delayTime) {
-
char str[250]; // I will count the actual number, some day
-
sprintf(str, "#0 P%d #1 P%d #2 P%d #3 P%d #4 P%d #5 P%d #6 P%d #16 P%d #17 P%d #18 P%d #19 P%d #20 P%d #21 P%d #22 P%d #23 P%d #24 P%d #25 P%d T%d", theFrame[0], theFrame[1], theFrame[2], theFrame[3], theFrame[4], theFrame[5], theFrame[6], theFrame[7], theFrame[8], theFrame[9], theFrame[10], theFrame[11], theFrame[12], theFrame[13], theFrame[14], theFrame[15], theFrame[16], moveTime);
-
Serial.println(str);
-
delay(moveTime + delayTime);
-
}
I am more happier with this
Pretty easy to program with one hand, and MANOI in the other.
Thanks to all who left a comment on the previous post!
