In: Computer Science
•Create a state machine that is sort of like a computer mouse with right clicks and left clicks. Instead it uses pB1 and pB2.
–Two fast clicks of pB1 turns on led 1 for 5s
–Click on pB2 from the idle state you simply turn on LED2 while the button is held in
Below is a state machine I've set up to turn on an LED for 5
s
if a pB is pushed one time, which is only a small part of what i
need, i need to make it so it will take a double click instead of a
single click and i also need to add a part that turns on a
different LED that only turns on when the buttons is pressed, if
you can help me complete my code that would be great, thank
you
#include <myGPIO.h>
#include <myTimer.h>
void setup() {
init_gpio_b(4,0); // PB init for input
init_gpio_b(5,0);
init_gpio_d(2,1); // Init LED outputs
init_gpio_d(3,1);
// init_TimerInterrupt();
Serial.begin(9600);
}
void loop() {
while(1){
whichCase();
myDelay1ms(5);
}
}
void whichCase(void){
static int val=0;
static int state=0;
static int count=0;
Serial.println(PORTD);
switch(state){
case 0:{ //****** Idle ****
// *** Do what the state does ****
gpo_d(3,1);
// *** criteria to leave the state ****
if(gpi_b(4)==0)state=1;
}
break;
case 1:{ //****Wait****
// *** Do what the state does ****
count++;
// *** criteria to leave the state ****
if(count>100){
state=0;
count=0;
}
else if(gpi_b(4)==1){
state=2;
count=0;
}
}
break;
case 2:{ //****ledOn****
// *** Do what the state does ****
gpo_d(3,0);
count++;
// *** criteria to leave the state ****
if(count>1000){
state=0;
count=0;
}
}
break;
default:{
break;
}
}
}
LED configurations with the LE abbreviation in the code
/*
* Mouse clicking panels
*
* configuration for pin B1
* Configuration for pin B2
* Configuration for LE 1
*
* please Mention the name of your project to be displayed in
panel
*/
// Assign different pin colours to the System so that you can easily differentiate
int red = 6, yellow = 5, green = 4;
// Different System Variables
byte state = 0; // Mentioning the initial state
void setup() {
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
}
void loop() {
switch(state){
case 0:
digital_Write(red, HIGH);
digital_rite(yellow, LOW);
digital_rite(green, LOW);
delay(1000);
state = 1;
break;
case 1:
digital_rite(red, HIGH);
digital_rite(yellow, HIGH);
digital_rite(green, LOW);
delay(3000);
state = 2;
break;
case 2:
digital_rite(red, LOW);
digital_rite(yellow, LOW); // i have designed different clicking
patterns as per your // requirment in the above given
question
digital_rite(green, HIGH);
delay(800);
state = 3;
break;
case 3:
digital_rite(red, LOW);
digital_rite(yellow, HIGH);
digital_Write(green,Low);
delay(200);
state = 0;
break;
default:
break;
}
}