The PIC32MX250F128B has 9 analog input lines with 10 bit resolution. They are numbersed as A0 - A8 and can be used with this name in the code. For compatibility with the other examples, I will stick to the pin names in my array. If I connect a 10 kOhm poti to GND and 3.3V and the middle pin to pin 2 on the left side, I can use this code to read the analog input (please note that the maximum voltage is 3.3V not 5V!)
/*
Code based on Analog Input
Created by David Cuartielles
Modified 4 Sep 2010 by Tom Igoe
modified 31 Dec 2014 by Mathias Wilhelm
This example code is in the public domain.
http://arduino.cc/en/Tutorial/AnalogInput
*/
const uint8_t LEFT=0;
const uint8_t RIGHT=1;
uint8_t nP[2][8] = {{0,17, 9,10,11,12,13,14},{18,17, 1, 2, 3, 6, 7, 8}};
int sensorPin = nP[LEFT][2]; // select the input pin for the potentiometer
int ledPin = nP[RIGHT][2]; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
}
It seems to be a good idea to connect the servo to the right side as above to combine the code:
/*
Control a Servo via a poti
Created 31 Dec 2014 by Mathias Wilhelm
*/
const uint8_t LEFT=0;
const uint8_t RIGHT=1;
uint8_t nP[2][8] = {{0,17, 9,10,11,12,13,14},{18,17, 1, 2, 3, 6, 7, 8}};
#include <SoftPWMServo.h>
int pos = 0; // variable to store the servo position, in microseconds
const int pin = nP[RIGHT][4]; // Choose _any_ pin number on your board
int sensorPin = nP[LEFT][2]; // select the input pin for the potentiometer
int ledPin = nP[RIGHT][2]; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
sensorValue = analogRead(sensorPin);
pos = map(sensorValue,0, 1023, 1000, 2000);
SoftPWMServoServoWrite(pin, pos);
delay(25);
}