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