Go Down

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

AWOL

I'm confused - what does that tell you about calibration, and whether or not it worked correctly?

CrossRoads

sensorValue will be a number from 0 to 1023.
If you multiply that number by 5.0/1024 (or 0.00488) you will get a result that should be very close to a reading of the same input that you would measure with a multimeter, assuming you also measure 5V on the Aref pin.

Try that, see if they agree.
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,

 I see what your saying now. I'm only reading the slider input and not the calibration done in the Arduino.

 Is there a way to read the apparently calibrated result?

 Can it be done by creating an o/p and reading from that?

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

CrossRoads

Look at the code now vs the code you originally posted.
The map() function is not being used,
the limiting to the high and low is not being done.

You need to put that stuff back in.
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.

UKHeliBob

Quote
Can it be done by creating an o/p and reading from that?
You already have the max and min values in sensorMax and sensorMin.  You could print those.

Note that because you don't reset the values, then running calibrate() a second or subsequent time may not actually change the values.
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,

 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;

Code: [Select]
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'm not a student or a lecturer. I'm a hobbyist.

AWOL

Quote
I get an error in the serial monitor
The serial monitor has no concept of "error"

dizzwold

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;

Code: [Select]
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.
I'm not a student or a lecturer. I'm a hobbyist.

AWOL

Is there something you're not telling us?

dizzwold

Hi,

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

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
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.
I'm not a student or a lecturer. I'm a hobbyist.

AWOL

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

dizzwold

Hi,

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

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

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

PaulMurrayCbr

#27
May 19, 2017, 05:53 am Last Edit: May 19, 2017, 05:55 am by PaulMurrayCbr
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.
http://paulmurraycbr.github.io/ArduinoTheOOWay.html

dizzwold

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.
I'm not a student or a lecturer. I'm a hobbyist.

dizzwold

Hi,

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

Code: [Select]
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.
I'm not a student or a lecturer. I'm a hobbyist.

Go Up