So I have a school project that requires sending square waves to a piezo controller. We avoid using Arduino Toolbox in Matlab because we have other peripherals that being controlled.
Our solution is to use serial communication in Matlab to control the Arduino board. Here is our code
Arduino:
unsigned long halfPeriod = 5000UL; // 5mS = 1/2 100 Hz
// don't use a floating # for delay, delayMicroseconds
void funcGen(){
digitalWrite(11, LOW);
delay(30);
for(int i = 0; i< 8; i++){
PINB = PINB | 0b00101000; // x-x-13-12-11-10-9-8 on Uno,
// toggle output by writing 1 to input register
delayMicroseconds(halfPeriod);
}
}
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite (13, HIGH); // known starting level
pinMode(11, OUTPUT);
digitalWrite (11, LOW); // known starting level
}
void loop(){
if (Serial.read() > 0){
funcGen();
Serial.flush();
}
}
Matlab:
arduino=serial('COM3','BaudRate',9600);
fopen(arduino)
fprintf(arduino,'1');
fclose(arduino);