Code Club Electronics 10

So following on from the previous post. Still using the Easy module shield) I have found out how to read the analogue input from the potentiometer. So far this gives a value and voltage.

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-potentiometer
 */

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin A0:
  int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 5V):
  float voltage = floatMap(analogValue, 0, 1023, 0, 5);

  // print out the value you read:
  Serial.print("Analog: ");
  Serial.print(analogValue);
  Serial.print(", Voltage: ");
  Serial.println(voltage);
  delay(1000);
}

So the two lines (the first is a comment) sets the program to read from A0

 // read the input on analog pin A0:
  int analogValue = analogRead(A0);

With output looking like

 // read the input on analog pin A0:
  int analogValue = analogRead(A0);

READING THE LIGHT SENSOR

By changing this to

 // read the input on analog pin A1:
  int analogValue = analogRead(A1);

It is possible to read the light sensor on the same board.

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-potentiometer
 */

float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin A0:
  int analogValue = analogRead(A0);
  // Rescale to potentiometer's voltage (from 0V to 5V):
  float light = floatMap(analogValue, 0, 1023, 0, 5);

  // print out the value you read:
  Serial.print("Analog: ");
  Serial.print(analogValue);
  Serial.print(", Light: ");
  Serial.println(light);
  delay(1000);
}

I have edited the lines above so that it reflects the fact we are now reading the light sensor.

  float light = floatMap(analogValue, 0, 1023, 0, 5);

  // print out the value you read:
  Serial.print("Analog: ");
  Serial.print(analogValue);
  Serial.print(", Light: ");
  Serial.println(light);
  delay(1000);
}

You will need to change the other lines to suit, as some of the comments still refer to the potentiometer.

Video

Video illustrates how rotation of the potentiometer is reflected in the output values.

Links

Tags

#Electronics,#Code,#Arduino,#Hacking.#TempSensor


MastodonPeertubeQoto sign up

Donate using Liberapay