#include "SoftwareSerial.h" // Declare and initialize UART pins const int rx_pin = 4; const int tx_pin = 5; // Declare and initialize hall sensor pin const int hall_pin = 8; // Define pin numbers for motor driver const int dirPin = 1; // direction const int stepPin = 2; // step const int enaPin = 3; // enable // set number of steps that the motor should rotate const int steps = 16*200*2; int sum = 0; // Create new serial with specified UART pins SoftwareSerial mySerial(rx_pin , tx_pin); void setup() { // Set the pin modes of UART pins pinMode(rx_pin, INPUT); // Receiving requires input pinMode(tx_pin, OUTPUT); // Transmitting requires output // Set sensor as input pinMode(hall_pin, INPUT); // Start the serial communication at 9600 bits/sec mySerial.begin(9600); // Define all pins for motor driver as output pinMode(stepPin,OUTPUT); pinMode(dirPin,OUTPUT); pinMode(enaPin,OUTPUT); // Enable the motor driver digitalWrite(enaPin,LOW); digitalWrite(dirPin,HIGH); // Set the rotation to one particular direction } void loop() { // Perform a certain number of steps for(int x = 0; x < steps; x++) { // Generate a pulse digitalWrite(stepPin,HIGH); delayMicroseconds(700); digitalWrite(stepPin,LOW); delayMicroseconds(700); // Read the analog value of the pin and send it via serial communication int val = analogRead(hall_pin); // Average the readings over ten points if (x%10 == 0){ mySerial.println(sum/10); sum = 0; } else { sum = sum + val; } } }