Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define POTV_PIN A0 // ZX-POTV pin.
- #define BUTTON_PIN 12 // ZX-BUTTON pin.
- #define DIR_PIN 7 // Direction pin for ZX-MOTOR2A.
- #define PWM_PIN 3 // PWM pin for ZX-MOTOR2A.
- int cnt = 0; // For count value while you're holding button.
- int dir = 0; // For motor direction driver state.
- void setup() {
- pinMode(DIR_PIN, OUTPUT); // Set Direction pin to OUTPUT DIGITAL MODE
- pinMode(BUTTON_PIN, INPUT); // Set ZX-BUTTON pin pin to INPUT DIGITAL MODE
- pinMode(LED_BUILTIN, OUTPUT); // Set LED on i-Duino R4 pin to OUTPUT DIGITAL MODE
- analogWriteResolution(12); // Write at Value Range 0-4095.
- analogReadResolution(14); // Read at Value Range 0-16383.
- Serial.begin(9600); // Set baud rate 9600 bps.
- while (!Serial) { delay(100); } // Wait for native USB.
- Serial.println(F("ZX-MOTOR2A Speed and Direction Control")); // Greeting Message.
- }
- void loop() {
- if (digitalRead(BUTTON_PIN) == 0) { // If hold button.
- cnt++; // Increase value
- if (cnt >= 5) { // If value is more than or equal to 5
- dir ^= 1; // Change state for motor direction driver by XOR (Exclusive OR) method.
- cnt = 0; // Reset value
- }
- }
- else{ // Unless hold button.
- cnt = 0; // Reset value
- }
- // Print count value section
- Serial.print("Count: ");
- Serial.println(cnt, DEC);
- // Input section
- int input_raw = analogRead(POTV_PIN); // Read POTV raw value (Range 0-16383)
- float input_voltage = ((float)input_raw * 5.0f) / 16383.0f; // Convert from range 0.0-16383.0 to 0.0-5.0 into float and save in variable.
- Serial.print("Input Raw: "); // Print input raw.
- Serial.print(input_raw, DEC); // Show input raw value.
- Serial.print(", Read Voltage: "); // Print input voltage.
- // Since input_voltage is float, so the second argument must be shows the amount of decimals.
- // This example will be show only 2 decimals.
- Serial.println(input_voltage, 2);
- int output_raw = map(input_raw, 0, 16383, 0, 4095); // Convert raw range value
- digitalWrite(DIR_PIN, dir); // Drive motor direction current state.
- digitalWrite(LED_BUILTIN, dir); // Drive LED to show motor direction current state.
- analogWrite(PWM_PIN, output_raw); // Write PWM with output raw
- float output_voltage = ((float)output_raw * 5.0f) / 4095.0f; // Convert from range 0.0-4095.0 to 0.0-5.0 into float and save in variable.
- Serial.print("Output Raw: "); // Print output raw.
- Serial.print(output_raw, DEC); // Show output raw value.
- Serial.print(", Write Voltage: "); // Print output voltage.
- // Since output_voltage is float, so the second argument must be shows the amount of decimals.
- // This example will be show only 2 decimals.
- Serial.print(output_voltage, 2);
- Serial.print(", Direction: "); // Print motor direction current state.
- Serial.println(dir, DEC); // Show motor direction current state.
- Serial.println();
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement