Port 3 Ultrasonic pin mapping


#1

Hi!

Help me please.
I’m trying to program mBot by Arduino IDE.

int trig = A3; // or A2
int echo = A2; // or A3

void setup() {                
  pinMode( trig, OUTPUT );
  pinMode( echo, INPUT  );
  Serial.begin(9600);  
}

unsigned int impulseTime=0; 
unsigned int distance_sm=0;

void loop() 
{
  digitalWrite( trig, HIGH); 
  delayMicroseconds(10);
  digitalWrite( trig, LOW);
  impulseTime = pulseIn( echo, HIGH, 1500 );
  Serial.print( impulseTime ); Serial.print( " - " );
  distance_sm = impulseTime/58;
  Serial.println(distance_sm);
  delay(100);
}

But nothing hapen.

What’s wrong?

BR,
Dmitry


#2

This is a working sketch for the mBot using the ultrasonic sensor that uses the official Makeblock libraries as its basis so looking through that code may be helpful.

#include <MeMCore.h>
MeRGBLed leds(7, 2);
MeUltrasonicSensor ul(3);
double distance = 0.0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  distance = ul.distanceCm();
  Serial.println("Distance: " + String(distance));
  if (distance < 10) {
    leds.setColor(0, 150, 0, 0);    // less than 10 cm red leds
  } else if (distance < 20) {
    leds.setColor(0, 150, 150, 0);  // less than 20 cm, greater than 10 yellow leds
  } else {
    leds.setColor(0, 0, 150, 0);    // greater than 20 cm green leds
  }

  leds.show();
}

The Makeblock libraries are located here.


#3

Thank you for information.

But my question is a bit different.

I need to know why I can’t use A2 and A3 onboard pins?

BR,
Dmitry


#4

Do you have any reference resources for writing this code? If yes, please share it with us.

void loop()
{
digitalWrite( trig, HIGH);
delayMicroseconds(10);
digitalWrite( trig, LOW);
impulseTime = pulseIn( echo, HIGH, 1500 );
Serial.print( impulseTime ); Serial.print( " - " );
distance_sm = impulseTime/58;
Serial.println(distance_sm);
delay(100);
}


#5

One time I found same code in the internet.
I can’t write where it was exactly placed but you can find it by google.
This code is perfectly work with any other my Arduino (nano, Uno, Mega etc).

Is it difficult question for Makeblock system?

BR Dmitry


#6

Hmm, not sure if this will help but do you have the following information?

Your code looks similar to this code on the Arduino Playground.


#7

Thank you for information.
It’s understanding from mCore schematic.

Probably I’m wrong using mBot ultrasonic sensor like HC-SR04.

I’ll try one more time.

Thank you very much for help!

BR Dmitry


#8

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.