Track Number of Turns and Changing Color


#21

It has to have a break in between the beeps. So there has to be a pause in between each beat. Which explains why I have to have .25 seconds after each time and the repeat block.


#22

OK, sorry for the delayed response, I was REALLY busy! I am now working on the code for you!


#23

Here is a code:
(Full image)


(Top half)


(Bottom half)

Here is the Ardunio C:

// generated by Best_codes and mBlock5 for mBot
// codes make you happy

#include <MeMCore.h>
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

MeLEDMatrix ledMtx_1(1);
unsigned char drawBuffer[16];
unsigned char *drawTemp;
MeLineFollower linefollower_1(1);
MeUltrasonicSensor ultrasonic_3(3);
MeDCMotor motor_9(9);
MeDCMotor motor_10(10);
MeRGBLed rgbled_7(7, 2);
MeBuzzer buzzer;

float linevar = 0;
float TurnCount = 0;

void do_nothing(){

  drawTemp = new unsigned char[16]{0,0,60,126,126,60,0,0,0,0,60,126,126,60,0,0};
  memcpy(drawBuffer, drawTemp, 16);
  free(drawTemp);
  ledMtx_1.drawBitmap(0, 0, 16, drawBuffer);

}
void move(int direction, int speed) {
  int leftSpeed = 0;
  int rightSpeed = 0;
  if(direction == 1) {
    leftSpeed = speed;
    rightSpeed = speed;
  } else if(direction == 2) {
    leftSpeed = -speed;
    rightSpeed = -speed;
  } else if(direction == 3) {
    leftSpeed = -speed;
    rightSpeed = speed;
  } else if(direction == 4) {
    leftSpeed = speed;
    rightSpeed = -speed;
  }
  motor_9.run((9) == M1 ? -(leftSpeed) : (leftSpeed));
  motor_10.run((10) == M1 ? -(rightSpeed) : (rightSpeed));
}

void _delay(float seconds) {
  long endTime = millis() + seconds * 1000;
  while(millis() < endTime) _loop();
}

void setup() {
  ledMtx_1.setColorIndex(1);
  ledMtx_1.setBrightness(6);
  rgbled_7.fillPixelsBak(0, 2, 1);
  TurnCount = 0;
  while(1) {
      linevar = linefollower_1.readSensors();
      if(linevar > 3){
        if((ultrasonic_3.distanceCm() > 7)  &&  (!((TurnCount > 2)))){

          move(1, 100 / 100.0 * 255);

        }else{
          if(!((TurnCount > 2))){

            move(4, 27 / 100.0 * 255);
            _delay(2);
            move(4, 0);
            _delay(1);
            for(int count3=0;count3<5;count3++){

              rgbled_7.setColor(0, 0, 255, 0);
              rgbled_7.show();

              buzzer.tone(147, 1 * 1000);
              _delay(0.02);
              _delay(0.25);
            }
            TurnCount += 1;

          }
          if(TurnCount > 2){
            for(int count4=0;count4<4;count4++){

              rgbled_7.setColor(0, 255, 0, 0);
              rgbled_7.show();

              buzzer.tone(294, 1 * 1000);
              _delay(0.02);
              _delay(1);
            }
            while(1) {
                do_nothing();

                _loop();
            }

          }

        }

      }else{
        while(1) {

            rgbled_7.setColor(0, 0, 0, 0);
            rgbled_7.show();

            _loop();
        }

      }

      _loop();
  }

}

void _loop() {
}

void loop() {
  _loop();
}

Thanks for your time,
Best_codes


#24

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