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?
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?
Yes, clearly shows the benefit of using braces!
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.
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.
You need two arrays (or one 2D array), but the index can be the same.
I'm only getting readings from the pot on A2 and nothing else.
for (int j = 0; j < 3; j++)
sensorValue = analogRead(sensorPin[j]);
Serial.println(sensorValue);
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
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
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.
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?
/*
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
it's not calibrating correctly with 1 switch and pot,
How do you know this with the code you just posted?
Hi AWOL,
AWOL:
How do you know this with the code you just posted?
From the serial monitor;
Serial.println(sensorValue);
Is'nt that correct?
Dizzwold.
I'm confused - what does that tell you about calibration, and whether or not it worked correctly?
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.
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.
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.
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.
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.