EVShield Library Tutorial
EVShield Library Tutorial
EVShield Library Tutorial
com
void setup()
{
Serial.begin(115200);
// start serial for output
delay(500);
// wait, allowing time to
// activate the serial monitor
Serial.println ("Initializing the devices ...");
//
// Initialize the protocol for EVShield
// Use Hardware I2C.
1/6
//
evshield.init( SH_HardwareI2C );
//
// Wait until user presses GO button to continue the program
//
Serial.println ("Press GO button to continue");
evshield.waitForButtonPress(BTN_GO);
//
// initialize the analog sensors.
// NXT light sensor is not supported on BAS1.
// connect a NXT light sensor to BAS2,
// connect a touch sensor to BAS1
//
touch1.init( &evshield, SH_BAS1 );
light1.init( &evshield, SH_BAS2 );
}
bool lastTouch, touchPressed;
void loop()
{
char
str[256];
int lightReading;
Serial.println("Into loop ------------------------------------");
touchPressed = touch1.isPressed();
sprintf (str, "touch1: is pressed : %s", touchPressed?"true":"false");
Serial.println(str);
if ( touchPressed != lastTouch ) {
if ( touchPressed == true ) {
Serial.println( "Changing light sensor to reflected light mode" );
light1.setActive();
} else {
Serial.println( "Changing light sensor to ambient light mode" );
light1.setPassive();
}
lastTouch = touchPressed;
}
lightReading = light1.readRaw();
sprintf (str, "Light sensor Reading: %d", lightReading);
Serial.println (str);
delay (500);
}
2/6
#include <Wire.h>
#include <EVShield.h>
#include <EVShieldAGS.h>
#include <EVs_EV3Ultrasonic.h>
//
// declare the EVShield(s) attached to your Arduino.
//
EVShield
evshield(0x34,0x36);
//
// declare the i2c devices used on EVShield(s).
//
EVs_EV3Ultrasonic us1;
void setup()
{
char str[50];
Serial.begin(115200); // start serial for output
delay(500); // wait, allowing time to activate the serial monitor
Serial.println ("--------------------------------------");
Serial.println ("Starting EV3 Ultrasonic Sensor Test program");
Serial.println ("--------------------------------------");
//
// Initialize the protocol for EVShield
// Use Hardware I2C.
//
evshield.init( SH_HardwareI2C );
//
// Wait until user presses GO button to continue the program
//
Serial.println ("Press GO button to continue");
evshield.waitForButtonPress(BTN_GO);
//
// Initialize the i2c sensors.
//
us1.init( &evshield, SH_BAS2 );
us1.setMode(MODE_Sonar_CM);
}
3/6
void loop()
{
uint8_t mode;
float distance;
char str[50];
int i;
//
// get the reading from sensor
//
distance = us1.getDist();
mode = us1.getMode();
//
// print the sensor staus
//
char *s;
switch (mode) {
case MODE_Sonar_Inches:
s = "Inches";
break;
case MODE_Sonar_CM:
s = "cm";
break;
}
Serial.print("Distance to obstacle: "); Serial.print(distance);
Serial.print (" (");
Serial.print (s);
Serial.println (")");
//
// wait for some time
//
delay(1000);
}
#include <Wire.h>
#include <EVShield.h>
#include <EVs_NXTCurrentMeter.h>
//
// declare the EVShield(s) attached to your Arduino.
//
EVShield
evshield(0x34,0x36);
4/6
//
// declare the i2c devices used on EVShield(s).
//
EVs_CurrentMeter im (0x28);
void setup()
{
char str[256];
Serial.begin(115200); // start serial for output
delay(500); // wait, allowing time to activate the serial monitor
//
// Initialize the protocol for EVShield
// Use Hardware I2C.
//
evshield.init( SH_HardwareI2C );
//
// Wait until user presses GO button to continue the program
//
Serial.println ("Press GO button to continue");
evshield.waitForButtonPress(BTN_GO);
//
// Initialize the i2c sensors.
//
im.init( &evshield, SH_BAS1 );
}
void loop()
{
char aa[80];
char str[256];
int acurr, rcurr, refI;
strcpy(aa, im.getFirmwareVersion() );
sprintf (str, "IMeter: getFirmwareVersion: %s", aa);
Serial.println(str);
strcpy(aa, im.getDeviceID() );
sprintf (str, "IMeter: DeviceID: %s", aa);
Serial.println(str);
strcpy(aa, im.getVendorID() );
sprintf (str, "IMeter: VendorID: %s", aa);
Serial.println(str);
/** Displays Absolute Current value
*/
acurr = im.getACurrent();
sprintf (str, "IMeter: Absolute Current: %d", acurr);
Serial.println(str);
/** Displays Relative Current value
*/
Copyright 2015 OpenElectrons.com
5/6
rcurr = im.getRCurrent();
sprintf (str, "IMeter: Relative Current: %d", rcurr);
Serial.println(str);
/** Displays Reference Current value
*/
refI = im.getReference();
sprintf (str, "IMeter: Reference Current: %d", refI);
Serial.println(str);
Serial.println( "-------------" );
delay (1500);
}
6/6