Write a program that will allow keyboard commands to change
Write a program that will allow keyboard commands to change motor speed throughthe change of PWM duty cycle, and show these analog input values on the screen once a second. Please note that this may be noisy and using the average of multiple readings may be required to reduce the noise. User should have the option to press“+” or “-“for increasing and decreasing speed of the motor, as we as “q” and “h” for stopping the operations and program completely or for help (where options to beentered are listed), respectively.
Write a program that implements the feedback controller. Put the feedback loop in an interrupt subroutine, and use the main program for keyboard I/O
Please write using Arduino language
Solution
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include \"sio.c\"
int count = 0;
void IO_setup() {
DDRB = 0xFF;
}
void IO_update() {
PORTB = count;
}
#define CLK_ms 10
unsigned int CNT_timer1;
volatile unsigned int CLK_ticks = 0;
volatile unsigned int CLK_seconds = 0;
SIGNAL(SIG_OVERFLOW1) {
CLK_ticks += CLK_ms;
if(CLK_ticks >=1000) {
CLK_ticks = CLK_ticks -1000;
CLK_seconds++;
IO_update();
}
TONT1 = CNT_timer1;
}
void CLK_setup() {
TOCR1A = (0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0)
| (0 << WGM11) | (0<<WGM10) | (0<<FOC1A | (0<<FOC1B);
TOCR1B = (0 << WGM12) | (0<<WGM13) | (0<<ICNC1 | (0<<ICES1) |
(1<<CS12) | (0<<CS11) | (1<<CS10);
CNT_timer1 = 0xFFF - CLK_ms + 8;
TONT1 = CNT_timer1;
TOFR& = ~(1<<TOV1);
T1MSK = (1<< TOIE1);
sei();
}
int main()
{
int c;
sio_init();
IO_setup();
CLK_setup();
for(;;) {
while((c=input())==-1){}
if(c == \'+\') {
if(++count > 255) count = 255;
outln(\"counter incremented\");
}
else if(c == \'-\')
{
if(--count < 0) count = 0;
outln(\"counter decremented\");
}
else if(c== \'p\') {
outln(count);
}
else if(c== \'h\') {
outln(\"HELP: +, -, p, q\");
}
else if(c== \'q\')
{
break;
}
}
sio_cleanup();
return 1;
}



