# Rev Up the Engine

The Rev Up the Engine project is a fun project that imitates the revving of an engine through software and hardware. It has both visual and physical interaction modules. There is the Button Module for the user to interact and imitate a gas pedal. There are also the [Buzzer Module](https://docs.acrome.net/electronics/add-on-modules/buzzer-module) and the [RGB LED Module](https://docs.acrome.net/electronics/add-on-modules/rgb-led-module) as rev limiter sound and rev level indicator.

**About Tools and Materials:**

[SMD Red](https://docs.acrome.net/electronics/smd-red) ([Purchase Here](https://www.robotshop.com/products/acrome-smd-red-smart-brushed-motor-driver-with-speed-position-and-current-control-modes))

[SMD USB Gateway](https://docs.acrome.net/electronics/gateway-modules/usb-gateway-module) ([Purchase Here](https://www.robotshop.com/products/acrome-usb-gateway-module-acrome-smd-products))

[Arduino Gateway Module](https://docs.acrome.net/electronics/gateway-modules/arduino-gateway-module) ([Purchase Here](https://www.robotshop.com/products/acrome-arduino-gateway-shield-module-acrome-smd-products))

[RGB LED Module](https://docs.acrome.net/electronics/add-on-modules/rgb-led-module) ([Purchase Here](https://www.robotshop.com/products/acrome-rgb-led-add-on-module-acrome-smd-products))

[Button Module](https://docs.acrome.net/electronics/add-on-modules/button-module) ([Purchase Here](https://www.robotshop.com/products/acrome-button-add-on-module-acrome-smd-products?pr_prod_strat=e5_desc\&pr_rec_id=e23ece12f\&pr_rec_pid=8120246796449\&pr_ref_pid=8121226592417\&pr_seq=uniform))

[BDC Motor](https://docs.acrome.net/electronics/electrical-motors/brushed-dc-motors-bdc) ([Purchase Here](https://www.robotshop.com/products/acrome-12v-brushed-dc-motor-with-built-in-encoder-100-rpm-speed))

[Buzzer Module](https://docs.acrome.net/electronics/add-on-modules/buzzer-module) ([Purchase Here](https://www.robotshop.com/products/acrome-buzzer-sound-add-on-module-acrome-smd-products))

## **Step 1: Hardware & Software Overview** <a href="#step-1-hardware-and-software-overview" id="step-1-hardware-and-software-overview"></a>

**Project Key Components**

1. [**SMD**](https://docs.acrome.net/electronics/smd-red)

   The SMD acts as a bridge between the script and the modules. It is responsible for interpreting the commands sent by the script and translating them into actions that read input from the [Button Module](https://docs.acrome.net/electronics/add-on-modules/button-module) and actuate the BDC motor, the [RGB LED Module](https://docs.acrome.net/electronics/add-on-modules/rgb-led-module) and the [Buzzer Module](https://docs.acrome.net/electronics/add-on-modules/buzzer-module).
2. [BDC Motor](https://docs.acrome.net/electronics/electrical-motors/brushed-dc-motors-bdc)&#x20;

   The 100 RPM BDC Motor with Encoder is used to imitate an engine running. The speed of the motor can be controlled precisely thanks to the built-in encoder.
3. [**Button Module**](https://docs.acrome.net/electronics/add-on-modules/button-module)

   The Button Module is used as an physical interaction device for the user. It imitates a gas pedal, revving the engine while it is pressed.
4. [RGB LED Module](https://docs.acrome.net/electronics/add-on-modules/rgb-led-module)\
   Changes colors based on engine speed, acting as a visual rev limiter indicator.
5. [Buzzer Module](https://docs.acrome.net/electronics/add-on-modules/buzzer-module)\
   Produces revving sounds that match engine speed for a realistic experience.
6. [**SMD Libraries**](https://docs.acrome.net/software/libraries)

   The SMD library is at the heart of the application. It communicates with the SMD using a specific communication protocol, sending commands to read the [Button Module](https://docs.acrome.net/electronics/add-on-modules/button-module) and actuate the BDC motor, also the other modules.

**Project Key Features**

* **Dynamic Engine Revving**

  The project allows the user to use the modules to interact and imitate an engine. It is a great project for seeing the results of the physical interaction. Holding the [Button Module](https://docs.acrome.net/electronics/add-on-modules/button-module) will result in rev increase and releasing it will decrease the rev.
* **Real-time Speed Control**

  The script continuously can store and monitor the current velocity level of the motor. The user can use these values to modify the project.
* **Customizable Speed Profile**

  The user can customize the step time or rev increase amount per step. Also, the [RGB LED Module](https://docs.acrome.net/electronics/add-on-modules/rgb-led-module) can be used to emit different light levels and colors.

## **Step 2: Assemble**

**Getting Started**

1. **Hardware Setup**
   * Connect the SMD to the PC or Arduino board using [USB Gateway Module](https://acrome.gitbook.io/acrome-smd-docs/electronics/gateway-modules/usb-gateway-module) or [Arduino Gateway Module](https://acrome.gitbook.io/acrome-smd-docs/electronics/gateway-modules/arduino-gateway-module).
   * Connect the 100 RPM BDC Motor with Encoder to the motor ports of the SMD and the [Button Module](https://docs.acrome.net/electronics/add-on-modules/button-module), the [RGB LED Module](https://docs.acrome.net/electronics/add-on-modules/rgb-led-module) and the [Buzzer Module](https://docs.acrome.net/electronics/add-on-modules/buzzer-module) to the SMD using an RJ-45 cable.
   * Make sure that the SMD is powered and all connections are correct.

#### Project Wiring Diagram

<figure><img src="https://1077748559-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LuxEcL3mxZNc5Aa92N6%2Fuploads%2F6VEsanDtEmONUUBDoz93%2FRev%20Up%20the%20Engine1.png?alt=media&#x26;token=538b3e6a-4170-49ab-a030-435c2d346fee" alt=""><figcaption></figcaption></figure>

## Step 3: Run & Test

1. **Run the Script**
   * Execute the script, initiating the Rev Up the Engine project.
   * Press or hold the [Button Module](https://docs.acrome.net/electronics/add-on-modules/button-module) to see the engine revving and other visual experiences.
2. **Experience the Engine Revving**
   * Observe how the motor speed increases gradually while the button is held down.
   * Release the button and notice the motor speed decreasing until it comes to a stop.
3. **Customize and Experiment**
   * Experiment with different parameter values in the script to customize the speed profile.
   * Explore additional features, such as the buzzer sound and frequency and RGB LED colors and intensities.

## Codes:

{% tabs %}
{% tab title="Python Code" %}
{% code lineNumbers="true" %}

```python
from smd.red import*
import time
from serial.tools.list_ports import comports
from platform import system

def USB_Port():
	ports = list(comports())
	usb_names = {
		"Windows": ["USB Serial Port"],
		"Linux": ["/dev/ttyUSB"],
		"Darwin": [
			"/dev/tty.usbserial",
			"/dev/tty.usbmodem",
			"/dev/tty.SLAB_USBtoUART",
			"/dev/tty.wchusbserial",
			"/dev/cu.usbserial",
            		"/dev/cu.usbmodem",
			"/dev/cu.SLAB_USBtoUART",
			"/dev/cu.wchusbserial",
		]
	}
	
	os_name = system()
	if ports:
		for port, desc, hwid in sorted(ports):
			if any(name in port or name in desc for name in usb_names.get(os_name, [])):
				return port
		print("Current ports:")
		for port, desc, hwid in ports:
			print(f"Port: {port}, Description: {desc}, Hardware ID: {hwid}")
	else:
		print("No port found")
	return None
	
port = USB_Port()
m = Master(port)
m.attach(Red(0))

print(m.scan_modules(0))

m.set_operation_mode(0, 0)
m.enable_torque(0, True)

button_prev = 0
pwm = 0

while True:
    time.sleep(0.05)
    button = m.get_button(0, 1)
    print(button)

    if button != None:
            if button == 1:
                if button_prev == 0:  # Button was just pressed
                    for pwm in range(51):
                        m.set_duty_cycle(0, pwm)
                        time.sleep(0.1)
                        red_value = int(pwm * 255 / 50)
                        green_value = int((50 - pwm) * 255 / 50)
                        light = m.set_rgb(0, 1, red=red_value, green=green_value, blue=0)
                        if pwm == 50:
                             m.set_buzzer(0, 1, 1000)  # Activate the buzzer
                    button_prev = 1
            elif button == 0:
                if button_prev == 1:  # Button was just released
                    for pwm in range(50, -1, -1):
                        m.set_duty_cycle(0, pwm)
                        time.sleep(0.1)
                        red_value = int(pwm * 255 / 50)
                        green_value = int((50 - pwm) * 255 / 50)
                        light = m.set_rgb(0, 1, red=red_value, green=green_value, blue=0)
                        if pwm == 0:
                            m.set_buzzer(0, 1, 0)  # Deactivate the buzzer

                    button_prev = 0
```

{% endcode %}
{% endtab %}

{% tab title="Arduino Code" %}
{% code lineNumbers="true" %}

```cpp
#include <Acrome-SMD.h>

#define ID 0
#define BAUDRATE 115200

Red master(ID, Serial, BAUDRATE);

bool button_prev = false;
int pwm = 0;

void setup() {
    master.begin();
    master.scanModules();
    master.setOperationMode(0);
    master.torqueEnable(1);
}

void loop() {
    bool button = master.getButton(5);

    if (button) {
        if (!button_prev) {  
            for (pwm = 0; pwm <= 50; pwm++) {
                master.setpoint(0, pwm);
                delay(100);
                int red_value = pwm * 255 / 50;
                int green_value = (50 - pwm) * 255 / 50;
                master.setRGB(5, red_value, green_value, 0); 
                if (pwm == 50) {
                    master.setBuzzer(5, 1000);
                }
            }
            button_prev = true;
        }
    } else {
        if (button_prev) { 
            for (pwm = 50; pwm >= 0; pwm--) {
                master.setpoint(0, pwm); 
                delay(100);
                int red_value = pwm * 255 / 50;
                int green_value = (50 - pwm) * 255 / 50;
                master.setRGB(5, red_value, green_value, 0); 
                if (pwm == 0) {
                    master.setBuzzer(5, 0);
                }
            }
            button_prev = false;
        }
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}
