2

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();

}
gre_gor
  • 1,669
  • 4
  • 18
  • 28
Rebekah
  • 33
  • 2
  • IR may not reflect off your dark and light surfaces in the same way as visible light. Your IR sensors may not be able to tell the difference between what you perceive with your eyes as dark and light. – st2000 Apr 20 '17 at 02:24
  • In addition, may types of IR receivers are designed to automatically adjust the receiver's gain so as to, for example, mitigate poor performance from randomly located IR transmitters. IR receivers found in remote controlled TVs or remote controlled Stereos normally auto adjust their own gain. – st2000 Apr 20 '17 at 02:28

0 Answers0