Using a pot to select sketches

Hey everyone, this is my first posting. I recently started throwing together some arduino projects such as face tracking, servos controlled with a mouse, sonar etc with processing. I am somewhat of a beginner though and just followed the tutorials, Ive adjusted my code and hardware so all these features work without changing any wiring etc. I then wrote a program to display different numbers with a 7 segment led from radioshack. My idea was as it switches numbers to switch sketches and ultimately throw the more advanced sketches on there once I got the hang of it. I got some basic one to pair for the time being like blink and fade but when I tried to use a basic sonar testing program it seems like the sensor falls out of sync if something is not in front of it or moves out of a certain range. It does not reset though the code I transferred works fine when it stands alone. I am sure I am missing some loop or parenthesis. Can anyone help point me in a right direction? I followed an ada merging tutorial and a good one i found on vimeo. Here is the code:

int led = 11;
int brightness = 0; // how bright the LED is
int fadeAmount = 5;
#define echoPin A2 // Echo Pin
#define trigPin A3 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A4);
// print out the value you read:
Serial.println(sensorValue);
//0
if (sensorValue < 100 && sensorValue >= 0){

//0
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
digitalWrite(9, 1);
digitalWrite(10, 0);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second

delay (100);}

if (sensorValue < 200 && sensorValue > 100){

//1
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
digitalWrite(9, 1);
digitalWrite(10, 0);
analogWrite(led, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay (100);}
//2
if (sensorValue < 300 && sensorValue > 200){
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
digitalWrite(9, 1);
digitalWrite(10, 0);

/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" /
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
}
else {
/
Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}

//Delay 50ms before next reading.

delay (100);}
//3
if (sensorValue < 400 && sensorValue > 300){
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
digitalWrite(9, 1);
digitalWrite(10, 0);
delay (100);}
//4
if (sensorValue < 500 && sensorValue > 400){
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 0);
digitalWrite(9, 1);
digitalWrite(10, 0);
delay (100);}
//5
if (sensorValue < 600 && sensorValue > 500){
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
digitalWrite(9, 0);
digitalWrite(10, 0);
delay (100);}
//6
if (sensorValue < 700 && sensorValue > 600){
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
digitalWrite(9, 0);
digitalWrite(10, 0);
delay (100);}
//7
if (sensorValue < 800 && sensorValue > 700){
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
digitalWrite(9, 1);
digitalWrite(10, 0);
delay (100);}
//8
if (sensorValue < 900 && sensorValue > 800){
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
digitalWrite(9, 1);
digitalWrite(10, 0);
delay (100);}
//9
if ( sensorValue > 900){
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
digitalWrite(9, 1);
digitalWrite(10, 0);
delay (100);
}
}

I am sure I am missing some loop or parenthesis

Not to mention code tags.

As @Awol says, you need to edit your post and put that long code in code tags to make it easier to read.

There seems to be an awful lot of repetition in it. Do you know how to use FOR and WHILE loops?

Could you write a short sketch that just illustrates the selection mechanism you want to use?

Do you know that an Arduino can only have one sketch loaded at any one time. So a selection mechanism can choose between different parts of a sketch, but can't choose a different sketch?

...R

I apologize for that. I actually have made excellent success for the most art with my set up. So what this code does is when the pot has an analog value of 0-100, the basic "blink" sketch activate. when the pot has a value of 101-200, the basic fade sketch starts, when the pot has a value of 201-300 , a basic sonar testing sketch activates. this whole time the serial monitor is displaying the analog pot value and distance during the last stage. 301-400 is the tricky one. I want this to activate a sketch that is used in conjunction with a Processing sketch and makes a sonar using the ultrasonic range finder sensor from the last sketch and a servo . I have gotten it to work with the following code but when I use the straight regular code it works more smoothly. After being processed through my code the sonar is more choppy. I also had to remove the serial monitor function because an initial requirement was if the serial monitor was available. I have tried to remove this command which compiles but then fails with processing . Here is my code, the original sonar code and the processing code. Thanks!

Potselect.ino (7.24 KB)

Sonar.ino (3.34 KB)

Sonar.pde (3.13 KB)

Arduro:
301-400 is the tricky one. I want this to activate a sketch that is used in conjunction with a Processing sketch and makes a sonar using the ultrasonic range finder sensor from the last sketch and a servo . I have gotten it to work with the following code but when I use the straight regular code it works more smoothly. After being processed through my code the sonar is more choppy. I also had to remove the serial monitor function because an initial requirement was if the serial monitor was available. I have tried to remove this command which compiles but then fails with processing .

I confess I am too lazy to spend an hour or so trying to figure out your code.

The piece of your description that I have quoted isn't at all clear. Perhaps you can expand on it.

For example what do you mean by

I have gotten it to work with the following code but when I use the straight regular code it works more smoothly

or

After being processed through my code the sonar is more choppy

or

I have tried to remove this command which compiles but then fails with processing

Can you produce a short sketch that illustrates the problem?

You seem to have delay() sprinkled through your code. Getting rid of all of them might help, though I haven't studied exactly what impact they have on the repetition frequency of loop().

...R

I apologize Robin, my hands type faster than I think Ha! What I meant by choppy is the servo for the sonar program pans smoothly a degree at a time with the original code but when I insert it into my multi sketch function the motion became much more jerky.
I tried removing the if serial is available command so that I could still monitor the pot value while processing is communicating with the arduino for its purposes. It doesn't seem that I can use the serial monitor while processing is accessing the board.

Anyway, I stayed up doing tutorials and I rewrote the whole sketch going a different route by using Case statements. The results were much better and the sonar works smoothly. I still can not use my serial monitor at the same time. Also, the servo stays off until case 4 is reached. It would be nice to write into the other cases to detach the servo so that it is just off when not being utilized and humming for the duration. Anyway, attached is my sketch. My next steps are figuring out those problems then adding a pan tilt controlled by mouse sketch then the face tracking sketch as well. I pasted the portion of the code regarding the sonar sweep sketch.

I've had to remove this part otherwise it says that COM3 is busy by Processing

Serial.print(potReading);
Serial.print(" Selection:");
Serial.println(potstatus);

Here is the sonar case and attached is the full sketch

case 3:

digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
digitalWrite(9, 1);
digitalWrite(10, 0);
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" /
Serial.print(potReading);
Serial.print(" Selection:");
Serial.print(potstatus);
Serial.println("Not in range");
digitalWrite(LEDPin, HIGH);
}
else {
/
Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.print(potReading);
Serial.print(" Selection:");
Serial.print(potstatus);
Serial.print(" Distance ");
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
break;

SwitchCase_1_9.ino (7.23 KB)

What are you using Processing for? Is it providing data to the Arduino sketch?

You could always write some code in Processing to receive the Serial.println() debug messages.

You still have an incredible amount of repeated text that could/should be replaced with FOR loops.

All the CASE logic would be much easier to follow if the stuff that happens was put into a separate function with a meaningful name. For example

....
case 3:
   waterTheFlowers();
   break:

case 4:
   feedThe Dog();
   break;
.....

...R

Hey Robin,
Thanks again for your guidance, I really do appreciate it. In the last code I posted I rewrote it with Case logic and I agree it was much better actually essential to switch between stacked sketches. I will look into your processing suggestion but can you point me to a link or documentation on what you are referring to with my repetition and FOR loops? I am pretty new to this so I am probably missing a major concept.

Arduro:
can you point me to a link or documentation on what you are referring to with my repetition and FOR loops?

Its so long since I learned (before the Internet) that I don't have any links.

Look at the Arduino Reference section, the examples that come with the IDE and use Google.

For example ...

for (byte n = 2; n <= 10; n++) {
   pinMode(n, OUTPUT);
}

...R

My idea was as it switches numbers to switch sketches and ultimately throw the more advanced sketches on there once I got the hang of it.

Interesting concept... you are taking Arduino code (C/C++) and turning it into BASIC... essentially. Putting all of your sketches into the main flash memory and then using/reusing variables to reduce the SRAM demands and reusing I/O pins is a creative approach but one that will simply lose steam as you move forward.

Rather, you can use a spreadsheet to track your Arduino resources and maximize the ability to run different sketches quickly, but each sketch should be compiled separately and support it own required libraries and functions. This approach allows for mental concentrations on the program at hand and not convoluted by trying to integrate the code into a nest of over woven code.

Ray