This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP 432 LED codes

Other Parts Discussed in Thread: ENERGIA

Hi. Can someone help me with debugging the codes below? It is for toggling between 2 LEDs using Bluetooth operated by a phone app I designed using MIT app inventor. Below consist of 2 separate codes for energia. I use 2 tabs in energia to run both codes simultaneously to make LED1 light up when my click LED1 on my phone app and vice versa for LED2. Thanks in advance.

CODE 1:

void setup()

{

pinMode(39,OUTPUT);

Serial.begin(9600);

Serial1.begin(9600);

}

void loop()

{

while(Serial1.available()>0)

{

char message =Serial1.read();

if(message=='on2')

{

digitalWrite(39,HIGH);

}

else if (message=='off2')

{

digitalWrite(39,LOW);

}

else

{

analogWrite(39,int(message));

}

}

}

CODE 2:

void setup()

{

pinMode(40,OUTPUT);

Serial.begin(9600);

Serial1.begin(9600);

}

void loop()

{

while(Serial1.available()>0)

{

char message =Serial1.read();

if(message=='on1')

{

digitalWrite(40,HIGH);

}

else if (message=='off1')

{

digitalWrite(40,LOW);

}

else

{

analogWrite(40,int(message));

}

}

}

  • Hi Justin!

    Could you describe your problem a little bit? What issues are you facing? What works and what does not? Furthermore please edit your post and use the Syntaxhighlighter in rich formatting mode for posting code - it is the </> symbol.

    Dennis

  • Sorry for being vague and the late reply, still new to this whole thing. The hardware i am using is HC-05 bluetooth, 2 LEDs (located at pin 40 and pin 39 ) and a MSP432P401R launchpad and as mentioned earlier i am using energia to program it.

    The app i created on MIT app inventor sends a text message, "on1", "off1", "on2" and "off2" to switch the corresponding LEDs (where "on1" and "off1" controls LED 1 while "on2" and "off2" controls LED 2) on and off via bluetooth. The code "analogWrite(40,int(message));" is the slider function to control the brightness of the LED. This only works when i was only controlling one LED even the slider function. When added another LED however, the LEDs did not light up accordingly. The LEDs lights up randomly no matter which button was pressed (eg: LED 1 lights up when "led 2 off" is pressed).

    I don't really think it is the app that is causing this problem, but if you wish to see it i could share a screenshot of what my app looks like. And again thank you in advance.
  • Hi Justin,

    My first suggestion would be to combine everything into one big loop, that way there aren't two separate files.

    I think where you are running into trouble is in the if, else if, and else statements. I'd consider using case and switch statements instead. I've outlined an example of how this might look, below. The tricky part about a case and switch is that each case must be an int value, so you'd have to convert the raw serial data first. In the example I skipped that part and put in numbers instead of the 'on1'/'off1' string. I'd suggest first trying this with simple int values before you switch to the char implementation.

    void loop() {
    
    if (Serial.available() > 0) {
    
       int inByte = Serial.read();
    
       switch (inByte) {
    
         case '1':  //on1
    
           digitalWrite(40, HIGH);
    
           break;
    
         case '2':  //off1
    
           digitalWrite(40, LOW);
    
           break;
    
         case '3':  //on2
    
           digitalWrite(39, HIGH);
    
           break;
    
         case '4': //off2
    
           digitalWrite(39, LOW);
    
           break;
    
         }
    
    }
    
    }

    Thanks,

    Mitchell

**Attention** This is a public forum