Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

Blinking LED in a random interval

$
0
0

What I am trying to do is, making a PIN switch between its states (HIGH and LOW) with a random interval. My experiment outline is:

 

There are two inputs from user, 1 and 2. An user input of 1 makes the LED to blink (or PIN to switch between HIGH and LOW) with a random interval between (500 to 1000) ms. If the user input is 2, the random interval becomes (100 to 500) ms. The system is should always listening to the user input, and as soon as it gets a new input, it adjusts the random delay accordingly. 

 

For example, the user gives an input of 1. The MSP430 chip generates a random number between (500 to 1000) ms. Let's assume it's 569 ms. Then the LED will blink with an interval of 569 ms. During the entire process, the launchpad will wait for the user input from serial monitor. If there's no input, it keep continuing with the input of 1, i.e., in the second cycle, it again generates a random number between 500 to 1000, and blink the led with specified delay. During this process, if the user passes an input of 2, the program will generate an different delay, between (100 to 500)  ms. This delay range will continue until user does not passes a new input. 

 

I've written some piece of code, of stuck at one place. I really cannot figure out how to switch between delays, based on user input. Right now, if I pass 1 as an input initially, it keep going with delays associated with 1. I know, it's  very novice question, but I am really not good at programming. Any help will be appreciated. 

 

Here goes my piece of code:

 

int incomingByte = 0;   // for incoming serial data


void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
        pinMode(2,OUTPUT);
}


void loop() {


        // send data only when you receive data:
        while (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();
                Serial.print ("The incomingByte value is: ");
                Serial.println (incomingByte, DEC);
                
                while (incomingByte == '1'){
                  Serial.print ("Now disrupting in slow mode with delay: ");
                  digitalWrite(2,HIGH);
                  int randomDelay = random(500,1000);
                  delay(randomDelay);
                  digitalWrite(2,LOW);
                  delay(randomDelay);
                  Serial.println(randomDelay);




                  /*incomingByte = Serial.read();
                  Serial.print("New incomingByte is: ");
                  Serial.println(incomingByte);
                  if (incomingByte == 10){
                    continue;
                  }
                  if (incomingByte == '2'){
                    break;
                  }*/




                }
                
                  while(incomingByte == '2'){
                  Serial.print ("Now disrupting in fast mode with delay: ");
                  digitalWrite(2,HIGH);
                  int randomDelay = random(100,500);
                  delay(randomDelay);
                  digitalWrite(2,LOW);
                  delay(randomDelay);
                  Serial.println(randomDelay);




                  /*incomingByte = Serial.read();
                  if (incomingByte == 10){
                    continue;
                  }
                  Serial.print("New incomingByte is: ");
                  Serial.println(incomingByte);
                  if (incomingByte == '1'){
                    break;
                 
                  }*/




                }
             }
                  


        }

 


Viewing all articles
Browse latest Browse all 2077

Trending Articles