Go Down

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

dizzwold

Hi,

 I'm back with another Array issue, and as before trying to learn from that.

 I'm a complete novice and only wrote my first Array last week with much help from KeithRB, PaulS and UKHeliBob, you are all gents!

 I'm using the WhileStatementConditional example from the IDE but wish to use it 'for testing', with 3 switches and 3 linear pots on an Arduino Due.

 I think I'm correct in saying I need to use 2 Arrays, 1 for the switches and 1 for the pots, but I could be wrong and having issues?

 I'm also unsure how to serial print the 3 pots values in a readable manner at the same time.

 With the current sketch the switches seem to be working fine, but I'm only getting readings from the pot on A2 and nothing else.

 Sorry for the state of the sketch, but I've left it 'as-is', except for my attempted input, I'll return with a cleaner version as I don't think the jumble of comments are helping me find my errors.

 As always, I would be grateful for some pointers and direction.

 As before there will possibly be laughter, tears and frustration with my mistakes.

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);
  // set the LED pins as outputs and the switch pin as input:
  pinMode(indicatorLedPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  for (int i = 0; i < 3; i++)
    pinMode(buttonPin[i], INPUT_PULLUP);
}

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

  // read the sensor:
  for (int j = 0; j < 3; j++)
    sensorValue = analogRead(sensorPin[j]);
  Serial.println(sensorValue);
  // apply the calibration to the sensor reading
  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

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

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

void calibrate() {

  // turn on the indicator LED to indicate that calibration is happening:
  digitalWrite(indicatorLedPin, HIGH);
  // read the sensor:
  for (int j = 0; j < 3; j++)
    sensorValue = analogRead(sensorPin[j]);

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

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



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

dizzwold

Hi,

 Ok, just cleaned the sketch up some.

 I think I've just found my first error.

 Everytime I call the sensor value I should use;

Code: [Select]
int sensorValue

 Would that be a good start?

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

AWOL

Quote
I'm also unsure how to serial print the 3 pots values in a readable manner at the same time
It's impossible to print three values at the same time using a single serial interface. What do you want?

AWOL

Code: [Select]
void loop() {
  for (int i = 0; i < 3; i++)
    // while the button is pressed, take calibration readings:
    while (digitalRead(buttonPin[i]) == LOW) {
      calibrate();
    }
Why call calibrate() three times?

dizzwold

Hi AWOL,

 I've clearly got this very wrong.

 I thought that would associate i 1 to 3 with j 1 to 3. i being the switches and j being the pots.

 What I wish to do is press switch 1 and calibrate pot 1, press switch 2 and calibrate pot 2, etc.

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

AWOL

You need to pass the value of i into calibrate() to use as the pot number

UKHeliBob

Code: [Select]

  for (int j = 0; j < 3; j++)
    sensorValue = analogRead(sensorPin[j]);
  Serial.println(sensorValue);

Which line(s) of code will be executed 3 times ?
Which line(s) of code will be executed once only?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

CrossRoads

Yes, clearly shows the benefit of using braces!

Code: [Select]

  for (int j = 0; j < 3; j++)
{
    sensorValue = analogRead(sensorPin[j]);
    sensorValue = analogRead(sensorPin[j]); // give source a chance to settle thru analog mux & ADC if it is a weak source
  Serial.println(sensorValue);  // prints results of the reads as they occur
}

vs the sketch code effectively:
  for (int j = 0; j < 3; j++)
{
    sensorValue = analogRead(sensorPin[j]);
}
  Serial.println(sensorValue); // prints results of the last read

vs
  for (int j = 0; j < 3; j++)
{
    Serial.println (analogRead(sensorPin[j]) ); // prints results of reads as they occur
}

Last method is probably least preferred as 3 rapid analogRead()s can return less then stellar results if the analog source is a weak current driver. May be okay tho if the serial data rate used is not super fast.
 

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

dizzwold

Hi UKHeliBob,

My thoughts are the switches should be executed 3 times where the sensor value should be read the once?

Am I barking up the wrong tree with 2 Arrays, or do I need 2 Arrays?

Thank you both for being direct.

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

AWOL

You need two arrays (or one 2D array), but the index can be the same.

UKHeliBob

Quote
I'm only getting readings from the pot on A2 and nothing else.
Code: [Select]

  for (int j = 0; j < 3; j++)
    sensorValue = analogRead(sensorPin[j]);
  Serial.println(sensorValue);

Quote
My thoughts are the switches should be executed 3 times where the sensor value should be read the once?
Each sensor pin will be read once (3 reads) but the sensor value (the last one read) will be printed once.

Try
Code: [Select]


  for (int j = 0; j < 3; j++)
    {
      sensorValue = analogRead(sensorPin[j]);
      Serial.println(sensorValue);
    }

My advice would be to always use curly brackets round the code to be executed by a for loop, even if there is only one line of code, which is not the case here
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 CrossRoads,

Been a while. Thank you for your input and I take onboard the use of the sensor value being read twice to get a better reading.

AWOL, it took me 2 days last week to write my first simple Array of 9 characters with much frustrated help, and you talk of a 2d Array! LOL, mabe next week, and thank you.

UKHeliBob, very grateful for your input 'again', thank you.

I think I'm in need of a beer and need to reflect on what I'm actually stating in the sketch and what I wish to do.

Must try harder! LOL.

Thank your for your inputs so far, your all gents to take on a novice and help.

Just caught your post UKHeliBob. Yet more schoolboy errors which CrossRoads had pointed out.

 I'm still learning, and I can appreciate it must be very frustating for you guys, but I don't ask for code. As stated, I can't thank you all enough for your help and patients.

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

dizzwold

Hi,

 I've done much research and reading last night and today to try and find what I need, and comeback empty handed and possibly more confused as much of the stuff I found talked about ConCat and Strings.

 To move away from my Array problems for the moment, with playing around with the sketch to day I noticed I could read the inputs from the 3 pots in the serial monitor, but found that when I went to calibrate any 1 of the pots it was'nt calibrating correctly.

 So I've gone back to the original example for 1 switch and 1 pot, removed some unwanted things like to pwm o/p for the led and the mapping to 0-255.

 I've found that it's not calibratring the pot. When I start I have readings from 6-1011 'not bad for some cheap pots. When i calibrate it I the get readings from 34-1023, then calibrate again and get 21-1022?

 Any thoughts as why this is happening?

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 = A0;       // pin that the sensor is attached to          // 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(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);
  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;
  }
}



 If it's not calibrating correctly with 1 switch and pot, I don't stand a chance with my 3 switch, pot and Array attempt because I'll always think I've still got my sketch wrong.

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

AWOL

Quote
it's not calibrating correctly with 1 switch and pot,
How do you know this with the code you just posted?

dizzwold

Hi AWOL,

How do you know this with the code you just posted?
From the serial monitor;

Code: [Select]
Serial.println(sensorValue);

Is'nt that correct?

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

Go Up