Go Down

Topic: My Array Nemesis (Read 4279 times) previous topic - next topic

dizzwold

Hi,

 With much reading and research, I still can't find what I need in an effort to get this sketch working.

 I've tried many things but none that work.

 I just don't know how get the value of i from the switches to corresponde to j the pots so that switch 1 will calibrate pot 1 etc. I also don't know as AWOL stated, to pass the value of i into calibration().

 Here's what I have currently;

Code: [Select]
/*
  Conditionals - while statement

  This example demonstrates the use of  while() statements.

  While the pushbutton is pressed, the sketch runs the calibration routine.
  The  sensor readings during the while loop define the minimum and maximum
  of expected values from the photo resistor.

  This is a variation on the calibrate example.

  The circuit:
   photo resistor connected from +5V to analog in pin 0
   10K resistor connected from ground to analog in pin 0
   LED connected from digital pin 9 to ground through 220 ohm resistor
   pushbutton attached from pin 2 to +5V
   10K resistor attached from pin 2 to ground

  created 17 Jan 2009
  modified 30 Aug 2011
  by Tom Igoe
  modified 20 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/WhileLoop

*/
// These constants won't change:
const int sensorPin[3] = {A0, A1, A2};// pin that the sensor is attached to
const int ledPin = 9;           // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin[3] = {2, 3, 4};        // pin that the button is attached to


// These variables will change:
int sensorMin = 1023;  // minimum sensor value
int sensorMax = 0;     // maximum sensor value
int sensorValue = 0;         // the sensor value


void setup()
{
  Serial.begin(9600);
  pinMode(indicatorLedPin, OUTPUT);  // set the LED pins as outputs and the switch pin as input:
  pinMode(ledPin, OUTPUT);
  for (int i = 0; i < 3; i++)
    pinMode(buttonPin[i], INPUT_PULLUP);
}

void loop()
{
  for (int i = 0; i < 3; i++)
    for (int j = 0; j = i; j++)
      //buttonPin[i++] = analogRead(sensorPin[j]);
      //for (i = j)
    {
      while (digitalRead(buttonPin[i]) == LOW)  // while the button is pressed, take calibration readings:
      {
        calibrate();
      }
      digitalWrite(indicatorLedPin, LOW);  // signal the end of the calibration period
      sensorValue = analogRead(sensorPin[j]);  // read the sensor:

      sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);  // apply the calibration to the sensor reading
      sensorValue = constrain(sensorValue, 0, 255);  // in case the sensor value is outside the range seen during calibration
      analogWrite(ledPin, sensorValue);  // fade the LED using the calibrated value:
      Serial.println(sensorValue);
      delay(1000);
    }
}

void calibrate()
{
  digitalWrite(indicatorLedPin, HIGH);  // turn on the indicator LED to indicate that calibration is happening:
  // read the sensor:
  for (int j = 0; j < 3; j++)
  {
    sensorValue = analogRead(sensorPin[j]);
    if (sensorValue > sensorMax)  // record the maximum sensor value
    {
      sensorMax = sensorValue;
    }
    if (sensorValue < sensorMin)  // record the minimum sensor value
    {
      sensorMin = sensorValue;
    }
  }
}



 Maybe what I have in the first part of void loop, the for i & for j should be in the void calibration, or something similar? Most of what I've tried and failed with, I've now deleted as it just made the sketch unbearable to read, but I've left a couple in at the beginning of void loop for example.

 I'd be very grateful for some advice and direction, as I'm a novice 'clearly', and going around in circles.

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

dizzwold

Hi,

 After more reading and research, I think I may have found what I need.

 As I'm using 3 switches to select the calibration of 3 pots, instead of using;

Code: [Select]
for (int i = 0; i < 3; i++)
    for (int j = 0; j = i; j++)


 Should I be using;

Code: [Select]
for (int index = 0;index < 3; index++)

 Will this 'link', the 2 arrays so switch 1 = pot 1, switch 2 = pot 2 and switch 3 = pot 3?

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

AWOL

Code: [Select]
for (int j = 0; j = i; j++)Why?

dizzwold

Hi,

Quote
for (int j = 0; j = i; j++)

Why?
Because I don't know how to get the 2 arrays to work together.

 So I was trying and experimenting with many different things to 'link i with j', this was one of them.

 From a coding novices point of view 'j = i', makes some sense. It maybe laughable for wiser experienced poeple. Some of things I've tried would possibly have you rolling around on the floor.

 I then stumbled on using 'index', but there is very little writen about using it with arrays.

 Will using 'index', instead of 'i & j', 'link', the 2 arrays the way I wish?

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

UKHeliBob

Quote
Will using 'index', instead of 'i & j', 'link', the 2 arrays the way I wish?
It doesn't make any difference what name you give the variable used to access the elements of the array.  Generically the variable would be referred to as the index to the array.  As long as the number of elements in two arrays is the same you can use the same index variable to access both.

So, if you want to read analogue values from 3 pins and save the values in an array you could do something like this.
Code: [Select]

const byte analogPins[] = {A0, A1, A2};
int analogValues[3];

void setup()
{
  for (int someVariable = 0; someVariable < 3; someVariable++)
  {
    analogValues[someVariable] = analogRead(analogPins[someVariable]);
  }
}

void loop()
{
}

I know that this is not exactly what you want to do, but could you apply this idea to your calibration ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

AWOL

Code: [Select]
for (int j = 0; j = i; j++)
You have an outer loop that starts with i == zero.
So, j gets assigned zero, then you assignment the value of i to j, and test the outcome.
The outcome is zero hence false, and so the body of the second loop does not get. executed.
i is incremented (value now one), j gets assigned the value zero, then the value of i.
The outcome is non-zero, so true, and the body of the second loop is executed.
And so on.

I don't think that's what you intended.

dizzwold

Hi,

 So do I need 2 variables, 1 for each array, or as I want the arrays to work together do I just need the 1 variable?

 I have i for the switches and j for the pots, should I just use i for both?

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

dizzwold

Hi,

 Ok, I've gone back to basics and wrote the following, to find 2 arrays with 1 variable is what I need;

Code: [Select]
int inputPins[] = {2, 3, 4, 5};
int ledPins[] = {6, 7, 8, 9};

void setup()
{
  for (int i = 0; i < 4; i++)
{
  pinMode (ledPins[i], OUTPUT);
  pinMode (inputPins[i], INPUT_PULLUP);
  digitalWrite (inputPins[i], HIGH);
}
}

 void loop()
 {
  for (int i = 0; i < 4; i++)
  {
    int val = digitalRead(inputPins[i]);
    if (val == HIGH)
    {
      digitalWrite (ledPins[i], HIGH);
    }
    else
    {
      digitalWrite (ledPins[i], LOW);
    }
  }
 }


 Surly it could have been less painfull than that, for someone to say "2 arrays, 1 variable is needed". No disrespect intended as it may have been for the entertainment value. Don't clap, now I have a grasp of this, just throw money. LOL.

 Thank you all for your input and patients. I'll crawl back in to my novice cave and implement what I've learnt.

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

UKHeliBob

Quote
Surly it could have been less painfull than that, for someone to say "2 arrays, 1 variable is needed".
As in reply #34, perhaps
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

dizzwold

Hi,

 
Quote
As in reply #34, perhaps
Pretty much so, yes.

 Again thank you everyone for your input and advice.

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

dizzwold

Hi,

 I don't think I'm quite over my array Nemesis yet.

 I've 'what I think I've done', is intergrated the back to basics example with the calibration sketch I had.

 I can't be sure this is working correctly as it is very hard to read the serial monitor prints with how it is currently.

 So my first question is;

 Am I printing the output of the pwm after a calibration or am I still reading the input of the pot mapped to a pwm?

 As I'm able to read the 3 values, albeit this could be, the input pots or the calibrated output pwm. Is it possible to make the serial monitor print the readings, in a more readable fasion 'like the commented-out serial prints I have'. Although these commented-out serial prints, print in a nice format, they don't print the info I wish for.

Code: [Select]
/*
  Conditionals - while statement

  This example demonstrates the use of  while() statements.

  While the pushbutton is pressed, the sketch runs the calibration routine.
  The  sensor readings during the while loop define the minimum and maximum
  of expected values from the photo resistor.

  This is a variation on the calibrate example.

  The circuit:
   photo resistor connected from +5V to analog in pin 0
   10K resistor connected from ground to analog in pin 0
   LED connected from digital pin 9 to ground through 220 ohm resistor
   pushbutton attached from pin 2 to +5V
   10K resistor attached from pin 2 to ground

  created 17 Jan 2009
  modified 30 Aug 2011
  by Tom Igoe
  modified 20 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/WhileLoop

*/
// These constants won't change:
const int sensorPin[3] = {A0, A1, A2};// pin that the sensor is attached to
const int ledPin[3] = {9, 10, 11};           // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin[3] = {2, 3, 4};        // pin that the button is attached to
const int ledPins[3] = {6, 7, 8};
//const int ledPin = 9;

// These variables will change:
int sensorMin = 1023;  // minimum sensor value
int sensorMax = 0;     // maximum sensor value
int sensorValue = 0;         // the sensor value


void setup()
{
  Serial.begin(9600);
  pinMode(indicatorLedPin, OUTPUT);  // set the LED pins as outputs and the switch pin as input:
  //pinMode(ledPin, OUTPUT);
  for (int i = 0; i < 3; i++)
  {
    pinMode(buttonPin[i], INPUT_PULLUP);
    pinMode(ledPins[i], OUTPUT);
    pinMode(sensorPin[i], INPUT);
    digitalWrite(buttonPin[i], HIGH);
   
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop()
{
  for (int i = 0; i < 3; i++)
 
  {
    int val = digitalRead(buttonPin[i]);
    if (val == LOW)
    {
      digitalWrite (ledPins[i], HIGH);
    }
    else
    {
      digitalWrite (ledPins[i], LOW);
    }

    while (digitalRead(buttonPin[i]) == LOW)  // while the button is pressed, take calibration readings:
    {
      calibrate();
    }

    digitalWrite(indicatorLedPin, LOW);  // signal the end of the calibration period
    sensorValue = analogRead(sensorPin[i]);  // read the sensor:
    sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);  // apply the calibration to the sensor reading
    sensorValue = constrain(sensorValue, 0, 255);  // in case the sensor value is outside the range seen during calibration
   
    analogWrite(ledPin[i], sensorValue);  // fade the LED using the calibrated value:
   
    //Serial.print("pot 1 = ");
    Serial.println(sensorValue);
    //Serial.print("\t");
    /*Serial.print("pot 2 = ");
    Serial.print(ledPin[1]);
    Serial.print("\t");
    Serial.print("pot 3 = ");
    Serial.println(ledPin[2]);*/
  }
}

void calibrate()
{
  digitalWrite(indicatorLedPin, HIGH);  // turn on the indicator LED to indicate that calibration is happening:
  // read the sensor:
  for (int i = 0; i < 3; i++)
  {
    sensorValue = analogRead(sensorPin[i]);
    if (sensorValue > sensorMax)  // record the maximum sensor value
    {
      sensorMax = sensorValue;
    }
    if (sensorValue < sensorMin)  // record the minimum sensor value
    {
      sensorMin = sensorValue;
    }
  }
}



 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

AWOL

Code: [Select]
void calibrate()
{
  digitalWrite(indicatorLedPin, HIGH);  // turn on the indicator LED to indicate that calibration is happening:
  // read the sensor:
  for (int i = 0; i < 3; i++)
  {
    sensorValue = analogRead(sensorPin[i]);
    if (sensorValue > sensorMax)  // record the maximum sensor value
    {
      sensorMax = sensorValue;
    }
    if (sensorValue < sensorMin)  // record the minimum sensor value
    {
      sensorMin = sensorValue;
    }
  }
}

Maybe I'm not understanding something here.
Please humour me, and assume I'm not.
Can you explain to me what the relationship between the maximum value read from the pot on pin A0 has with the maximum value read from the pot on pin A1?

dizzwold

Hi,

 Are you suggesting I shouldn't have a variable in void calibrate?

 Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

AWOL

The only variable you have in the calibrate function is "i"- all others are globals
I don't see your response in any way addresses my question.

dizzwold

Hi,

 Sorry, yes "i" is the variable. Sensor value, sensor max and sensor min are the globals.

 What I should have stated was the for loop and variable.

Dizzwold.
I'm not a student or a lecturer. I'm a hobbyist.

Go Up