BruteForce 0 Posted April 27, 2015 Share Posted April 27, 2015 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; }*/ } } } Quote Link to post Share on other sites
enl 227 Posted April 27, 2015 Share Posted April 27, 2015 There are several ways to interpret the root issue, so I'll pick one: When the input is '1', the first inner loop is entered, but never exited, as incomingByte is never changed within the loop. I'll spend a few minutes to address some other things in another post... Quote Link to post Share on other sites
enl 227 Posted April 27, 2015 Share Posted April 27, 2015 Deeper issues: The structure you have is actually fine if you want a SINGLE blink per input. Change the inner 'while's to 'if's. If you want repetitive blinking, with the inputs changing the state, the structure you have isn't really ideal, but can be used.What you might try is adding an additional condition to each of the inner while loops, similar to this: while (incomingByte =='1' && Serial.available()==0) This will cause the inner loop to end when another input is available, so that the outer loop can read it. A better overall structure for the code could be selected, as well, but I will avoid that for the moment. BruteForce 1 Quote Link to post Share on other sites
BruteForce 0 Posted April 27, 2015 Author Share Posted April 27, 2015 Thank you very much. At first, I would apologize for my incomplete post. I did try some code to get out of this loop, those are commented. What I posted here is just to keep running for the same input. I was stuck on getting out of the loop part. I am gonna try your suggestion and let you know about the result. Thanks again... I really appreciate. Quote Link to post Share on other sites
abecedarian 330 Posted April 27, 2015 Share Posted April 27, 2015 This doesn't require any 'while' loops be used since the "void loop()" structure will loop indefinitely on its own. My suggestion would be check if there's a character available in the serial buffer and if so, read it. Then based on what that character is, either '1' or '2', set your delay variable accordingly, then toggle the output pin as desired. If the character isn't '1' or '2', don't do anything other than keep toggling the pin. int togglePin = 2; // pin to toggle int incomingByte = '1'; // for incoming serial data, default to 'slow' blink int randomDelay; // stores random slow blink rate, will change later /* Setup system */ void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps pinMode(togglePin, OUTPUT); // set toggling pin to output mode } /* Main program loop */ void loop() { if (Serial.available()) { // Character in serial buffer? incomingByte = Serial.read(); // get the character Serial.print ("The incomingByte value is: "); Serial.println (incomingByte, DEC); // Output what was typed to the user } if (incomingByte == '1'){ // Was the character a '1'? randomDelay = random(500,1000); // generate a "slow" random delay Serial.print ("Now disrupting in slow mode with delay: "); Serial.println(randomDelay); // Output delay interval to the user incomingByte = '0'; // set to '0' so we don't jump back in here later } if (incomingByte == '2'){ // Was the character a '2'? randomDelay = random(100,500); // generate a "fast" random delay Serial.print ("Now disrupting in fast mode with delay: "); Serial.println(randomDelay); // Output delay interval to the user incomingByte = '0'; // set to '0' so we don't jump back in here later } /* toggle the pin according to the randomDelay value determined above */ digitalWrite(togglePin, HIGH); delay(randomDelay); digitalWrite(togglePin, LOW); delay(randomDelay); } // loop over and over. BruteForce and dubnet 2 Quote Link to post Share on other sites
BruteForce 0 Posted April 28, 2015 Author Share Posted April 28, 2015 Thank you very much! Thanks a lot! I really appreciate your support. Quote Link to post Share on other sites
abecedarian 330 Posted April 28, 2015 Share Posted April 28, 2015 @@BruteForce - I hope what I did makes sense... and works how you desired. If you want it to continue generating random intervals, comment out / delete the "incomingByte = '0';" statements. Another potential issue with this is if the user inputs an invalid number, it will stop generating random values too, so might want some sort of check against the value before assigning it to incomingByte. There are more elegant ways to go about it too, but I went for simple, straightforward. Quote Link to post Share on other sites
energia 485 Posted May 1, 2015 Share Posted May 1, 2015 also see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers Wrong thread ;-) Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.