Experimenting with Ringo's edge sensors and ambient light sensors

Here is my first code offering. What it does and how it works is in the comments of the code. I tried to be very verbose in my comments. Open up an “NEW” IDE and then copy/pase this code into it, and then save it to your drive. Download and explore and enjoy.

// This is a simple program for ewxploring the inner workings of the Ringo Robot.
// Load this onto your Ringo and open the Serial Monitor to 9600 baud
// Set down and then pick up the Ringo, watch the numbers change for the IR sensors.

// Ringo uses the same three analog inputs for edge sensing and ambient light sensing. You toggle which of these you want by setting D4 low or high.
//#define SourceSelect LOW // Uncomment this for edge sensing
#define SourceSelect HIGH // Uncomment this for ambient light sensing. I don’t see a lot of clear diferentiation between light and dark.

// The IR sensors work by emitting IR light and sensing IR light reflected back
// A low number means little reflected light seen, high means more reflected light seen
// Try covering the sensors and watching the numbers. Also, set Ringo on different surfaces to
// see how well the IR is reflected. Notice the differences if Ringo is tipped onto the front
// felt pad, or the rear. The larger the gap between the sensor and surface, the lower the reading.

// You can use this routine to understand what Ringo is seeing with his sensors when you are
// trying to debug other code. Just load it up and see what Ringo sees.

int Analog[4]; // Create an array to record all 4 analog inputs.
char strng[100]; // We will use this string array to format the output data
int InitRead[4];

void setup()
{
// We are just going to set all analog pins as analog inputs
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);

pinMode(4, OUTPUT); // This is the output that switches sensors from edge to ambient
digitalWrite(4, SourceSelect);

Serial.begin(9600); // Initialize the serial port to 9600 baud

// This next line sends a header to the serial port to help the user understand the meaning of the data
Serial.println("\nBattery Right Sensor Left Sensor Tail Sensor");

pinMode(8, OUTPUT); // This output can turn on the IR sensor emitters, generating the IR lights
digitalWrite(8,HIGH); // Here we actually turn on the IR emitters
InitRead[0] = analogRead(A0); // Battery
InitRead[1] = analogRead(A1); // Right Antenna Sensor
InitRead[2] = analogRead(A2); // Left Antenna Sensor
InitRead[3] = analogRead(A3); // Tail Antenna Sensor
}

void loop()
{
// Here we read all 8 analog inputs. We could use a loop to do this, but I also want to label them.
Analog[0] = analogRead(A0); // Battery
Analog[1] = analogRead(A1); // Right Antenna Sensor
Analog[2] = analogRead(A2); // Left Antenna Sensor
Analog[3] = analogRead(A3); // Tail Antenna Sensor

// sprintf() is a C routine that formats data into a string. It is similar to printf() which prints to the display,
// and it is also similar to fprintf() which prints to a file on the disk. Since Ringo has no display
// or disk, those other commands are invalid here. You can web-search “sprintf” for more details.
// Note: Sprintf is not a memory-efficient command, owing to the need for the char array declared above. But in this
// program we are not worried about running out of memory, and the routine is certainly handy for proper formatting.
// Notice that all the data falled into neat columns.
sprintf(strng, " %3d %4d %4d %4d", Analog[0], Analog[1], Analog[2], Analog[3]);
Serial.println(strng); // Here we send the string to the serial port.
sprintf(strng, " %3d %4d %4d %4d\n", Analog[0] - InitRead[0], Analog[1] - InitRead[1], Analog[2] - InitRead[2], Analog[3] - InitRead[3]);
Serial.println(strng); // Here we send the string to the serial port.
delay(1000); // Wait 1 second, then re-run loop again.
}

1 Like