My Array Nemesis

Hi,

I've gone back to the original example format as stated, with my pullup and active low, but I wish to have an analog o/p of 0-1023 and not mapped to a pwm o/p of 0-255, which is why I got rid of the led pin, the map and constraint?

If I change the map and constraint from 0-255 to 0-1023 I get an error in the serial monitor due to;

analogWrite(ledPin, sensorValue);

All the calibration examples and what I can find on the web are all much the same and map to pwm o/p.

Dizzwold

I get an error in the serial monitor

The serial monitor has no concept of "error"

Hi,

I get an error in the serial monitor may be incorrect, but I don’t know of any other explaination for the following msg;

assertion "duty <= pPwm->PWM_CH_NUM[ul_channel].PWM_CPRD" failed: file "../source/pwmc.c", line 272, function: PWMC_SetDutyCycle
Exiting with status 1.

Sorry, but that was the best term I could think of regarding.

Dizzwold.

Is there something you're not telling us?

Hi,

Yes, sorry. For some reason, I’ve got my simpleton head on today!

/*
  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 = A0;       // 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 = 2;        // 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);
  // set the LED pins as outputs and the switch pin as input:
  pinMode(indicatorLedPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // while the button is pressed, take calibration readings:
  while (digitalRead(buttonPin) == LOW) {
    calibrate();
  }
  // signal the end of the calibration period
  digitalWrite(indicatorLedPin, LOW);

  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 1023);

  // in case the sensor value is outside the range seen during calibration
  sensorValue = constrain(sensorValue, 0, 1023);

  // fade the LED using the calibrated value:
  analogWrite(ledPin, sensorValue);
  Serial.println(sensorValue);
}

void calibrate() {
  // turn on the indicator LED to indicate that calibration is happening:
  digitalWrite(indicatorLedPin, HIGH);
  // read the sensor:
  sensorValue = analogRead(sensorPin);

  // record the maximum sensor value
  if (sensorValue > sensorMax) {
    sensorMax = sensorValue;
  }

  // record the minimum sensor value
  if (sensorValue < sensorMin) {
    sensorMin = sensorValue;
  }
}

Dizzwold.

No, I meant like "which processor are you compiling for, and with which libraries". That sort of thing.

Hi,

I'm using an Arduino Due with the WhileStatementConditional example.

The Board is "Arduino Due (Programming Port)".

Dizzwold.

I’m not sure why you need to calibrate your pots. A correctly wired-up potentiometer on an analog input will produce a value of 0 at ground and 1023 at AREF - usually 5 or 3.3v, no matter what the overall value of the pot is. The value will correspond to the posotion og the pot, unless you are accidentally using a logarithmic potentiometer.

To do this, attach one side of the pot to ground, the other side to +5v, and the wiper in the middle to your analog pin.

If you are getting weird or unstable values, I’d suspect that you have done something like attaching +5v and the wiper but not attaching ground. Analog inputs read voltage, and that’s not how voltage works.

Hi PaulMurrayCBr,

Thank you for your input. I appreciate what you are saying.

One of the reasons for ‘calibrating’ each pot individualy is as I will be using them to control my DAW ‘Logic Pro’, remotely. My DAW has a learn function, where you select the control button, knob or fader in the DAW then move one of controls on your remote and the 2 are mapped together.

With getting ‘jitter’, from the pots I have, you can’t be accurate as to which pot is mapped. Thus with the calibration idea when you press switch 1 pot 1 is isolated and the only pot that will then be mapped, and so-on with switch 2 for pot 2, switch 3 for pot 3.

So, Yes you are correct, but there is method behind what would seem odd.

Dizzwold.

Hi,

Going back to the orginal 2 Arrays, should I be considering something along the lines of;

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

Or would I be going backwards with adding a pin count?

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;

/*
  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.

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;

for (int i = 0; i < 3; i++)
    for (int j = 0; j = i; j++)

Should I be using;

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.

for (int j = 0; j = i; j++)Why?

Hi,

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.

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.

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 ?

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.

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.

Hi,

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

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.

Surly it could have been less painfull than that, for someone to say "2 arrays, 1 variable is needed".

As in reply #34, perhaps

Hi,

As in reply #34, perhaps

Pretty much so, yes.

Again thank you everyone for your input and advice.

Dizzwold.