Using Audrino, 1) write a sketch that prints the integers in inscreasing order f
ID: 674771 • Letter: U
Question
Using Audrino,
1) write a sketch that prints the integers in inscreasing order from 1 to n then in decreasing order from n-w to 1. Print all values in a single row and leave at least 2 blank spaces between each value on the row. In other words, write the program to create the following output in Serial Monitor when n=7 (1 2 3 4 5 6 7 6 5 4 3 2 1) In your solution declare n as variable.
2) Extend the preceeding program by repeating the print out values of n=3,5,7,9 in the same program. The output of your program in the serial moinitor should look ilike this
(1 2 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 7 6 5 4 3 2 1)
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1)
Explanation / Answer
Program:1
int n = 3; //
int i = 1;
int j = n-1;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop() // run over and over again
{
// print out the state of the button:
if(i<=n) //printing forward
{
Serial.print(i+" ");
i=i+1;
}
if(i>n) // printing reverse
{
Serial.print(j+" ");
j=j-1;
}
delay(1000); // delay in between reads for stability
}
Program 2:
int n = 3;
int i = 1;
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop() // run over and over again
{
// print out the state of the button:
if(i<=n)//printing forward
{
Serial.print(i+" ");
i=i+1;
}
if(i==n)
{
j=n-1;
i=i+1;
}
if(i>n && j>0) // printing reverse
{
Serial.print(j+" ");
j=j-1;
}
if(j==0)
{
Serial.println("");
if(n<9)
n=n+2; // increment n to have values like 3, 5, 7,9
i=1;
}
delay(1000); // delay in between reads for stability
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.