Advertisement
timor2542

[i-Duino R4] BO1 Motor Speed and Direction Control with ZX-MOTOR2A

Mar 7th, 2024 (edited)
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.87 KB | Source Code | 0 0
  1. #define POTV_PIN A0 // ZX-POTV pin.
  2. #define BUTTON_PIN 12 // ZX-BUTTON pin.
  3. #define DIR_PIN 7  // Direction pin for ZX-MOTOR2A.
  4. #define PWM_PIN 3  // PWM pin for ZX-MOTOR2A.
  5. int cnt = 0;  // For count value while you're holding button.
  6. int dir = 0;  // For motor direction driver state.
  7. void setup() {
  8.   pinMode(DIR_PIN, OUTPUT); // Set Direction pin to OUTPUT DIGITAL MODE
  9.   pinMode(BUTTON_PIN, INPUT); // Set ZX-BUTTON pin pin to INPUT DIGITAL MODE
  10.   pinMode(LED_BUILTIN, OUTPUT); // Set LED on i-Duino R4 pin to OUTPUT DIGITAL MODE
  11.  
  12.   analogWriteResolution(12);  // Write at Value Range 0-4095.
  13.   analogReadResolution(14);   // Read at Value Range 0-16383.
  14.  
  15.   Serial.begin(9600); // Set baud rate 9600 bps.
  16.   while (!Serial) { delay(100); }  // Wait for native USB.
  17.   Serial.println(F("ZX-MOTOR2A Speed and Direction Control")); // Greeting Message.
  18. }
  19.  
  20. void loop() {
  21.   if (digitalRead(BUTTON_PIN) == 0) { // If hold button.
  22.     cnt++;  // Increase value
  23.     if (cnt >= 5) { // If value is more than or equal to 5
  24.       dir ^= 1;  // Change state for motor direction driver by XOR (Exclusive OR) method.
  25.       cnt = 0; // Reset value
  26.     }
  27.   }
  28.   else{  // Unless hold button.
  29.     cnt = 0; // Reset value
  30.   }
  31.  
  32.   // Print count value section
  33.   Serial.print("Count: ");  
  34.   Serial.println(cnt, DEC);
  35.  
  36.   // Input section
  37.   int input_raw = analogRead(POTV_PIN); // Read POTV raw value (Range 0-16383)
  38.   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.
  39.   Serial.print("Input Raw: "); // Print input raw.
  40.   Serial.print(input_raw, DEC); // Show input raw value.
  41.   Serial.print(", Read Voltage: ");  // Print input voltage.
  42.  
  43.   // Since input_voltage is float, so the second argument must be shows the amount of decimals.
  44.   // This example will be show only 2 decimals.
  45.   Serial.println(input_voltage, 2);
  46.  
  47.   int output_raw = map(input_raw, 0, 16383, 0, 4095);  // Convert raw range value
  48.   digitalWrite(DIR_PIN, dir); // Drive motor direction current state.
  49.   digitalWrite(LED_BUILTIN, dir); // Drive LED to show motor direction current state.
  50.   analogWrite(PWM_PIN, output_raw); // Write PWM with output raw
  51.   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.
  52.   Serial.print("Output Raw: "); // Print output raw.
  53.   Serial.print(output_raw, DEC); // Show output raw value.
  54.   Serial.print(", Write Voltage: "); // Print output voltage.
  55.  
  56.   // Since output_voltage is float, so the second argument must be shows the amount of decimals.
  57.   // This example will be show only 2 decimals.
  58.   Serial.print(output_voltage, 2);
  59.  
  60.   Serial.print(", Direction: "); // Print motor direction current state.
  61.   Serial.println(dir, DEC); // Show motor direction current state.
  62.  
  63.   Serial.println();
  64.   delay(100);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement