Devices & Components
Arduino Nano
Arduino Micro
Arduino Uno Rev3
SS49E Hall-effect linear sensor
Single/Dual Digital Potentiometer with SPI™ Interface MCP41050
Project description
Code
Analog output via digiPOTs
arduino
1#include <SPI.h> 2 3#define CS_RUDDER 10 4#define CS_LEFT 8 5#define CS_RIGHT 9 6#define SENSOR_LEFT_PEDAL A6 7#define SENSOR_RIGHT_PEDAL A5 8#define SENSOR_RUDDER_LEFT A0 9#define SENSOR_RUDDER_RIGHT A1 10 11int val_left, val_right, remap_left, remap_right, rudder, 12 val_brake_left, val_brake_right, remap_brake_left, remap_brake_right; 13 14int tuning_rudder = 128; 15// int tuning_left_brake = 255; 16// int tuning_right_brake = 255; 17 18void setup() { 19 Serial.begin(9600); 20 SPI.begin(); 21 pinMode (CS_RUDDER, OUTPUT); 22 pinMode (CS_LEFT, OUTPUT); 23 pinMode (CS_RIGHT, OUTPUT); 24} 25 26void loop() { 27 28 // -------------------------------------------------------- 29 // RUDDER PEDALS -128 --- x --- 128 30 // -------------------------------------------------------- 31 32 // Read and remap sensor RUDDER LEFT 33 val_left = analogRead(SENSOR_RUDDER_LEFT); 34 if (val_left <= 950) { 35 remap_left = map(val_left, 30, 870, -128, 0); // -40 36 } 37 else remap_left = 0; 38 // Set limitations RUDDER LEFT 39 if (remap_left > 0) remap_left = 0; 40 if (remap_left <= -128) remap_left = -128; 41 42 // Read and remap sensor RUDDER RIGHT 43 val_right = analogRead(SENSOR_RUDDER_RIGHT); 44 if (val_right <= 950) { 45 remap_right = map(val_right, 0, 820, 132, 0); // -50 46 } 47 else remap_right = 0; 48 //Set limitations RUDDER RIGHT 49 if (remap_right < 0) remap_right = 0; 50 if (remap_right >= 127) remap_right = 127; 51 52 rudder = remap_left + remap_right; 53 54 55 if (remap_left == 0) { 56 tuning_rudder = tuning_rudder + 128; 57 } 58 else { 59 tuning_rudder = abs(tuning_rudder - 128); 60 } 61 62 // -------------------------------------------------------- 63 // BRAKING 64 // -------------------------------------------------------- 65 66 val_brake_left = analogRead(SENSOR_LEFT_PEDAL); 67 68 if (val_brake_left <= 328) { 69 remap_brake_left = map(val_brake_left, 200, 328, 0, 255); 70 } 71 else remap_brake_left = 255; 72 if (remap_brake_left < 0) remap_brake_left = 0; 73 74 val_brake_right = analogRead(SENSOR_RIGHT_PEDAL); 75 if (val_brake_right <= 328) { 76 remap_brake_right = map(val_brake_right, 200, 328, 0, 255); 77 } 78 else remap_brake_right = 255; 79 if (remap_brake_right < 0) remap_brake_right = 0; 80 81/* 82 // ARDUINO SETUP 83 84 // Neutral sensors setup 85 Serial.print(val_left); 86 Serial.print(" | " ); 87 Serial.println(val_right); 88 delay(400); 89 90 // Output setup 91 Serial.print(remap_brake_left); 92 Serial.print(" | " ); 93 Serial.println(remap_brake_right); 94 Serial.print(" | " ); 95 Serial.print(rudder); 96 Serial.print(" | " ); 97 Serial.println(tuning_rudder); 98 delay(400); 99*/ 100 101 // -------------------------------------------------------- 102 // SEND DATA TO digiPOTs 103 // -------------------------------------------------------- 104 105 digitalWrite(CS_RUDDER, LOW); 106 SPI.transfer(0b00010001); 107 SPI.transfer(tuning_rudder); 108 digitalWrite(CS_RUDDER, HIGH); 109 delay(5); // Delay for data time smoothing 110 111 digitalWrite(CS_LEFT, LOW); 112 SPI.transfer(0b00010001); 113 SPI.transfer(remap_brake_left); 114 digitalWrite(CS_LEFT, HIGH); 115 delay(5); 116 117 digitalWrite(CS_RIGHT, LOW); 118 SPI.transfer(0b00010001); 119 SPI.transfer(remap_brake_right); 120 digitalWrite(CS_RIGHT, HIGH); 121 delay(5); 122}
Downloadable files
DigiPOT circuit
DigiPOT circuit

For Arduino Joystick Library
For Arduino Joystick Library

TWCS circuit
TWCS circuit

For Arduino Joystick Library
For Arduino Joystick Library

TWCS circuit
TWCS circuit

DigiPOT circuit
DigiPOT circuit

Comments
Only logged in users can leave comments