New Orleans
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« on: April 22, 2012, 02:54:06 pm » |
Hey Forum, I'm working on a sketch that uses analog input from flex sensors to turn on LEDs. I keep getting an "error: using obsolete binding" message for my void loop functions however I'm pretty sure I need them in order for the sketch to operate the way I've intended. This is the sketch: //Flex Sensor Pin (flexPin) //the analog pin the Flex Sensor is connected to int flexPin1 = 0; int flexPin2 = 1; int flexPin3 = 2; int flexPin4 = 3; int flexPin5 = 4;
void setup() { for (int i=2; i<9; i++){ pinMode(i, OUTPUT); for (int i2=9; i2<16; i2++){ pinMode(i2, OUTPUT); for (int i3=16; i2<23; i3++){ pinMode(i3, OUTPUT); for (int i4=23; i2<30; i4++){ pinMode(i4, OUTPUT); for (int i5=30; i2<37; i5++){ pinMode(i5, OUTPUT); } } } } } }
void loop(){
for (int i=2; i<9; i++) { digitalWrite(i, LOW); } for (int i2=9; i<16; i2++) { digitalWrite(i2, LOW); } for (int i3=16; i<23; i3++) { digitalWrite(i3, LOW); } for (int i4=23; i<30; i4++) { digitalWrite(i4, LOW); } for (int i5=30; i<37; i5++) { digitalWrite(i5, LOW); }
/* Read the flex Level Adjust the value 130 to 275 to span 28 to 36 The values 130 and 275 may need to be widened to suit the minimum and maximum flex levels being read by the Analog pin */ int lightLevel = map(analogRead(flexPin1), 175, 250, 2, 8); int lightLevel2 = map(analogRead(flexPin2), 175, 250, 9, 15); int lightLevel3 = map(analogRead(flexPin3), 175, 250, 16, 22); int lightLevel4 = map(analogRead(flexPin4), 175, 250, 23, 29); int lightLevel5 = map(analogRead(flexPin5), 175, 250, 30, 36);
// Make sure the value is between 4 and 13, to turn on an LED int ledON = constrain(lightLevel, 2, 8); int ledON2 = constrain(lightLevel2, 9, 15); int ledON3 = constrain(lightLevel3, 16, 22); int ledON4 = constrain(lightLevel4, 23, 29); int ledON5 = constrain(lightLevel5, 30, 36);
//Blink the LED on blink(ledON, 10,1); blink(ledON2, 10,1); blink(ledON3, 10,1); blink(ledON4, 10,1); blink(ledON5, 10,1); }
// The blink function - used to turn the LEDs on and off void blink(int LEDPin, int onTime, int offTime){ // Turn the LED on digitalWrite(LEDPin, HIGH);
// Delay so that you can see the LED go On. delay(onTime);
// Turn the LED Off digitalWrite(LEDPin, LOW);
// Delay so that you can see the LED go On. delay(offTime); }
// The blink function - used to turn the LEDs on and off void blink2(int LEDPin2, int onTime2, int offTime2){ // Turn the LED on digitalWrite(LEDPin2, HIGH);
// Delay so that you can see the LED go On. delay(onTime2);
// Turn the LED Off digitalWrite(LEDPin2, LOW);
// Delay so that you can see the LED go On. delay(offTime2); }
// The blink function - used to turn the LEDs on and off void blink3(int LEDPin3, int onTime3, int offTime3){ // Turn the LED on digitalWrite(LEDPin3, HIGH);
// Delay so that you can see the LED go On. delay(onTime3);
// Turn the LED Off digitalWrite(LEDPin3, LOW);
// Delay so that you can see the LED go On. delay(offTime3); }
// The blink function - used to turn the LEDs on and off void blink4(int LEDPin4, int onTime4, int offTime4){ // Turn the LED on digitalWrite(LEDPin4, HIGH);
// Delay so that you can see the LED go On. delay(onTime4);
// Turn the LED Off digitalWrite(LEDPin4, LOW);
// Delay so that you can see the LED go On. delay(offTime4); }
// The blink function - used to turn the LEDs on and off void blink5(int LEDPin5, int onTime5, int offTime5){ // Turn the LED on digitalWrite(LEDPin5, HIGH);
// Delay so that you can see the LED go On. delay(onTime5);
// Turn the LED Off digitalWrite(LEDPin5, LOW);
// Delay so that you can see the LED go On. delay(offTime5); } Additionally, I need to make a revision to control this entire loop with momentary push button similar to this: http://www.sparkfun.com/products/9338I have found coding for simple switches but I am confused about digitally writing values. I am working to combine the sketch above with the following sketch: /* Button Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 2. The circuit: * LED attached from pin 13 to ground * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 from ground * Note: on most Arduinos there is already an LED on the board attached to pin 13. created 2005 by DojoDave <http://www.0j0.org> modified 30 Aug 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button */
// constants won't change. They're used here to // set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin
// variables will change: int buttonState = 0; // variable for reading the pushbutton status
void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }
void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); Any assistance is greatly appreciated. Thanks! Moderator edit: Subject line amended. (Nick Gammon)
|
|
|
|
« Last Edit: April 22, 2012, 08:33:27 pm by Nick Gammon »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: April 22, 2012, 02:58:28 pm » |
my void loop functions You cannot have more than one loop function. The second sketch is incomplete. What's the emergency?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Gosport, UK
Offline
Faraday Member
Karma: 19
Posts: 3118
|
 |
« Reply #2 on: April 22, 2012, 03:00:26 pm » |
for (int i2=9; i<16; i2++) Your conditional i should be i2.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: April 22, 2012, 03:04:41 pm » |
Why have you even got i2? There's no reason not to reuse i
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
New Orleans
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #4 on: April 22, 2012, 03:08:01 pm » |
You cannot have more than one loop function. Your conditional i should be i2. You're both correct. Any thoughts on the using obsolete binding at 'i' " error? I fixed the previous mistake but I still receive the error. Revised Sketch: //Flex Sensor Pin (flexPin) //the analog pin the Flex Sensor is connected to int flexPin1 = 0; int flexPin2 = 1; int flexPin3 = 2; int flexPin4 = 3; int flexPin5 = 4;
void setup() { for (int i=2; i<9; i++){ pinMode(i, OUTPUT); for (int i2=9; i2<16; i2++){ pinMode(i2, OUTPUT); for (int i3=16; i2<23; i3++){ pinMode(i3, OUTPUT); for (int i4=23; i2<30; i4++){ pinMode(i4, OUTPUT); for (int i5=30; i2<37; i5++){ pinMode(i5, OUTPUT); } } } } } }
void loop(){
for (int i=2; i<9; i++) { digitalWrite(i, LOW); } for (int i2=9; i2<16; i2++) { digitalWrite(i2, LOW); } for (int i3=16; i<23; i3++) { digitalWrite(i3, LOW); } for (int i4=23; i<30; i4++) { digitalWrite(i4, LOW); } for (int i5=30; i<37; i5++) { digitalWrite(i5, LOW); }
/* Read the flex Level Adjust the value 130 to 275 to span 28 to 36 The values 130 and 275 may need to be widened to suit the minimum and maximum flex levels being read by the Analog pin */ int lightLevel = map(analogRead(flexPin1), 175, 250, 2, 8); int lightLevel2 = map(analogRead(flexPin2), 175, 250, 9, 15); int lightLevel3 = map(analogRead(flexPin3), 175, 250, 16, 22); int lightLevel4 = map(analogRead(flexPin4), 175, 250, 23, 29); int lightLevel5 = map(analogRead(flexPin5), 175, 250, 30, 36);
// Make sure the value is between 4 and 13, to turn on an LED int ledON = constrain(lightLevel, 2, 8); int ledON2 = constrain(lightLevel2, 9, 15); int ledON3 = constrain(lightLevel3, 16, 22); int ledON4 = constrain(lightLevel4, 23, 29); int ledON5 = constrain(lightLevel5, 30, 36);
//Blink the LED on blink(ledON, 10,1); blink(ledON2, 10,1); blink(ledON3, 10,1); blink(ledON4, 10,1); blink(ledON5, 10,1); }
// The blink function - used to turn the LEDs on and off void blink(int LEDPin, int onTime, int offTime){ // Turn the LED on digitalWrite(LEDPin, HIGH);
// Delay so that you can see the LED go On. delay(onTime);
// Turn the LED Off digitalWrite(LEDPin, LOW);
// Delay so that you can see the LED go On. delay(offTime); }
// The blink function - used to turn the LEDs on and off void blink2(int LEDPin2, int onTime2, int offTime2){ // Turn the LED on digitalWrite(LEDPin2, HIGH);
// Delay so that you can see the LED go On. delay(onTime2);
// Turn the LED Off digitalWrite(LEDPin2, LOW);
// Delay so that you can see the LED go On. delay(offTime2); }
// The blink function - used to turn the LEDs on and off void blink3(int LEDPin3, int onTime3, int offTime3){ // Turn the LED on digitalWrite(LEDPin3, HIGH);
// Delay so that you can see the LED go On. delay(onTime3);
// Turn the LED Off digitalWrite(LEDPin3, LOW);
// Delay so that you can see the LED go On. delay(offTime3); }
// The blink function - used to turn the LEDs on and off void blink4(int LEDPin4, int onTime4, int offTime4){ // Turn the LED on digitalWrite(LEDPin4, HIGH);
// Delay so that you can see the LED go On. delay(onTime4);
// Turn the LED Off digitalWrite(LEDPin4, LOW);
// Delay so that you can see the LED go On. delay(offTime4); }
// The blink function - used to turn the LEDs on and off void blink5(int LEDPin5, int onTime5, int offTime5){ // Turn the LED on digitalWrite(LEDPin5, HIGH);
// Delay so that you can see the LED go On. delay(onTime5);
// Turn the LED Off digitalWrite(LEDPin5, LOW);
// Delay so that you can see the LED go On. delay(offTime5); } I'm attempting to connect and test a project scheduled to be completed tomorrow but there were power supply issues so the setup needed to be switched to battery power and utilize an on/off switch. If successful, it should look very interesting. Thanks for the help! Keep it coming!
|
|
|
|
|
Logged
|
|
|
|
|
Gosport, UK
Offline
Faraday Member
Karma: 19
Posts: 3118
|
 |
« Reply #5 on: April 22, 2012, 03:09:56 pm » |
You have to do all of them, i3, i4 etc.
|
|
|
|
|
Logged
|
|
|
|
|
New Orleans
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #6 on: April 22, 2012, 03:12:06 pm » |
Why have you even got i2? There's no reason not to reuse i I'm using i2, i3, etc... because each variable is receiving different analog input and driving different pins. Is it possible to reuse i and still maintain separate output? for (int i=2; i<9; i++) { digitalWrite(i, LOW); } for (int i2=9; i2<16; i2++) { digitalWrite(i2, LOW); } for (int i3=16; i<23; i3++) { digitalWrite(i3, LOW); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: April 22, 2012, 03:12:42 pm » |
Just get rid of i2 etc, and simply use i throughout
I still don't see the emergency
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Gosport, UK
Offline
Faraday Member
Karma: 19
Posts: 3118
|
 |
« Reply #8 on: April 22, 2012, 03:16:30 pm » |
I don't think you need separate for() loops at all. In setup() and loop(), just one for() loop to cover the whole range should do.
|
|
|
|
« Last Edit: April 22, 2012, 03:23:27 pm by dxw00d »
|
Logged
|
|
|
|
|
New Orleans
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #9 on: April 22, 2012, 03:37:53 pm » |
Just get rid of i2 etc, and simply use i throughout This worked. Thank you!!!! //Flex Sensor Pin (flexPin) //the analog pin the Flex Sensor is connected to int flexPin1 = 0; int flexPin2 = 1; int flexPin3 = 2; int flexPin4 = 3; int flexPin5 = 4;
void setup() { for (int i=2; i<9; i++){ pinMode(i, OUTPUT); for (int i=9; i<16; i++){ pinMode(i, OUTPUT); for (int i=16; i<23; i++){ pinMode(i, OUTPUT); for (int i=23; i<30; i++){ pinMode(i, OUTPUT); for (int i=30; i<37; i++){ pinMode(i, OUTPUT); } } } } } }
void loop(){
for (int i=2; i<9; i++) { digitalWrite(i, LOW); } for (int i2=9; i2<16; i2++) { digitalWrite(i2, LOW); } for (int i=16; i<23; i++) { digitalWrite(i, LOW); } for (int i=23; i<30; i++) { digitalWrite(i, LOW); } for (int i=30; i<37; i++) { digitalWrite(i, LOW); }
/* Read the flex Level Adjust the value 130 to 275 to span 28 to 36 The values 130 and 275 may need to be widened to suit the minimum and maximum flex levels being read by the Analog pin */ int lightLevel = map(analogRead(flexPin1), 175, 250, 2, 8); int lightLevel2 = map(analogRead(flexPin2), 175, 250, 9, 15); int lightLevel3 = map(analogRead(flexPin3), 175, 250, 16, 22); int lightLevel4 = map(analogRead(flexPin4), 175, 250, 23, 29); int lightLevel5 = map(analogRead(flexPin5), 175, 250, 30, 36);
// Make sure the value is between 4 and 13, to turn on an LED int ledON = constrain(lightLevel, 2, 8); int ledON2 = constrain(lightLevel2, 9, 15); int ledON3 = constrain(lightLevel3, 16, 22); int ledON4 = constrain(lightLevel4, 23, 29); int ledON5 = constrain(lightLevel5, 30, 36);
//Blink the LED on blink(ledON, 10,1); blink(ledON2, 10,1); blink(ledON3, 10,1); blink(ledON4, 10,1); blink(ledON5, 10,1); }
// The blink function - used to turn the LEDs on and off void blink(int LEDPin, int onTime, int offTime){ // Turn the LED on digitalWrite(LEDPin, HIGH);
// Delay so that you can see the LED go On. delay(onTime);
// Turn the LED Off digitalWrite(LEDPin, LOW);
// Delay so that you can see the LED go On. delay(offTime); }
// The blink function - used to turn the LEDs on and off void blink2(int LEDPin2, int onTime2, int offTime2){ // Turn the LED on digitalWrite(LEDPin2, HIGH);
// Delay so that you can see the LED go On. delay(onTime2);
// Turn the LED Off digitalWrite(LEDPin2, LOW);
// Delay so that you can see the LED go On. delay(offTime2); }
// The blink function - used to turn the LEDs on and off void blink3(int LEDPin3, int onTime3, int offTime3){ // Turn the LED on digitalWrite(LEDPin3, HIGH);
// Delay so that you can see the LED go On. delay(onTime3);
// Turn the LED Off digitalWrite(LEDPin3, LOW);
// Delay so that you can see the LED go On. delay(offTime3); }
// The blink function - used to turn the LEDs on and off void blink4(int LEDPin4, int onTime4, int offTime4){ // Turn the LED on digitalWrite(LEDPin4, HIGH);
// Delay so that you can see the LED go On. delay(onTime4);
// Turn the LED Off digitalWrite(LEDPin4, LOW);
// Delay so that you can see the LED go On. delay(offTime4); }
// The blink function - used to turn the LEDs on and off void blink5(int LEDPin5, int onTime5, int offTime5){ // Turn the LED on digitalWrite(LEDPin5, HIGH);
// Delay so that you can see the LED go On. delay(onTime5);
// Turn the LED Off digitalWrite(LEDPin5, LOW);
// Delay so that you can see the LED go On. delay(offTime5); } I still don't see the emergency Given the time I want to make sure there is a viable solution to a situation I cannot change...but I guess if you search hard enough there will always be one. However the issue still remains of using a switch to turn the entire loop on and off. This is the clearest example of the situation that I have found but it doesn't account for several LEDs and additional digitalwrite's... /* Button Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 2. The circuit: * LED attached from pin 13 to ground * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 from ground * Note: on most Arduinos there is already an LED on the board attached to pin 13. created 2005 by DojoDave <http://www.0j0.org> modified 30 Aug 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button */
// constants won't change. They're used here to // set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin
// variables will change: int buttonState = 0; // variable for reading the pushbutton status
void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }
void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } } Thanks you for your help!!
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: April 22, 2012, 03:41:29 pm » |
Which loop - there are several?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
New Orleans
Offline
Newbie
Karma: 0
Posts: 28
|
 |
« Reply #11 on: April 22, 2012, 03:52:29 pm » |
Which loop - there are several? My question exactly. Is it possible to run all the loops concurrently as one loop and use one push button to turn them all off or on?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: April 22, 2012, 03:55:51 pm » |
I'm still not sure what you're asking, but all the loops could go inside the conditional.
No, you can't have concurrency.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
UK, Southwest
Offline
Full Member
Karma: 5
Posts: 138
Arduino rocks
|
 |
« Reply #13 on: April 22, 2012, 04:15:07 pm » |
1. Your setup loops are nested. They should not be. Each setup for statement should probably be like:
int i; for (i = 9; i < 12; i++) { do stuff - the stuff inside the brackets for *this* for loop is what gets executed }
2. Look at the blink without delay example. This shows how to achieve a concurrency effect without using delay. The delay statement pauses all execution - the whole processor waits. Write down, on paper, the steps needed to if you have a watch. e.g. Light 1 is to flash in 60 secs - write down time1 now. Light 2 is to flash in 120 seconds - write down time2 now. Start loop Look at watch. Is the current time - written time1 > 60? If so, switch the light, depending on main switch. Record the time again. Look at watch. Is the current time - written time2 > 120? If so, switch the light, depending on main switch. Record the time again.
3. The fact you have an emergency something I cannot help you with.
4. We could perhaps help you if you gave a clear indication of what you are trying to do, and what currently happens. What is the main switch?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13897
Lua rocks!
|
 |
« Reply #14 on: April 22, 2012, 08:34:01 pm » |
This isn't an emergency. An emergency is when your heart stops beating. I've taken the liberty of amending the thread topic.
|
|
|
|
|
Logged
|
|
|
|
|
|