I played around a few hours and actually figured out how I can send variables from Matlab to Arduino, just as you said. I just have to use:
>>arduino=serial('COM4','BaudRate',9600)
>> fopen(arduino)
>> fprintf(arduino,'1')
>> fprintf(arduino,'2')
>> fclose(arduino)
in Matlab. To get the LED to light up, I would use
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13,LOW);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
char letter = Serial.read();
if (letter=='1')
{
digitalWrite(13,HIGH);
}
else if (letter=='2')
{ digitalWrite(13,LOW);
}
}
}
on the Arduino. But how do I combine this code with the stepper code from the previous post though in order to move the stepper when I send a variable from Matlab? I tried:
#include <AccelStepper.h>
AccelStepper stepper1(1, 9, 8);
void setup()
{
stepper1.setMaxSpeed(3200);
pinMode(13, OUTPUT);
digitalWrite(13,LOW);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
char letter = Serial.read();
if (letter=='1')
{
digitalWrite(13,HIGH);
stepper1.moveTo(6400);
stepper1.setSpeed(3200);
stepper1.runSpeedToPosition();
}
else if (letter=='2')
{ digitalWrite(13,LOW);
stepper1.moveTo(6400);
stepper1.setSpeed(500);
stepper1.runSpeedToPosition();
}
}
}
but nothing would turn. On the Arduino serial communication website it states that
All Arduino boards have at least one serial port (also known as a UART or USART): Serial. It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.
Could that be it?after all my BED driven stepper is using pins (1,8,9). However, I tried AccelStepper stepper1(2, 9, 8 ) and while this is working as long as I activate the code directly in the Arduino sketch, once again nothing moves if I try it via serial communication via Matlab .
Isa