Offline
Full Member
Karma: 2
Posts: 136
W9LZ
|
 |
« on: February 03, 2013, 05:49:47 pm » |
I've built a kluge that performs 5 functions. I can make any of the five work perfectly as standalone sketches. I'm trying to use the Serial Monitor to input a selection that through the use of a switch. The switch/case combo works except it only allows one iteration of the called function. If I send say "1111111" it will run 7 times, "2222" 4 times, etc. Anyone have an idea on a slick method to pull this off?
|
|
|
|
« Last Edit: February 03, 2013, 05:55:09 pm by Learning »
|
Logged
|
|
|
|
|
East Anglia (UK)
Offline
Edison Member
Karma: 55
Posts: 1601
May all of your blinks be without delay
|
 |
« Reply #1 on: February 03, 2013, 06:00:34 pm » |
Show us the code !
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 136
W9LZ
|
 |
« Reply #2 on: February 03, 2013, 07:01:01 pm » |
Here's the basic structure. Thanks for looking at it. static char mode; #include <NewPing.h> #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor. #define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor. #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. int FLASH_PIN = 13; int MIC_PIN = 0; NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() { Serial.begin(115200); pinMode(MIC_PIN, INPUT); pinMode(FLASH_PIN, OUTPUT); } void loop() { if(Serial.available()) { char mode = Serial.read(); Serial.println(mode); switch(mode) { case '1': Sound(); break; case '2': Ultrasonic(); break; case '3': Proximity(); break; case '4': Magnetic(); break; case '5': Mystery(); break; } } } void Sound() { Serial.print("Sound"); // while(mode == '1') { // if(Serial.available()) { char mode = Serial.read(); } int value = analogRead(MIC_PIN); Serial.println(value); { if (value < 120) { Serial.print ("SDFGHJKLJHGFDGHJKL:JHGFSDGHKJHGFSDSGHJKHGFSD"); digitalWrite(FLASH_PIN,LOW); } else digitalWrite(FLASH_PIN,HIGH); } } } void Ultrasonic() { // while(mode == '2') { // if(Serial.available()) { char mode = Serial.read(); }
Serial.print("Ultrasonic"); Serial.println(mode); delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS). Serial.print("Ping: "); Serial.print(uS / US_ROUNDTRIP_CM / 2.54); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo) Serial.println("in"); if (uS / US_ROUNDTRIP_CM /2.54 < 3) // 12 inch out { digitalWrite(FLASH_PIN,LOW); // flash it! } else { digitalWrite(FLASH_PIN,HIGH); // don't flash it! } } } void Proximity() { // while(mode == '3') { // RF disrupter sketch here Serial.print("Proximity"); Serial.print(mode); } } void Magnetic() { // while(mode == '4') { // Hall effect sketch Serial.print("Magnetic"); Serial.print(mode); } } void Mystery() { //while(mode == '5') { // Jack Bauer special Serial.print("Mystery"); Serial.print(mode); } }
|
|
|
|
« Last Edit: February 03, 2013, 09:18:54 pm by Nick Gammon »
|
Logged
|
|
|
|
|
North Queensland, Australia
Offline
Edison Member
Karma: 35
Posts: 1279
|
 |
« Reply #3 on: February 03, 2013, 07:16:58 pm » |
I've built a kluge that performs 5 functions. I can make any of the five work perfectly as standalone sketches. I'm trying to use the Serial Monitor to input a selection that through the use of a switch. The switch/case combo works except it only allows one iteration of the called function.
If I send say "1111111" it will run 7 times, "2222" 4 times, etc. Anyone have an idea on a slick method to pull this off?
You say it only allows one iteration, then its running 7 and 4 times. Is your intention to only respond to one of the '1' in "1111111"? If so, flushing the RX buffer may help after reading the value: while( Serial.available() ) Serial.read();
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14121
Lua rocks!
|
 |
« Reply #4 on: February 03, 2013, 07:31:03 pm » |
Read this before posting a programming questionPlease edit your post, select the code, and put it between [code] ... [/code] tags. You can do that by hitting the # button above the posting area.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 136
W9LZ
|
 |
« Reply #5 on: February 03, 2013, 07:46:20 pm » |
No, the intention is to dynamically switch between functions by doing a single key entry through the serial monitor. It will 'run' the function each time it receives a number like 11111, but I want it to continue running that function until I enter another choice.
|
|
|
|
|
Logged
|
|
|
|
|
North Queensland, Australia
Offline
Edison Member
Karma: 35
Posts: 1279
|
 |
« Reply #6 on: February 03, 2013, 07:56:24 pm » |
Maybe something like this: void loop(){ static char mode = '1'; //Default starting mode.
//Get new mode if available if(Serial.available()){ mode = Serial.read(); }
//Now your switch Serial.println( mode );
switch( mode ){ //... } }
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 31
Posts: 1733
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #7 on: February 03, 2013, 08:03:26 pm » |
What about reading it from serial and then store it in a temporary variable. Now with that variable you can check if any new incoming chars are different or not. If it is different, do another function, if not, do same function.
Use a while loop to compare the variables.
|
|
|
|
« Last Edit: February 03, 2013, 09:19:40 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 136
W9LZ
|
 |
« Reply #8 on: February 03, 2013, 08:05:56 pm » |
Still only runs once and stops. I was pretty sure I tried this about 3 days ago, haha. Driving me nuts.
|
|
|
|
|
Logged
|
|
|
|
|
North Queensland, Australia
Offline
Edison Member
Karma: 35
Posts: 1279
|
 |
« Reply #9 on: February 03, 2013, 08:18:56 pm » |
Well how are you sending the data, if you send a string with a null terminator you need to account for it. You could flush all remaining characters. //Get new mode if available if(Serial.available()){ mode = Serial.read(); while( Serial.available() ) Serial.read(); } Or maybe even better, check if the character is what you are looking for: //Get new mode if available if(Serial.available()){
char temp = Serial.read();
if( ( temp >= '1' ) && ( temp <= '5' ) ){ mode = temp; } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 136
W9LZ
|
 |
« Reply #10 on: February 03, 2013, 09:03:31 pm » |
You could flush all remaining characters.
Code:
//Get new mode if available if(Serial.available()){ mode = Serial.read(); while( Serial.available() ) Serial.read(); }
Yep, this was the bug! The old null character being ignored trick. Thank you!!!
-Jim
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14121
Lua rocks!
|
 |
« Reply #11 on: February 03, 2013, 09:20:45 pm » |
You have multiple declarations of mode. That is bad practice: static char mode; ... char mode = Serial.read(); ... char mode = Serial.read(); ... char mode = Serial.read();
These are all different variables. The resulting behaviour may not be what you expect.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 136
W9LZ
|
 |
« Reply #12 on: February 03, 2013, 10:03:08 pm » |
Thanks for the "positive" response.
|
|
|
|
|
Logged
|
|
|
|
|
|