Created Fri, 13 Nov 2015 21:25:00 +0000 by barronl
Fri, 13 Nov 2015 21:25:00 +0000
I am trying to develop a program in MPIDE to track the position of an object containing a rare earth magnet travelling along a straight path by monitoring the outputs of a series of SS49E hall effect sensors, spaced at 2cm intervals above the path. To achieve this I need to first measure the output of each of the 10 sensors whilst under no magnetic influence, to obtain a default reading for each specific sensor. The default values for each sensor then needs to be declared as a 'const int' value in each case outside of the Void setup & void loop functions, so these default values can be called from anywhere in the program. My problem is that the default values for analogRead(0) to analogRead(2) & analogRead(5) to analogRead(8), come out fine, but the analogReads for (3),(4) & (9), always come out as zero, as shown below in the output to the Serial Monitor: -
** * Sensor 1 default reading = 532 Sensor 2 default reading = 526 Sensor 3 default reading = 513 Sensor 4 default reading = 0 Sensor 5 default reading = 0 Sensor 6 default reading = 509 Sensor 7 default reading = 540 Sensor 8 default reading = 530 Sensor 9 default reading = 516 Sensor 10 default reading = 0
SNSR0=533; SNSR1=526; SNSR2=514; SNSR3=523; SNSR4=521; SNSR5=509; SNSR6=540; SNSR7=530; SNSR8=517; SNSR9=521;
SNSR0=533; SNSR1=527; SNSR2=514; SNSR3=523; SNSR4=522; SNSR5=510; SNSR6=541; SNSR7=530; SNSR8=517; SNSR9=521;
Reading these same analog ports in the Void Loop part of the program shows that all the sensors & analog ports are working fine, as in the last 2 lines above.
I have been struggling to overcome this problem for 4 days now, but still can fathom out what's causing this problem.
Code is shown below, & any help or suggestions on this problem would be very much appreciated.
/*
Program to get default values for the 1st 10 ADC ports connected to Hall Effect Sensors whilst no magnets are within range of the sensor
*/
int a = 0;
int result;
int Snsr1;
int Snsr2;
int Snsr3;
int Snsr4;
int Snsr5;
int Snsr6;
int Snsr7;
int Snsr8;
int Snsr9;
int Snsr10;
/* "int Get_Rdg(){}" Is a function to get default values for the 1st 10 ADC ports, each being connected to Hall Effect Sensors whilst no magnets are within
* range of the sensors.
* This is achieved by sampling each sensor 5 times, discarding the 1st reading, then totalling the results of the next 4 samples
* & diveding the result by 4. The result is then assigned to selected analog channel's constant variable.
* The number of the analog pin to be sampled is then advanced to next analog pin until all 10 sensors have
* been sampled.
*/
int GET_RDG(){
result = 0; // Discard any previous results
int i; //Loop Counter
int sval = 0;
for (i = 0; i <= 4; i++)
{
sval = sval + analogRead(a); // Sample the selected analog pin.
if (i != 0) // Check for 1st reading in the loop.
{sval = sval;} // If it isn't the first, keep the result.
else
{sval = 0;} // If it is the first, discard the result.
}
result = sval/4; // Calculate the average of the last 4 samples & assign to 'resault' variable.
a++; // Increment variable 'a' to next analog port.
return result;
// delay(50);
}
const int Snr1 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 1
const int Snr2 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 2
const int Snr3 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 3
const int Snr4 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 4
const int Snr5 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 5
const int Snr6 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 6
const int Snr7 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 7
const int Snr8 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 8
const int Snr9 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 9
const int Snr10 = GET_RDG(); // Use GET_RDG() function to default reading for Sensor 10
void setup() {
// Open serial port & print out the default values for each of the 10 sensors.
Serial.begin(9600);
delay(2000);
Serial.print("Sensor 1 default reading = ");
Serial.println(Snr1);
Serial.print("Sensor 2 default reading = ");
Serial.println(Snr2);
Serial.print("Sensor 3 default reading = ");
Serial.println(Snr3);
Serial.print("Sensor 4 default reading = ");
Serial.println(Snr4);
Serial.print("Sensor 5 default reading = ");
Serial.println(Snr5);
Serial.print("Sensor 6 default reading = ");
Serial.println(Snr6);
Serial.print("Sensor 7 default reading = ");
Serial.println(Snr7);
Serial.print("Sensor 8 default reading = ");
Serial.println(Snr8);
Serial.print("Sensor 9 default reading = ");
Serial.println(Snr9);
Serial.print("Sensor 10 default reading = ");
Serial.println(Snr10);
}
void loop(){
// Continually sample each ADC port & print out readings of each of the 10 sensors in turn.
Serial.println(" ");
a = 0;
for (a = 0; a <= 9; a++) {
int Rdg = analogRead(a);
if (a == 0) {
Snsr1 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr1);
Serial.print("; ");
}
if (a == 1) {
Snsr2 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr2);
Serial.print("; ");
}
if (a == 2) {
Snsr3 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr3);
Serial.print("; ");
}
if (a == 3) {
Snsr4 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr4);
Serial.print("; ");
}
if (a == 4) {
Snsr5 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr5);
Serial.print("; ");
}
if (a == 5) {
Snsr6 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr6);
Serial.print("; ");
}
if (a == 6) {
Snsr7 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr7);
Serial.print("; ");
}
if (a == 7) {
Snsr8 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr8);
Serial.print("; ");
}
if (a == 8) {
Snsr9 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr9);
Serial.print("; ");
}
if (a == 9) {
Snsr10 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr10);
Serial.println("; ");
}
}
delay(1000);
}
Tue, 05 Jan 2016 05:55:02 +0000
Hi,
I do not have your sensors but I do have a working uC32. So first make sure jumpers JP6 and JP8 are the "AN" position that is away from the analog inputs. You cannot use analog pins 4 & 5 if you use the I2C bus, sorry. Then when you were taking your baseline readings, the "GET_RDG()" function may not have been reading the right channel "I think it was but OK" I don't know about AN9 seams to work on mine. I took the liberty of modifying your code. This is the output I get:
Sensor 1 default reading = 558 Sensor 2 default reading = 516 Sensor 3 default reading = 499 Sensor 4 default reading = 510 Sensor 5 default reading = 540 Sensor 6 default reading = 537 Sensor 7 default reading = 467 Sensor 8 default reading = 505 Sensor 9 default reading = 470 Sensor 10 default reading = 456
SNSR1=694; SNSR2=758; SNSR3=758; SNSR4=764; SNSR5=785; SNSR6=780; SNSR7=735; SNSR8=760; SNSR9=741; SNSR10=722;
SNSR1=721; SNSR2=763; SNSR3=759; SNSR4=762; SNSR5=785; SNSR6=788; SNSR7=734; SNSR8=760; SNSR9=741; SNSR10=722;
I have nothing connected to the pins.
Luc
/*
Program to get default values for the 1st 10 ADC ports connected to Hall Effect Sensors whilst no magnets are within range of the sensor
*/
int a = 0;
int result;
int Snsr[10];
/*
int Snsr1;
int Snsr2;
int Snsr3;
int Snsr4;
int Snsr5;
int Snsr6;
int Snsr7;
int Snsr8;
int Snsr9;
int Snsr10;
*/
/* "int Get_Rdg(){}" Is a function to get default values for the 1st 10 ADC ports, each being connected to Hall Effect Sensors whilst no magnets are within
* range of the sensors.
* This is achieved by sampling each sensor 5 times, discarding the 1st reading, then totalling the results of the next 4 samples
* & diveding the result by 4. The result is then assigned to selected analog channel's constant variable.
* The number of the analog pin to be sampled is then advanced to next analog pin until all 10 sensors have
* been sampled.
*/
int GET_RDG(int a){
result = 0; // Discard any previous results
int i; //Loop Counter
int sval = 0;
for (i = 0; i <= 4; i++)
{
sval = sval + analogRead(a); // Sample the selected analog pin.
if (i != 0) // Check for 1st reading in the loop.
{sval = sval;} // If it isn't the first, keep the result.
else
{sval = 0;} // If it is the first, discard the result.
}
result = sval/4; // Calculate the average of the last 4 samples & assign to 'resault' variable.
// a++; // Increment variable 'a' to next analog port.
return result;
// delay(50);
}
const int Snr1 = GET_RDG(0); // Use GET_RDG() function to default reading for Sensor 1
const int Snr2 = GET_RDG(1); // Use GET_RDG() function to default reading for Sensor 2
const int Snr3 = GET_RDG(2); // Use GET_RDG() function to default reading for Sensor 3
const int Snr4 = GET_RDG(3); // Use GET_RDG() function to default reading for Sensor 4
const int Snr5 = GET_RDG(4); // Use GET_RDG() function to default reading for Sensor 5
const int Snr6 = GET_RDG(5); // Use GET_RDG() function to default reading for Sensor 6
const int Snr7 = GET_RDG(6); // Use GET_RDG() function to default reading for Sensor 7
const int Snr8 = GET_RDG(7); // Use GET_RDG() function to default reading for Sensor 8
const int Snr9 = GET_RDG(8); // Use GET_RDG() function to default reading for Sensor 9
const int Snr10 = GET_RDG(9); // Use GET_RDG() function to default reading for Sensor 10
void setup() {
// Open serial port & print out the default values for each of the 10 sensors.
Serial.begin(9600);
delay(2000);
Serial.print("Sensor 1 default reading = ");
Serial.println(Snr1);
Serial.print("Sensor 2 default reading = ");
Serial.println(Snr2);
Serial.print("Sensor 3 default reading = ");
Serial.println(Snr3);
Serial.print("Sensor 4 default reading = ");
Serial.println(Snr4);
Serial.print("Sensor 5 default reading = ");
Serial.println(Snr5);
Serial.print("Sensor 6 default reading = ");
Serial.println(Snr6);
Serial.print("Sensor 7 default reading = ");
Serial.println(Snr7);
Serial.print("Sensor 8 default reading = ");
Serial.println(Snr8);
Serial.print("Sensor 9 default reading = ");
Serial.println(Snr9);
Serial.print("Sensor 10 default reading = ");
Serial.println(Snr10);
}
void loop(){
// Continually sample each ADC port & print out readings of each of the 10 sensors in turn.
Serial.println(" ");
// a = 0;
for (a = 0; a <= 9; a++) {
int Rdg = analogRead(a);
Snsr[a] = Rdg;
Serial.print("SNSR");
Serial.print(a+1);
Serial.print("=");
Serial.print(Snsr[a]);
Serial.print("; ");
/*
if (a == 0) {
Snsr1 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr1);
Serial.print("; ");
}
if (a == 1) {
Snsr2 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr2);
Serial.print("; ");
}
if (a == 2) {
Snsr3 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr3);
Serial.print("; ");
}
if (a == 3) {
Snsr4 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr4);
Serial.print("; ");
}
if (a == 4) {
Snsr5 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr5);
Serial.print("; ");
}
if (a == 5) {
Snsr6 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr6);
Serial.print("; ");
}
if (a == 6) {
Snsr7 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr7);
Serial.print("; ");
}
if (a == 7) {
Snsr8 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr8);
Serial.print("; ");
}
if (a == 8) {
Snsr9 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr9);
Serial.print("; ");
}
if (a == 9) {
Snsr10 = Rdg;
Serial.print("SNSR");
Serial.print(a);
Serial.print("=");
Serial.print(Snsr10);
Serial.println("; ");
}
*/
}
delay(1000);
}