Skip to content

Instantly share code, notes, and snippets.

View GiovanniBalestrieri's full-sized avatar
🍣
When there is a shell, there is a way

UserK GiovanniBalestrieri

🍣
When there is a shell, there is a way
View GitHub Profile
@GiovanniBalestrieri
GiovanniBalestrieri / .ino
Last active August 29, 2015 14:25
Getting angular rate value from L3G4200D gyroscope and Arduino
void getGyroValues()
{
timerReading = micros() - dt;
byte xMSB = readRegister(L3G4200D_Address, 0x29);
byte xLSB = readRegister(L3G4200D_Address, 0x28);
x = ((xMSB << 8) | xLSB);
byte yMSB = readRegister(L3G4200D_Address, 0x2B);
byte yLSB = readRegister(L3G4200D_Address, 0x2A);
@GiovanniBalestrieri
GiovanniBalestrieri / .ino
Created July 27, 2015 00:00
Read and write from and to registers #I2C #Arduino
void writeRegister(int deviceAddress, byte address, byte val)
{
Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}
int readRegister(int deviceAddress, byte address)
@GiovanniBalestrieri
GiovanniBalestrieri / .ino
Created July 27, 2015 00:33
Getting CSV angular rate data from L3G4200D #Arduino #Gyroscope
//#define DEBUG
#include <Wire.h>
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
// Declare L3G4200D address
@GiovanniBalestrieri
GiovanniBalestrieri / .ino
Created July 27, 2015 23:29
Serial Communication between Matlab and Arduino
int a=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
serialRoutine();
@GiovanniBalestrieri
GiovanniBalestrieri / .m
Created July 28, 2015 00:09
Snippet 1 - Matlab Arduino serial communication
% XBee expects the end of commands to be delineated by a carriage return.
xbee = serial(port,'baudrate',9600,'terminator','CR','tag','Quad');
set(xbee, 'TimeOut', 5); %I am willing to wait 1.5 secs for data to arive
% I wanted to make my buffer only big enough to store one message
set(xbee, 'InputBufferSize', 390 )
% Before you can write to your serial port, you need to open it:
fopen(xbee);
disp('Serial port opened');
@GiovanniBalestrieri
GiovanniBalestrieri / .m
Created July 28, 2015 00:11
Snippet 2 - Matlab Arduino Serial communication
fprintf(xbee,'T');
disp('Sent: T');
pause(1);
ack = fscanf( xbee, '%s');
if (strcmp(deblank(ack), 'Ok') == 1)
disp ('Received: OK');
end
fprintf(xbee,'M');
disp ('Sent: M');
@GiovanniBalestrieri
GiovanniBalestrieri / .m
Created July 28, 2015 00:13
Snippet 3 - Matlab Arduino Serial communication
fclose(xbee);
delete(xbee);
clear xbee
@GiovanniBalestrieri
GiovanniBalestrieri / .m
Last active April 5, 2016 11:26
Full Code - Matlab Arduino Serial communication
%% Arduino Matlab communication
clear all;
clc;
delete(instrfindall);
%% Check serial objects & Connect to Port
% Check the port you are using with the arduino, then run: sudo ln -s /dev/ttyNUMBER /dev/ttyS101
disp('Linux User? Have you run the simbolic link with ttyS101?');
disp('sudo ln -s /dev/tty_NUMBER_ /dev/ttyS101');
port = '/dev/ttyS101';
@GiovanniBalestrieri
GiovanniBalestrieri / .m
Created July 28, 2015 02:24
Rolling plot Matlab Arduino Gyroscope
%% Ask desired Sample Rate in Hz
rate=input('Enter the samplerate in Hz. Max 500Hz: ');
delay = 1/rate;
str = sprintf('SampleRate fixed to: %f.', delay);
disp(str);
@GiovanniBalestrieri
GiovanniBalestrieri / .m
Created July 28, 2015 02:27
Rolling Plot Matlab Arduino gyroscope
%% Initializinig rolling plot
buf_len = 100;
index = 1:buf_len;
% create variables for the Xaxis
gxdata = zeros(buf_len,1);
gydata = zeros(buf_len,1);
gzdata = zeros(buf_len,1);