I'm trying to use the WiiNunchuck with a WiiChuck to be able to eventually control a motor for an electric skateboard. I want to be able to control a square wave and a duty cycle in order to control the speed of the skateboard when the motor is attached. Initially, I just want to be able to control the duty cycle with the Nunchuck first so that the square wave can adjust accordingly. I've searched online but haven't found any luck with this. Some help would be much appreciated. Here is some code I initially wrote to control the square wave using a potentiometer when connected to an oscilloscope. Now I want to be able to see the square wave on the serial plotter instead.
// Connect potentiometer to pin A0
const int potPin = A0;
// Set output pin to pin 9
const int outPin = 9;
int Square;
// Set maximum value read from analogRead
const int maxValue = 1023;
// Duty cycle
float d;
// Frequency value (Hz) - Can be changed
float f = 100;
// Time management
unsigned long prevTime = 0;
// Time increment
float i = 0;
unsigned int OnTime;
unsigned int OffTime;
void setup() {
pinMode(outPin, OUTPUT);
// Serial comms for plotter
// Baud rate of 9600
Serial.begin(115200);
}
void loop() {
// Read potentiometer value
int sensorValue = analogRead(potPin);
// Duty cycle calculation
// d = value read / max value
d = (float)sensorValue - 300 / 300;
Serial.println("Duty");
Serial.println(d);
OnTime=d/f*1000*1000;
Serial.println("OnTime");
Serial.println(OnTime);
OffTime=(1-d)/f*1000*1000;
Serial.println("OffTime");
Serial.println(OffTime);
// Then output is high
digitalWrite(outPin, HIGH);
delayMicroseconds(OnTime);
digitalWrite(outPin,LOW);
delayMicroseconds(OffTime);
}