Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Arduino Code: 2.3 Using Switches to change direction with Interrupts and lots of

ID: 2084138 • Letter: A

Question

Arduino Code:

2.3 Using Switches to change direction with Interrupts and lots oflights flashing
[Libraries of Energia] Usethe millis() function to blink the LEDs, parseInt() for figuring out desired angle; just to name afew. For this part of this lab, pass desired absolute angle that you want the servo to rotate tovia the “Serial Monitor.” This will most likely be used as the bases for your final project tocontrol your servomotor. If the following commands are passed in to your microcontroller “R” rotate all the way right and stop at max angle lets say 180° “L” rotate all the way left and stop at max angle lets say 0° in this caseYou should give plenty of time to get to any desired angle, i.e. you need to keep track of yourstack and estimate the amount of time required to get to a desired angle, e.g. if I pass in(assuming starting at 0°) “R,90,R,5,L” it should go to 180° first then to 90°, back to 180°, then to5°, and finally 0°. Another example if I pass in “135,5,90,45,0” it will go from 0° to 135° thento 5°, ect. For example, the HS-322HD servomotor at 4.8V will rotate at a speed of 0.19sec/60°with no load, but if you don’t use the level shifter the servo will actually be supplied with avoltage 3.3V for its pulses in which case you probably want give a bit more time, e.g. like0.30sec/60°, but this a will be a judgment call on your case (probably should make this easy tochange).

Bonus 10% Part
Need to implement part 2.3 first. Use the Green LED for clockwise, blink at a different rate thanthe Blue LED for counter clockwise, and solid on red LED when stopped (can’t use delay()function for this, servo has to move continuously).1. Relative angle of rotation to be sent via “Serial Monitor”2. Speed to desired angle you pick3. Display on “Serial Monitor” the absolute angle of rotation, e.g. start is 0° and end should

be ~180°a. You have to keep track of this based off direction and angle you put in to rotate

the servob. You probably should define one direction as positive and the other as negative orleft and right

4. Blink Green LED at 2Hz when rotating clockwise (other LEDs should be off)5. Blink Blue LED at 3Hz when rotating counter clockwise (other LEDs should be off)6. When stopped (either by button push or maxed out angle, i.e. the servo has stopped forany reason) should show solid red LED (other LEDs should be off)Basically, at the frequencies we are asking you to blink your LEDs, if you were to use the delay()function it would stop and start the servo very obviously and this is not what is wanted. Anadditional reference to look at is the Blink without delay example.

Explanation / Answer

Answer for Part 2.3 - Arduino program for desired anglecontrol from serialmonitor

Above stated requirement can be easily met by using the standard available libraries. Here I use servo.h library using which the servo can be controlled easily.

#include<servo.h>

#include<string.h> / Include the built in library which is available in arduino public libraries

String readstring =String(100);

int servopin = 4; / Declare the servo pin or pin number to which the servo motor control signalwire connected to arduino

Servo Servo1; / Create a servo object or name the servo motor ou connect

void setup()

{

Servo1.attach(servopin); /Attach the servo motor to the arduino digital pin

serial.begin(9600);

}

void loop()

{

char ch ;

int angle;

Serial.println("System is ready");

Serial.println("Enter the desired angle to rotate the servo motor");

while (serial.available())

{

delay(10);

ch =Serial.read(); /Reads one byte of character from serialmonitor

if (ch == "R")

{

angle =180; /180 degrees

Servo1.write(angle);

}

else if( ch == 'L')

{ angle = 0;

Servo1.write(angle);

}

else if( ch >= '0')

{

raedstring.append(ch); / converting the one byte character as string

serial.println(raedstring);

int n = atoi(readstring); convert string to number

Servo1.write(n);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote