Problem in Interfacing Arduino Mega 2560 with Matlab GUI.

hello! i am interfacing Arduino Mega 2560 with matlab, i have to control DC Motor speed using Matlab GUI by PWM technique, but its not working when i use analogRead/Write command. this shows error of "Undefined analogRead/Write..." and it is defined already... (while using digitalRead/Write command, it works. only problem in analog) Here is the Code.

clear a; global a; a= arduino('COM1') a.pinMode(7,'output'); a.pinMode(6,'output'); a.pinMode(5,'output');

double av;

av=a.analogRead(7); av=(av/1023)*255; a.analogWrite(7,av);

a.digitalWrite(6,1); a.digitalWrite(5,0);

Can anyone help me please. Thanks in adv

a.analogWrite(7,av);Is it correct to use a double here?

i tried without double as well. its not working in GUI m-file

hi i have the same problem with analoWrite i use a slider and i want to control a led light(like a dimmer) and i write a.analogWrite(13,analog) that analog is the value of slider and i have the same error as yours

how you want to increase or decrease the pwm?

i solve it with push button.slider doesn't work because it's output isn't compatible with the analogWrite(pin,value)

in your code define av as a global too i did that and it works

like this: clear a; global a; global av; a= arduino('COM1') a.pinMode(7,'output'); a.pinMode(6,'output'); a.pinMode(5,'output');

write these in the callback part : global a; global av; av=a.analogRead(7); av=(av/1023)*255; a.analogWrite(7,av); a.digitalWrite(6,1); a.digitalWrite(5,0);

What program is running on your Arduino?

…R

first I compile the adioes on the arduino and next i write the above code in matlab

ashamed: first I compile the adioes on the arduino and next i write the above code in matlab

What is "the adioes"? Do you have a link to the where you got the code?

...R

ashamed , ok thanks i will try it , i will vary speed like analogWrite(pin, 128) #for half speed. using push buttons, and how did u use the slider i also have confusion regarding this can u show me the code how u used slider, i will try that too. thanks

dear noman make the variable global and round up the value feeding to the port of arduino :)

Robin2: What is "the adioes"? Do you have a link to the where you got the code?

...R

you can download it from www.mathworks.com. go to the site and search arduino support package , its a zip file about 180kb . it has 3 folders named example,pde and simulink and some other files. in the pde folder you have 5 folders that adioes is in it after using matlab compile it to the board and then use it

Nouman-amin:

ashamed , ok thanks i will try it , i will vary speed like analogWrite(pin, 128) #for half speed. using push buttons, and how did u use the slider i also have confusion regarding this can u show me the code how u used slider, i will try that too. thanks

i already use push buttons to vary the analog value I have problem in using slider too :confused:

faiz-ali-shah: dear noman make the variable global and round up the value feeding to the port of arduino :)

yeah it worked #faiz, thanks buddy :)

Nouman-amin: yeah it worked #faiz, thanks buddy :)

Did it work with slider?? can you put the code here please?

faiz-ali-shah: dear noman make the variable global and round up the value feeding to the port of arduino :)

thankyou faiz i do that and it worked good work

ashamed: thankyou faiz i do that and it worked good work

ashamed please put the code of slider, that worked for me with push button only.

first define a global b then in the slider callback write these codes:

global a; global b; m = get(handles.slider1,'Value'); /////m is the slider string name b = round(m); a.analogWrite(12,b);

we must thank faiz for this solution THANK YOU FAIZ

ashamed: first define a global b then in the slider callback write these codes:

global a; global b; m = get(handles.slider1,'Value'); /////m is the slider string name b = round(m); a.analogWrite(12,b);

we must thank faiz for this solution THANK YOU FAIZ

thanks dear.. Yes #Faiz worked with me on it, we are together, He's my friend :)

really??? thats cool in that case you will be a good team it seems faiz has good talent in programming

ashamed: really??? thats cool in that case you will be a good team it seems faiz has good talent in programming

Yes, He has a good talent. and thanks to you too for involving with us.

your welcome