In: Computer Science
arduino programing
Question 1: write an APL program that will
print the following output on the serial monitor 10 times
only.
Output: 1
2
3
4
5
Question 2: write an APL program that will turn
the traffic lights on and off by taking input from the Serial port
working at 11500 bits processing rate. Use r or R for RED LIGHTS, y
or Y for YELLOW LIGHTS and g or G for GREEN LIGHTS.
Question 3: write an APL program that will
control two DC motors at 75 % PWM using ADA FRUIT STEPPER MOTOR
SHIELD. Your program must let user to send the direction in which
the motor rotates. F or F for FORWARD, b or B for BACKWARDS, r or R
for RIGHT, l or L for LEFT, and s or S for STOP.
here i am writing in arduino code after // this i will explain about that line
Question no.1:
void setup() {
Serial.begin(9600); //for this line
arduino will start communication with serail monitor at 9600
processing rate
}
void loop()
{
// it is also known as main function
for(int i=0; i<10;
i++)
// this for loop will run 10 times means it will print content 10
times on the serial monitor
{
for(int j=1; j<=5;
j++)
// this loop will run 5 times and will start with 1 and print 1 and
each and every loop it will increment by 1
{
Serial.println(j);
// it will print value of j with new line
}
}
}
Question no.2
#include<SoftwareSerial.h>
//need to add library file of serial port
SoftwareSerial mySerial(2, 3); // declaring
serial port under the barracket declare the rx pin and tx pin of
arduino
char val; // declaring the varibale which store
the value coming from serial port
int redled = 7; // declaring pin for
lights
int yellowled = 8;
int greenled = 9;
void setup()
{
// Setup function
mySerial.begin(11500);
// setting the data rate for the serial port to communicate at this
rate
pinMode(redled, OUTPUT);
//pimode for the lights
pinMode(yellowled, OUTPUT);
pinMode(greenled, OUTPUT);
void
off();
// calling off function for initially all lights will off when
program will start
}
void off()
{
digitalWrite(redled,LOW);
// creating off function
digitalWrite(yellowled,LOW);
digitalWrite(greenled,LOW);
}
void loop()
{
// main function
if(mySerial.available())
//it will checks for communication means data is coming or
not
{
val=mySerial.read();
// if coming then store the data in val variable
}
switch(val)
// switch case is the best control structure to control component
according to serail port data
{
case
'r':
// if val == r
void
off();
// then calling off function if any light is on then it will off
then
digitalWrite(redled,HIGH); // only
red light will on
break;
case
'R':
// if data will come in capital letter
void off();
digitalWrite(redled,HIGH);
break;
case
'y':
// similarly it is for all the lights
void off();
digitalWrite(yellowled,HIGH);
break;
case 'Y':
void off();
digitalWrite(yellowled,HIGH);
break;
case 'g':
void off();
digitalWrite(greenled,HIGH);
break;
case 'G':
void off();
digitalWrite(greenled,HIGH);
break;
default:
// if nothing data are come or another else data will come then it
call off function
void off();
break;
}
}
Question no.3:
#include<SoftwareSerial.h>
// library file for communication between ports
SoftwareSerial mySerial(2,3); //declartion of serail
port with rx and tx pin
const int
IN1=7; //
motor 1 of 2 pin
const int IN2=6;
//motor 1 of 2 pin
const int IN3=5; // motor
2 of 1 pin
const int IN4=4; // motor 2 of
2 pin
const int EN1=8; // sheild enable
pin
const int EN2=9; // sheild enable pin
char
val; //
declaring the varibale which store the value coming from serial
port
void setup() {
mySerial.begin(9600);
// start communication with serial port
pinMode(IN1,OUTPUT);
// pinmode of all motor and enable pin
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(EN1,OUTPUT);
pinMode(EN2,OUTPUT);
void stop();
}
void loop() {
analogWrite(EN1,192);
// here we are controlling speed of motor see when we use 100 % of
pwm then we have to give 255 value so here are using 75% of pwm
then 255 of 75% is around 192
analogWrite(EN2,192);
if(mySerial.available())
//it will checks for communication means data is coming or
not
{
val=mySerial.read();
// if coming then store the data in val variable
}
switch(val) // switch case is
the best control structure to control component according to serail
port data
{
case
'f':
// when serial read f then it call forward function
void forward();
break;
case
'F': // also when
serial read F then it call forward function
void forward();
break;
case
'b':
// similarly when b or B read then it call backward function
void backward();
break;
case 'B':
void backward();
break;
case
'r': //
similarly when r and R read then it call right function
void right();
break;
case 'R':
void right();
break;
case
'l':
//similalrly when l and L read then it call left function
void left();
break;
case 'L':
void left();
break;
case
's': // and
when s and S read then it call stop function
void stop();
break;
case 'S':
void stop();
break;
}
}
void
forward()
//creating forward function it will run the motor in forward
direction
{
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}
void
backward()
//creating backward function it will run the motor in backward
direction
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}
void
left()
//creating left function it will run the motor in left
direction
{
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}
void
right()
//creating right function it will run the motor in right
direction
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}
void stop()
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}