You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.5 KiB
76 lines
1.5 KiB
3 years ago
|
|
||
|
int sensores[]={A0,A1,A2,A3,A4,A5,A6,A7};//Sensores
|
||
|
int valores[8];
|
||
|
int umbral[]={1023,1023,1023,1023,1023,1023,1023,1023};
|
||
|
int numSensores=8;//total de sensres en la barra
|
||
|
int ir =2;//encender y apagar la barra
|
||
|
|
||
|
|
||
|
void setup() {
|
||
|
Serial.begin(115200);
|
||
|
//Inicializamos los pines de los sensores
|
||
|
for(int x=0;x<numSensores;x++){
|
||
|
pinMode(sensores[x],INPUT);
|
||
|
}
|
||
|
pinMode(LED_BUILTIN,OUTPUT);
|
||
|
//pin para controlar el encendido de la barra
|
||
|
pinMode(ir,OUTPUT);
|
||
|
digitalWrite(ir,HIGH);//Encendemos la barra
|
||
|
calibracion();
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
int pos=getPosicion();
|
||
|
|
||
|
//PINTAR LOS VALORES
|
||
|
for(int x=0;x<numSensores;x++){
|
||
|
Serial.print(valores[x]);
|
||
|
Serial.print("\t");
|
||
|
}
|
||
|
Serial.println(pos);
|
||
|
delay(100);
|
||
|
}
|
||
|
|
||
|
void leerRAW(){
|
||
|
//LEER LOS VALORES DE LOS SENSORES
|
||
|
for(int x=0;x<numSensores;x++){
|
||
|
valores[x]=analogRead(sensores[x]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int getPosicion(){
|
||
|
leerDigital();
|
||
|
int activos=0;
|
||
|
int suma=0;
|
||
|
for(int x=0;x<numSensores;x++){
|
||
|
suma=suma+valores[x];
|
||
|
if(valores[x]!=0){
|
||
|
activos++;
|
||
|
}
|
||
|
}
|
||
|
return suma/activos;
|
||
|
|
||
|
}
|
||
|
|
||
|
void leerDigital(){
|
||
|
//LEER LOS VALORES DE LOS SENSORES
|
||
|
for(int x=0;x<numSensores;x++){
|
||
|
valores[x]=analogRead(sensores[x])>(umbral[x]+70)?(x*500)+500:0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void calibracion(){
|
||
|
digitalWrite(LED_BUILTIN,HIGH);
|
||
|
for(int y=0;y<400;y++){
|
||
|
for(int x=0;x<numSensores;x++){
|
||
|
int v=analogRead(sensores[x]);
|
||
|
if(umbral[x]>v){
|
||
|
umbral[x]=v;
|
||
|
}
|
||
|
}
|
||
|
delay(10);
|
||
|
}
|
||
|
|
||
|
digitalWrite(LED_BUILTIN,LOW);
|
||
|
}
|