12 static bool digitalReadSafe(uint8_t pin,
bool defaultValue = 1)
14 if (pin < NUM_DIGITAL_PINS)
16 return digitalRead(pin);
24 static void pinModeSafe(uint8_t pin, uint8_t mode)
26 if (pin < NUM_DIGITAL_PINS)
32 void Zumo32U4ProximitySensors::clearAll()
48 uint8_t lineSensorEmitterPin)
50 this->lineSensorEmitterPin = lineSensorEmitterPin;
55 const size_t size = numSensors *
sizeof(SensorData);
56 void * newArray = realloc(dataArray, size);
57 if (newArray == NULL) {
return; }
60 this->dataArray = (SensorData *)newArray;
63 this->numSensors = numSensors;
65 for (uint8_t i = 0; i < numSensors; i++)
68 dataArray[i].pin = pins[i];
75 const size_t size = numLevels *
sizeof(uint16_t);
77 void * newArray = realloc(levelsArray, size);
85 memcpy(newArray, levels, size);
86 levelsArray = (uint16_t *)newArray;
87 this->numLevels = numLevels;
90 void Zumo32U4ProximitySensors::prepareToRead()
96 if (levelsArray == NULL)
98 uint16_t defaultBrightnessLevels[] = { 4, 15, 32, 55, 85, 120 };
107 for (uint8_t i = 0; i < numSensors; i++)
109 pinModeSafe(dataArray[i].pin, INPUT_PULLUP);
117 if (lineSensorEmitterPin < NUM_DIGITAL_PINS)
119 digitalWrite(lineSensorEmitterPin, LOW);
120 pinMode(lineSensorEmitterPin, OUTPUT);
121 delayMicroseconds(pulseOffTimeUs);
133 for (uint8_t i = 0; i < numSensors; i++)
135 dataArray[i].withRightLeds = 0;
136 dataArray[i].withLeftLeds = 0;
139 for (uint8_t i = 0; i < numLevels; i++)
141 uint16_t brightness = levelsArray[i];
144 delayMicroseconds(pulseOnTimeUs);
145 for (uint8_t i = 0; i < numSensors; i++)
147 if (!digitalReadSafe(dataArray[i].pin, 1))
149 dataArray[i].withLeftLeds++;
153 delayMicroseconds(pulseOffTimeUs);
156 delayMicroseconds(pulseOnTimeUs);
157 for (uint8_t i = 0; i < numSensors; i++)
159 if (!digitalReadSafe(dataArray[i].pin, 1))
161 dataArray[i].withRightLeds++;
165 delayMicroseconds(pulseOffTimeUs);
171 if (sensorNumber >= numSensors) {
return 0; }
172 return !digitalReadSafe(dataArray[sensorNumber].pin, 1);
177 if (sensorNumber >= numSensors) {
return 0; }
178 return dataArray[sensorNumber].withLeftLeds;
183 if (sensorNumber >= numSensors) {
return 0; }
184 return dataArray[sensorNumber].withRightLeds;
187 uint8_t Zumo32U4ProximitySensors::findIndexForPin(uint8_t pin)
const
189 for (uint8_t i = 0; i < numSensors; i++)
191 if (dataArray[i].pin == pin)
static const uint8_t SENSOR_NO_PIN
A constant that can be used in place of a pin number to indicate that no pin should be used.
static void stop()
Stops emitting IR pulses.
static void start(Direction direction, uint16_t brightness, uint16_t period=defaultPeriod)
Starts emitting IR pulses.
void read()
Emits IR pulses and gets readings from the sensors.
static const uint16_t defaultPeriod
The default period for the infrared pulses.
void init(uint8_t *pins, uint8_t numSensors, uint8_t lineSensorEmitterPin=defaultLineSensorEmitterPin)
Configures this object to use a custom set of pins.
uint8_t countsWithRightLeds(uint8_t sensorNumber) const
Returns the number of brightness levels for the right LEDs that activated the specified sensor.
void setBrightnessLevels(uint16_t *levels, uint8_t levelCount)
Sets the sequence of brightness levels used by read().
void lineSensorEmittersOff()
Turns the IR emitters for the line sensors off.
bool readBasic(uint8_t sensorNumber)
Does a quick digital reading of the specified sensor without emitting any IR pulses.
static const uint16_t defaultPulseOffTimeUs
The default time to leave the infrared LEDs off between readings, in microseconds.
static const uint16_t defaultPulseOnTimeUs
The default duration of the bursts of infrared pulses emitted, in microseconds.
uint8_t countsWithLeftLeds(uint8_t sensorNumber) const
Returns the number of brightness levels for the left LEDs that activated the specified sensor.
void pullupsOn()
Sets each sensor pin to an input with pull-up resistors enabled.