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

I\'m working with the MSP430g2553 using code composer. In my class we are progra

ID: 2084780 • Letter: I

Question

I'm working with the MSP430g2553 using code composer. In my class we are programming in C and recently had a quiz in which I needed to blink the red and green LEDs with an external button and to make them blink at different speeds with the onboard button SW. But I had a hard time doing this, the reason is because with the external button I needed to choose between lighting up the red LED, then the green, then Both, and same with speeds using the external but in this case.

My question is, is there a way to write a code using port1 interrupt and timer interrupts that whenever I press a button, the Red LED will turn on, and if I press the button again the green led turns on and the red led turns off, and if I press tje button again both LEDs will stay on. And after this for the velocities, if I press the other button it will read whatever I chose with the first button and then make it blink a certain velocity, if I press the second button again it will give another velocity and you know doing variations with this, if I choose the green button, and the second button will do the same as it was doing with the red led but now for the green button.

Therefore my question is, is this possible? and if it is, can you write me a code at least to know how to choose different options with one pushbutton?

Let's assume the onboard button of the launchpad BIT3 will be used to light the red LED and the green LED.

I don't know how to do this and this is not shown in class. Thank you in advance.

Explanation / Answer

//LED for different switching sequence of different LEDS

**************************************
const int BUTTON = 5; //use button on board
const int LED = 14; //useredledonboard
//
int ledState = LOW; //currentledState
int buttonState; //currentbuttonState
int lastButtonState = LOW; //previous buttonState
long time = 0; //lasttimeswitchispressed
long debounce = 50; //debouncetime
void setup()
{
//set I/O pins
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
}
void loop()
{
int buttonState = digitalRead (BUTTON); //is switch press
//
//when button is press & (previous & current state is different) & (previous - current time > debounce)
if (buttonState == HIGH && lastButtonState == LOW && millis() - time > debounce)
{
if (ledState == HIGH) //when LED IS ON
{
ledState = LOW; //turn LED OFF
}
else //when LED is OFF
{
ledState = HIGH; //turn it ON
}
time = millis(); //save time after releasing the button
}
digitalWrite (LED, ledState); //output LED
lastButtonState = buttonState; //save the previous buttonState to current buttonState
}

**************************
//for green led to output direction
#include <msp430g2553.h>

void main( void )
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

P1DIR |= BIT6; // Set P1.6 (green LED) to output direction, 1 is output
P1OUT &= ~BIT6; // Set the green LED off
P1DIR &= ~BIT3; // Port 1 P1.3 (push button) as input, 0 is input
P1REN |= BIT3; // Enable Port 1 P1.3 (push button) pull-up resistor
P1OUT |= BIT3; // The button is up
//
while( 1 )
{
if( (P1IN & BIT3 ) == 0 ) // Push button down when bit 3 == 0
{
if( (P1OUT | BIT6) == 0 )
{
P1OUT &= ~BIT6;
}
else
{
P1OUT |= BIT6;
}
}
}
}
************************