How to convert Arduino code to MATLAB code?
9 views (last 30 days)
Show older comments
#define LT1 digitalRead(10)
#define LT2 digitalRead(4)
#define LT3 digitalRead(2)
#define ENA 5
#define ENB 11
#define IN1 6
#define IN2 7
#define IN3 8
#define IN4 9
#define ABS 115
void forward(){
analogWrite(ENA, ABS);
analogWrite(ENB, ABS);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("go forward!");
}
void back()
{
analogWrite(ENA, ABS);
analogWrite(ENB, ABS);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("go back!");
}
void left()
{
analogWrite(ENA, ABS);
analogWrite(ENB, ABS);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
Serial.println("go left!");
}
void right()
{
analogWrite(ENA, ABS);
analogWrite(ENB, ABS);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
Serial.println("go right!");
}
void stop()
{
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
Serial.println("Stop!");
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(LT2){
forward();
}
else if(LT1)
{
left();
while(LT1);
}
else if(LT3)
{
right();
while(LT3);
}
}
0 Comments
Answers (1)
M
on 9 Nov 2017
If you want to use Arduino from matlab, you can use the MATLAB arduino support package : https://fr.mathworks.com/matlabcentral/fileexchange/47522-matlab-support-package-for-arduino-hardware
Arduino uses C/C++ language, to convert C to Matlab, you can have a look here : https://fr.mathworks.com/help/matlab/call-mex-files-1.html
1 Comment
Madhu Govindarajan
on 9 Nov 2017
If you use Arduino MATLAB support package as mentioned here, you can find the equivalent for the digitalWrites and analogWrites here by searching the documentation.
HTH, Madhu
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!