Question

I'm working on a program that will read distance with 16 analog sensors. I just came to the problem, and compare them to see which one is the largest. Perhaps there is a method or something that help me? If not, how do I do compare them? I need to find the largest value of the sensors to work with later.

Please help me, I'm stuck at this for the last 2 hours.

Here is the code:

#define a A0
#define b A1
#define c A2
#define d A3
#define e A4
#define f A5
#define g A6
#define h A7
#define i A8
#define j A9
#define k A10
#define l A11
#define m A12
#define n A13
#define o A14
#define p A15


int aState = 0;
int bState = 0;
int cState = 0;
int dState = 0;
int eState = 0;
int fState = 0;
int gState = 0;
int hState = 0;
int iState = 0;
int jState = 0;
int kState = 0;
int lState = 0;
int mState = 0;
int nState = 0;
int oState = 0;
int pState = 0;

void setup()
{
  pinMode(a, INPUT);
  pinMode(b, INPUT); 
  pinMode(c, INPUT); 
  pinMode(d, INPUT); 
  pinMode(e, INPUT); 
  pinMode(f, INPUT); 
  pinMode(g, INPUT); 
  pinMode(h, INPUT); 
  pinMode(i, INPUT); 
  pinMode(j, INPUT); 
  pinMode(k, INPUT); 
  pinMode(l, INPUT); 
  pinMode(m, INPUT); 
  pinMode(n, INPUT); 
  pinMode(o, INPUT); 
  pinMode(p, INPUT);  
} 

void loop()
{

  int x[15];  
  x[0] = analogRead(a)/4;
  x[1] = analogRead(b)/4;
  x[2] = analogRead(c)/4;
  x[3] = analogRead(d)/4;
  x[4] = analogRead(e)/4;
  x[5] = analogRead(f)/4;
  x[6] = analogRead(g)/4;
  x[7] = analogRead(h)/4;
  x[8] = analogRead(i)/4;
  x[9] = analogRead(j)/4;
  x[10] = analogRead(k)/4;
  x[11] = analogRead(l)/4;
  x[12] = analogRead(m)/4;
  x[13] = analogRead(n)/4;
  x[14] = analogRead(o)/4;
  x[15] = analogRead(p)/4;



}
Was it helpful?

Solution

add

#include <limits.h>

on top and later you can do something like:

int i, iMax;
int maxValue=INT_MIN;
for(i=0;i<16;i++) {
  if(x[i] > maxValue) {
    maxValue = x[i];
    iMax = i;
  }  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top