@@ -68,6 +68,11 @@ ScienceKitCarrier::ScienceKitCarrier(){
6868 range1=0 ;
6969 range2=0 ;
7070
71+ ultrasonic = new Arduino_GroveI2C_Ultrasonic ();
72+ distance=0.0 ;
73+ travel_time=0.0 ;
74+ ultrasonic_is_connected=false ;
75+
7176
7277 thread_activity_led = new rtos::Thread ();
7378 thread_update_bme = new rtos::Thread ();
@@ -90,6 +95,8 @@ int ScienceKitCarrier::begin(const bool auxiliary_threads){
9095
9196 Wire.begin ();
9297
98+ // most of begin functions return always 0, it is a code-style or future implementation
99+
93100 // let's start apds89960
94101 if (beginAPDS ()!=0 ){
95102 return ERR_BEGIN_APDS;
@@ -100,7 +107,7 @@ int ScienceKitCarrier::begin(const bool auxiliary_threads){
100107 return ERR_BEGIN_INA;
101108 }
102109
103- // resistance pin
110+ // let's start resistance measurement
104111 if (beginResistance ()!=0 ){
105112 return ERR_BEGIN_RESISTANCE;
106113 }
@@ -115,6 +122,12 @@ int ScienceKitCarrier::begin(const bool auxiliary_threads){
115122 return ERR_BEGIN_FUNCTION_GENERATOR_CONTROLLER;
116123 }
117124
125+ // let's start ultrasonic and check if it is connected
126+ if (beginUltrasonic ()!=0 ){
127+ return ERR_BEGIN_ULTRASONIC;
128+ }
129+
130+
118131 // let's start activity led and bme688
119132 if (auxiliary_threads){
120133 startAuxiliaryThreads ();
@@ -136,11 +149,15 @@ void ScienceKitCarrier::update(const bool roundrobin){
136149 updateINA ();
137150 updateResistance ();
138151 updateIMU ();
152+
153+ // update external
154+ updateUltrasonic ();
139155 }
140156 else {
141157 switch (round_robin_index){
142158 case 0 :
143- updateAnalogInput ();
159+ updateAnalogInput (); // it is very fast (about 2ms)
160+ updateUltrasonic (); // requires about 5ms when not connected
144161 break ;
145162 case 1 :
146163 updateAPDS ();
@@ -602,6 +619,37 @@ uint8_t ScienceKitCarrier::getRange2(){
602619 return range2;
603620}
604621
622+ /* *******************************************************************/
623+ /* Ultrasonic Sensor */
624+ /* *******************************************************************/
625+
626+ int ScienceKitCarrier::beginUltrasonic (){
627+ ultrasonic->begin ();
628+ updateUltrasonic ();
629+ }
630+
631+ void ScienceKitCarrier::updateUltrasonic (){
632+ if (ultrasonic->checkConnection ()){
633+ ultrasonic_is_connected=true ;
634+ distance=ultrasonic->getMeters ();
635+ travel_time=ultrasonic->getTravelTime ();
636+ }
637+ else {
638+ ultrasonic_is_connected=false ;
639+ }
640+ }
641+
642+ float ScienceKitCarrier::getDistance (){
643+ return distance;
644+ }
645+
646+ float ScienceKitCarrier::getTravelTime (){
647+ return travel_time;
648+ }
649+
650+ bool ScienceKitCarrier::getUltrasonicIsConnected (){
651+ return ultrasonic_is_connected;
652+ }
605653
606654
607655
0 commit comments