0
Offline
Full Member
Karma: 0
Posts: 209
Office meets Adruino
|
 |
« on: September 16, 2012, 12:26:56 pm » |
An array index usually starts with a Is there any way to start an array with the first cell being [1] instead of
|
|
|
|
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Online
God Member
Karma: 21
Posts: 707
|
 |
« Reply #1 on: September 16, 2012, 12:35:37 pm » |
Other than making the array length 1 greater than actually needed? No!
|
|
|
|
|
Logged
|
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 988
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #2 on: September 16, 2012, 12:47:02 pm » |
You can (sort of), but why would you want to? Example: byte array[10]; byte index = 1; //first index is 1.
something = array[index - 1]; //get the first element Just for reference (useful for Serial control), you can also use ascii - assuming in this example that the array has <= 10 elements: Example: byte array[10]; byte index = '0'; //first element.
something = array[index - '0']; //get the element by converting ascii to decimal
|
|
|
|
« Last Edit: September 16, 2012, 12:49:19 pm by Tom Carpenter »
|
Logged
|
~Tom~
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 209
Office meets Adruino
|
 |
« Reply #3 on: September 16, 2012, 01:23:26 pm » |
I am just going to define the array with 5 elements instead of 4 and use the extra element for storing information when no leds are on.
I am playing with an rgb color sensor and I was going to store the values for each color when reflected with light from an RGB LED in the array.
1 was going to represent the red led on 2.. blue led on 3... white led on 4... green led on
it never occured to me that I may want the color values with no leds on. I will just use the fifth element to store the color information with no leds on or the ambient light in the room.
I will just add for 0... no leds are on
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 209
Office meets Adruino
|
 |
« Reply #4 on: September 16, 2012, 01:23:54 pm » |
Thanks for the help anyway 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 15
Posts: 1009
Arduino rocks
|
 |
« Reply #5 on: September 16, 2012, 01:30:23 pm » |
void array(size_t i) { static byte ary[4]; return ary[i-1]; }
If you use that function you can just replace the [ ] with ( ) and use it as a 1-indexed array.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6406
-
|
 |
« Reply #6 on: September 16, 2012, 01:47:04 pm » |
If you're really determined to do that you could declare a pointer and set it to the address of the [-1] element of your array and then dereference the pointer as if it was an array. Accessed via the pointer, all the element indices would be one higher than normal (it would go from 1 .. n rather than 0 .. n-1). I think it's a spectacularly bad idea, but it would achieve the effect you asked for.
Perhaps if you explain what you hope to achieve through this we could suggest a way to achieve it that isn't so nasty.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 209
Office meets Adruino
|
 |
« Reply #7 on: September 16, 2012, 01:56:20 pm » |
If you're really determined to do that you could declare a pointer and set it to the address of the [-1] element of your array and then dereference the pointer as if it was an array. Accessed via the pointer, all the element indices would be one higher than normal (it would go from 1 .. n rather than 0 .. n-1). I think it's a spectacularly bad idea, but it would achieve the effect you asked for.
Perhaps if you explain what you hope to achieve through this we could suggest a way to achieve it that isn't so nasty.
When I started my sketch I defined constants to represent colors the sensor I am using could represent. 1 was going to be red, 2 was going to be blue, 3 was going to be white and 4 was going to represent green. As I have written the program I started to want to save the color values in a variable and the sensible way was to declare an array of color values. I had already used the above mothod extensively in my scetch and I did not allow for a zero value to represent a color. I already have a bunch coded and was trying to find an easy way out is all. I think I found a better idea.
|
|
|
|
« Last Edit: September 16, 2012, 01:59:03 pm by copiertalk »
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 209
Office meets Adruino
|
 |
« Reply #8 on: September 16, 2012, 02:02:34 pm » |
my sketch /* TCS3200 / 3210 COLOR sensor from TAOS. Written By Michael King Copiertalk.com Creative commons share alike. The sensor provides a pulse depending on the value of the color filter , frequency scaling desired as well as the output enable pin being low. We are going to use an interupt on pin 18 to count the number of pulses in a given amount of time. We will use the values returned to try and and tell the color the sensor is "seeing" at the moment. We will use these pin settings in our program to fine tume the sensor to our needs. s0 and s1 set the Frequency scaling s0 = low / s1 = low is power down mode for the sensor s0 = low / s1 = hihg 2% scale s0 = high / s1 = low 20% scale s0 = high / s1 = high 100% scale s2 and s3 set the color filter of the sensor s2 = low / s3 = low red filter s2 = low / s3 = high blue filter s2 = high / s3 = low Clear or no filter s2 = high / s3 = high green filter Some breakout boards use an LED to illuminate the object and we will use pin 13 to control this led on the breakout board that I am using. */
// include the library code: #include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins LiquidCrystal lcd(53, 49, 41, 43, 45, 47);
unsigned long colormillis = millis(); // millis for color function
// **************************************************** // s0 and s1 set the Frequency scaling // s0 = low / s1 = low is power down mode for the sensor // s0 = low / s1 = hihg 2% scale // s0 = high / s1 = low 20% scale // s0 = high / s1 = high 100% scale // s2 and s3 set the color filter of the sensor // s2 = low / s3 = low red filter // s2 = low / s3 = high blue filter // s2 = high / s3 = low Clear or no filter // s2 = high / s3 = high green filter // **************************************************** const int s0 = 30; // Pins for our color sensor filters const int s1 = 31; // and our scaling. You can use any digital pins. const int s2 = 32; const int s3 = 33; const int oe = 35;
const int ledpin = 13; // pin to control sensor led on /off const int redrgbledpin = 5; const int bluergbledpin = 7; const int greenrgbledpin = 6;
const int ambient = 0; // constant vales to represent color const int red = 1; const int blue = 2; const int nocolor = 3; const int green = 4;
const unsigned long waittime = 25; // time to take sensor reading
// These variables are to store the pulse count returned by // our interupt service routine. we assign them with the function call unsigned long colorpulsecount[5];
volatile unsigned long sensorpulse = 0; // This value will be changed // by the service routine and // then taken and asigned to another // variable in our program depending on the // color filter in use
// **************************************************** // Setup function // This is executed once at the start of our program // It sets up our pins for our sensor, our lcd and defines our // initial values of pin modes and outut levels. // **************************************************** void setup() { sensorpulse = 0; // I want to make sure this value starts at zero
pinMode (redrgbledpin,OUTPUT); // Set leds pinMode (bluergbledpin,OUTPUT); // pinmodes as pinMode (greenrgbledpin,OUTPUT); // OUTPUTS pinMode(ledpin,OUTPUT);
pinMode (s0, OUTPUT); // set up our sensor pinMode (s1, OUTPUT); // pins to control scaling
pinMode (s2, OUTPUT); // set up our sensor pins pinMode (s3, OUTPUT); // to control color filter
pinMode (oe,OUTPUT); // output enable pin for our color sensor digitalWrite(oe,LOW); // low is enable / high is disabled
digitalWrite(s0,HIGH); // Set scaling here. digitalWrite(s1,LOW); // s0 high and s1 low is 20% scale
digitalWrite(ledpin,HIGH); // Pin to control LED on sensor if needed digitalWrite(redrgbledpin,HIGH); digitalWrite(bluergbledpin,HIGH); digitalWrite(greenrgbledpin,HIGH);
attachInterrupt(5, sensorisr, RISING); // Asign our interupt to the function and set to rising
lcd.begin(16, 2); // set up the LCD's number of columns and rows: Serial.begin(9600); // start serial scrnsetup(); // write static portion of our lcd. }// end setup
// **************************************************** // This is our interupt service routine. // Any time an interupt happens on the specified pin // we increment the value of sensorpule and leave. // Thats all we do here.. // **************************************************** void sensorisr () { sensorpulse ++; // Add 1 to our pulse count. } // end sensorisr
// **************************************************** // Get the color values from our sensor // 0 = ambient color // 1 = red filter // 2 = blue filter // 3 = no color filter // 4 = green filter // **************************************************** void getcolorval() // Get the sensor values; { ledcontrol (red); delay (250); // these will not stay here. I just want to watch the leds colorpulsecount[red] = getcolor (red); ledcontrol (blue); delay (250); colorpulsecount[blue] = getcolor (blue); ledcontrol (green); delay (250); colorpulsecount[nocolor] = getcolor (nocolor); ledcontrol (nocolor); delay(250); colorpulsecount[green] = getcolor (green); ledcontrol (ambient); delay(250); colorpulsecount[ambient] = getcolor (ambient); } // end getcolorval
// **************************************************** // Our Main loop over and over again // **************************************************** void loop() { getcolorval(); displaylcd(); dispserial(); delay(250); // So we can read the display }// end loop
|
|
|
|
« Last Edit: September 16, 2012, 03:02:18 pm by copiertalk »
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 209
Office meets Adruino
|
 |
« Reply #9 on: September 16, 2012, 02:03:30 pm » |
9000 characters apparently // **************************************************** // This function sets pin modes // Depending on the color filter defined below. // 1 = red filter // 2 = blue filter // 3 = no color filter // 4 = green filter // **************************************************** void colormode ( int color) { if (color == red) { digitalWrite ( s2, LOW); // red digitalWrite ( s3, LOW); } if (color == blue) { digitalWrite ( s2, LOW); // BLue filter digitalWrite ( s3, HIGH); } if (color == nocolor) { digitalWrite ( s2, HIGH); // clear filter or no color digitalWrite ( s3, LOW); } if (color == green) { digitalWrite ( s2, HIGH); // greeen digitalWrite ( s3, HIGH); } } // colormode
// **************************************************** // This function returns a value // Depending on the color filter defined below. // 0 ambient color // 1 = red filter // 2 = blue filter // 3 = no color filter // 4 = green filter // **************************************************** unsigned long getcolor (int color) { unsigned long colorpulse = 0; // pulse count to return sensorpulse = 0; // make sure sensor pulse starts at zero. colormillis = millis(); // record current millis if ((color >=1) && (color <= 4)) { colormode (color); } else { colorpulse = 0; // return zero if we ask for calor value that is not 1,2,3,4 } digitalWrite(oe,LOW); // turn sensor on do { // do nothing until wait time expires } while ((millis() - colormillis) < waittime); digitalWrite (oe, HIGH); // turn sensor off. colorpulse = sensorpulse; sensorpulse = 0; // reset value of sensor pulse return colorpulse; } // end getcolor
// **************************************************** // set the static portion of our display up // This does not change so there is no reason to display it over and over // **************************************************** void scrnsetup() { lcd.setCursor(0,0); lcd.print("R:"); lcd.setCursor(7,0); lcd.print("B:"); lcd.setCursor(0,1); lcd.print("G:"); lcd.setCursor(7,1); lcd.print("N:"); } // end scrnsetup
// **************************************************** // Display our non static values to the lcd display // // // **************************************************** void displaylcd() { lcd.setCursor(3,0); lcd.print(" "); lcd.setCursor(3,0); lcd.print(colorpulsecount[red]); lcd.setCursor ( 10,0); lcd.print(" "); lcd.setCursor ( 10,0); lcd.print(colorpulsecount[blue]); lcd.setCursor(3,1); lcd.print(" "); lcd.setCursor(3,1); lcd.print(colorpulsecount[green]); lcd.setCursor ( 10,1); lcd.print(" "); lcd.setCursor ( 10,1); lcd.print(colorpulsecount[nocolor]); } // end displaylcd
//************************************************ // Here we just display the sensor value to serial minitor // this procedure echos our sensor pulse count // to the seial monitor. //************************************************ void dispserial() { Serial.print ( "red :"); Serial.println (colorpulsecount[red]); Serial.print ( "Blue :"); Serial.println (colorpulsecount[blue]); Serial.print ( "CL or NC :"); Serial.println (colorpulsecount[nocolor]); Serial.print ( "Green :"); Serial.println (colorpulsecount[green]); Serial.println ("-----------"); } // end dispserial
// **************************************************** // This function toggles LEDs on depending on the value it is sent. // 0 = ambient // 1 = red // 2 = blue // 3 = no color // 4 = green // **************************************************** void ledcontrol ( int color) { if (color == 0) // turn all leds off { digitalWrite(ledpin,HIGH); digitalWrite(redrgbledpin,HIGH); digitalWrite(bluergbledpin,HIGH); digitalWrite(greenrgbledpin,HIGH); } if (color == 1) // turn red led on { digitalWrite(ledpin,HIGH); digitalWrite(redrgbledpin,LOW); digitalWrite(bluergbledpin,HIGH); digitalWrite(greenrgbledpin,HIGH); } if (color == 2) // turn blue led on { digitalWrite(ledpin,HIGH); digitalWrite(redrgbledpin,HIGH); digitalWrite(bluergbledpin,LOW); digitalWrite(greenrgbledpin,HIGH); } if (color == 3) // turn white led on { digitalWrite(ledpin,LOW); digitalWrite(redrgbledpin,HIGH); digitalWrite(bluergbledpin,HIGH); digitalWrite(greenrgbledpin,HIGH); } if (color == 4) // turn green led on { digitalWrite(ledpin,HIGH); digitalWrite(redrgbledpin,HIGH); digitalWrite(bluergbledpin,HIGH); digitalWrite(greenrgbledpin,LOW); } } // end ledcontrol
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 3
|
 |
« Reply #10 on: January 09, 2013, 06:24:24 pm » |
copiertalk, can you send me the sketch that you are using? I am also working with that sensor.
Thanks!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #11 on: January 09, 2013, 06:26:49 pm » |
Is there any way to start an array with the first cell being [1] instead of Yes: point the pointer to the one unit before the start of the array. so new_array will be mapped to old_array[i-1].
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 15
Posts: 1009
Arduino rocks
|
 |
« Reply #12 on: January 10, 2013, 12:24:47 am » |
Is there any way to start an array with the first cell being [1] instead of Yes: point the pointer to the one unit before the start of the array. so new_array will be mapped to old_array[i-1].
Why did you decide to post a duplicate of the one posted by PeterH on september 16? Especially since OP is long gone.
|
|
|
|
|
Logged
|
|
|
|
|
|