c_cpp Robolution基本代码片段
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Robolution基本代码片段相关的知识,希望对你有一定的参考价值。
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
#include "Adafruit_VL53L0X.h"
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
void setup() {
// Robojax.com I2C address update 20181206
lox.begin(0x31);// put any address between 0x29 to 0x7F
}
void loop(){
}
#include "Adafruit_VL53L0X.h"
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
void setup() {
Serial.begin(9600);
// wait until serial port opens for native USB devices
while (! Serial) {
delay(1);
}
Serial.println("Adafruit VL53L0X test");
if (!lox.begin()) {
Serial.println(F("Failed to boot VL53L0X"));
while(1);
}
// power
Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
}
void loop() {
VL53L0X_RangingMeasurementData_t measure;
Serial.print("Reading a measurement... ");
lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!
if (measure.RangeStatus != 4) { // phase failures have incorrect data
Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
} else {
Serial.println(" out of range ");
}
delay(100);
}
#include <Wire.h>
#include <VL53L0X.h>
VL53L0X sensor;
VL53L0X sensor2;
void setup()
{
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
delay(500);
Wire.begin();
Serial.begin (9600);
//SENSOR
pinMode(10, INPUT);
delay(150);
Serial.println("00");
sensor.init(true);
Serial.println("01");
delay(100);
sensor.setAddress((uint8_t)22);
Serial.println("02");
//SENSOR 2
pinMode(11, INPUT);
delay(150);
sensor2.init(true);
Serial.println("03");
delay(100);
sensor2.setAddress((uint8_t)25);
Serial.println("04");
Serial.println("");
Serial.println("addresses set");
Serial.println("");
Serial.println("");
sensor.setTimeout(500);
sensor2.setTimeout(500);
}
void loop()
{
Serial.println("__________________________________________________________________");
Serial.println("");
Serial.println("=================================");
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;
//for (byte i = 1; i < 120; i++)
for (byte i = 1; i < 120; i++)
{
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0)
{
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
Serial.println("=================================");
//CHECK DISTANCES
long DISTANCE_FWD = (sensor.readRangeSingleMillimeters());
long DISTANCE_FLT = (sensor2.readRangeSingleMillimeters());
//FWD OR SENSOR
if (sensor.timeoutOccurred())
{
Serial.println("_________________________________");
Serial.print("Distance FWD (READING): ");
Serial.println(" TIMEOUT");
Serial.println("_________________________________");
Serial.println("");
}
else
{
Serial.println("_________________________________");
Serial.print("Distance FWD (feet): ");
Serial.println(DISTANCE_FWD/12);
Serial.print("Distance FWD (inches): ");
Serial.println(DISTANCE_FWD);
Serial.println("_________________________________");
Serial.println("");
}
//FLT OR SENSOR2
if (sensor2.timeoutOccurred())
{
Serial.println("_________________________________");
Serial.print("Distance FLT (READING): ");
Serial.println(" TIMEOUT");
Serial.println("_________________________________");
Serial.println("");
}
else
{
Serial.println("_________________________________");
Serial.print("Distance FLT (feet): ");
Serial.println(DISTANCE_FLT/12);
Serial.print("Distance FLT (inches): ");
Serial.println(DISTANCE_FLT);
Serial.println("_________________________________");
Serial.println("");
}
Serial.println("__________________________________________________________________");
Serial.println();
Serial.println();
Serial.println();
Serial.println();
delay(2000);//can change to a lower time like 100
}
#include <Wire.h>
#include <VL53L0X.h>
//#define XSHUT_pin6 not required for address change
#define XSHUT_pin5 9
#define XSHUT_pin4 8
#define XSHUT_pin3 7
#define XSHUT_pin2 6
#define XSHUT_pin1 5
//ADDRESS_DEFAULT 0b0101001 or 41
//#define Sensor1_newAddress 41 not required address change
#define Sensor2_newAddress 42
#define Sensor3_newAddress 43
#define Sensor4_newAddress 44
#define Sensor5_newAddress 45
#define Sensor6_newAddress 46
VL53L0X Sensor1;
VL53L0X Sensor2;
VL53L0X Sensor3;
VL53L0X Sensor4;
VL53L0X Sensor5;
VL53L0X Sensor6;
void setup()
{ /*WARNING*/
//Shutdown pins of VL53L0X ACTIVE-LOW-ONLY NO TOLERANT TO 5V will fry them
pinMode(XSHUT_pin1, OUTPUT);
pinMode(XSHUT_pin2, OUTPUT);
pinMode(XSHUT_pin3, OUTPUT);
pinMode(XSHUT_pin4, OUTPUT);
pinMode(XSHUT_pin5, OUTPUT);
Serial.begin(9600);
Wire.begin();
//Change address of sensor and power up next one
Sensor6.setAddress(Sensor6_newAddress);
pinMode(XSHUT_pin5, INPUT);
delay(10); //For power-up procedure t-boot max 1.2ms "Datasheet: 2.9 Power sequence"
Sensor5.setAddress(Sensor5_newAddress);
pinMode(XSHUT_pin4, INPUT);
delay(10);
Sensor4.setAddress(Sensor4_newAddress);
pinMode(XSHUT_pin3, INPUT);
delay(10);
Sensor3.setAddress(Sensor3_newAddress);
pinMode(XSHUT_pin2, INPUT);
delay(10);
Sensor2.setAddress(Sensor2_newAddress);
pinMode(XSHUT_pin1, INPUT);
delay(10);
Sensor1.init();
Sensor2.init();
Sensor3.init();
Sensor4.init();
Sensor5.init();
Sensor6.init();
Sensor1.setTimeout(500);
Sensor2.setTimeout(500);
Sensor3.setTimeout(500);
Sensor4.setTimeout(500);
Sensor5.setTimeout(500);
Sensor6.setTimeout(500);
// Start continuous back-to-back mode (take readings as
// fast as possible). To use continuous timed mode
// instead, provide a desired inter-measurement period in
// ms (e.g. sensor.startContinuous(100)).
Sensor1.startContinuous();
Sensor2.startContinuous();
Sensor3.startContinuous();
Sensor4.startContinuous();
Sensor5.startContinuous();
Sensor6.startContinuous();
}
void loop()
{
Serial.print(Sensor1.readRangeContinuousMillimeters());
Serial.print(',');
Serial.print(Sensor2.readRangeContinuousMillimeters());
Serial.print(',');
//Serial.print(Sensor3.readRangeContinuousMillimeters());
//Serial.print(','); Did not work for me result was 65535
Serial.print(Sensor4.readRangeContinuousMillimeters());
Serial.print(',');
Serial.print(Sensor5.readRangeContinuousMillimeters());
Serial.print(',');
Serial.print(Sensor6.readRangeContinuousMillimeters());
Serial.println();
}
以上是关于c_cpp Robolution基本代码片段的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp 快速代码片段,用于在统计(阻止)/ dev / rdsk中的设备时验证fstat64和stat64的行为。
c_cpp 这个简单的代码片段显示了如何使用有符号整数在C中完成插值。 for()循环确定要插入的范围
c_cpp UV Index Indicator订阅PubNub并使用颜色显示UV索引值。博文的代码片段。在这里查看项目:https:/