View the step-by-step solution to:

@ Modified_ForTune |Arduin0 1.5 [I X Hm Edh ShdwTooh Hem Modifiedfortune f"MDdified Fortune*/ int ledl = 12; int led2 = 13; int led3 = 15; int...

I am having trouble setting up a while loop in Matlab that will interact with LED's on an arduino board that has already had a program uploaded to it. I keep receiving error messages in command:  

>> Modified_Fortune

Press 1, 2, 3, or 4: 1

Press 1, 2, 3, or 4: 2

Error using serial/fwrite (line 199)

Unsuccessful write: OBJ must be connected to the hardware with FOPEN.

Error in Modified_Fortune (line 13)

fwrite(s,'1'); %activates pin 12 on arduino board




HERE'S MY CODE WITH SCREENSHOTS AT THE END:

delete(instrfind({'Port'},{'COM3'}))

clear all;

s=serial('COM3');

fopen(s);

while 1

a=input('Press 1, 2, 3, or 4: ', 's');

fwrite(s,'1'); %activates pin 12 on arduino board

fwrite(s,'2');%activates pin 13 on arduino board

fwrite(s,'3');%activates pin 15 on arduino board

fwrite(s,'4');%activates the channel 12,13,15 on arduino board.

fclose(s);

end


Modified_Fortune4.jpg

Modified_Fortune1.jpg

Modified_Fortune2.jpg

Modified_Fortune3.jpg

Modified_Fortune1.jpg

@ Modified_ForTune |Arduin0 1.8.5 — [I X
Hm Edh ShfidwTooh Hem Modifiedfortune f“MDdified Fortune*/ int ledl = 12; int led2 = 13; int led3 = 15; int eharsRead; int matlabData; char buffer[4]; [/setting the Character size to a list of 10 void setupt) { Serial.begin(9600}; // Serial.println("Please enter a number 1 to 10 to receive today's fertune:"}; void loopt] {
pinMedetledl, OUTPUT]: f/red pinMedetled2, OUTPUT]; f/yellow pinModetledB, OUTPUT]; [/green matlabData = Serial.read(l; // read data and can mess up program inside loop
while(Serial.available(]==0]; l/waits for input ffthe next 3 lines prevents an infinite loop as it helps control buffering
charsRead = Serial.read‘BytesUntil('\n"Ir buffer, sizeefibuffer] - 1];
buffer[charsRead] = '\fi'; matlabData = ateitbuffer}; Serial.print(“Here's your fortune: “l; { if (matlabData == 1]
{ digitalWrite(12, HIGH] ; v

Modified_Fortune2.jpg

@I Modifiedjortune | Alduino 1.8.5 File Edit Sketch Tools Help Modified_F0rtune digitalWritEIiB, LOW];
digitalWrite (15, LOW] ;
delayiflflfl]; Serial.print(" You will be hungry in one hourl"); Serial.print('\n'j; }
else if (matlabData == 2) {
digitalWritetlz, LOW];
digitalWrite (13, HIGH];
digitalWritetls, LOW);
delaylflflfl); Serial.print(" You will and up J+¢ OJ Jijll"); llbigger than a mountain
Serial.print('\n'];
}
else if (matlabData == 3)
{
digitalWritet12, LOW);
digitalWritetlB, LOW];
digitalWrite (15, HIGH);
delayiflflfl); Serial.print(" The great danger juBt be youraelfl");
Serial.print('\n'};
Il
else if {matlabData == 4]
{
digitalWrite (12, HIGH];
digitalWritet13, HIGH];
digitalWritetis, HIGH]; v

Modified_Fortune3.jpg

ulgluulnlluu ‘4... Ala—Hill , delayiflflfl]; Seria1.print(" You‘ll be blessed with diamonds...but you won't be able to sell them!"]; Serial.print('\n‘]; } else {
Seria1.print(" ND FORTUNE TDDAY!"]; Seria1.print('\n‘];

Modified_Fortune4.jpg

i0 UsevsuemIz\OneDrIve\Documents\MATLAB\H0mewovk\4 3\Mod|fled,Fonune.m \qummbwmh‘ 10
11
12
13
14
15
16
17
18
19
20 %https : llwww .mathworks . com/matlabcentral/answers/307 996*ErduinO*SEOp*EO*CO
%Press a number between 1 e 5 and receive a fortune! %Refer to Arduino code Modified_Fort.une. %Tone modified our; support packages not working. % deletefinstrfind” '1’ort' )1 ( 'COM3' H) clear gig,- 5=5erial € ‘ COM3 ' ) .' fopen‘S): while 1
E:input('Pre55 1, 2, 3, or 4: ', '3'),-
fwrite(5,‘1'): %activate5 pin 12 on erduino board
fwrite(5, '2'):%activate5 pin 13 on erduino board
fwrite(s,‘3');%activates pin 15 on arduino board
fwrite(s,‘4');%activates the channel 12,13,15 on arduino board.
fclose(s); end unica1:erwithethercomputeredurinQraewhileeloop scnpt Ln 19 Col 1

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question