I wrote code for the vibrating motors to vibrate when an IR sensor detects a darker surface. I have 4 vibration motors for corresponding 4 IR sensors. My program is not able to read the values from the sensor. In the serial monitor the values that are read from the sensor is a huge non stopping value. None of my IR sensors is detecting the surface. As a result no vibrations are produced. I am using Arduino Pro Mini and my code is:
#include <EEPROM.h>
// pin numbers
const int irLED = 7; // wire connected to transistor/220 ohm resistor (all on same pin)
const int collectors[] = {A0,A1,A2,A3}; // (with pullup resistors)
const int vibrations[] = {3,5,6,9}; // vibration motors
// thresholds for each finger
int detectionThreshold[] = {940,960,970,975};
int whiteThreshold[] = {600,600,600,600};
// timers (in milliseconds)
int lastVibration[] = {0,0,0,0};
const int numFingers = 4;
int readings[] = {1023, 1023, 1023, 1023, 1023, 1023};
boolean vibrating[] = {false, false, false, false};
void blackLoop()
{
int now = millis();
// get the current readings
for(int i = 0; i < numFingers; i++)
{
readings[i] = analogRead(collectors[i]);
Serial.print(readings[i]);
}
// vibrate on black, with minimum vibration time
for(int i = 0; i < numFingers; i++)
{
if(readings[i]<=detectionThreshold && readings[i] >= whiteThreshold[i])
{ Serial.print(readings[i]);
digitalWrite(vibrations[i], HIGH);
vibrating[i] = true;
lastVibration[i] = now;
}
else // if(now - lastVibration[i] > vibrationDuration)
{
digitalWrite(vibrations[i], LOW);
vibrating[i] = false;
}
}
}
void setup()
{
// put your setup code here, to run once:
for(int i=0; i<numFingers; i++)
{
digitalWrite(collectors[i], HIGH); // set pullup
pinMode(vibrations[i], OUTPUT);
}
pinMode(irLED, OUTPUT);
digitalWrite(irLED, HIGH);
Serial.begin(9600);
Serial.println("my sketch has started");
}
void loop()
{
// put your main code here, to run repeatedly:
blackLoop();
}