Using a Wii Nunchuck with WiiChuck.h

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);
    
}

Once the serial data is scrolling down your serial monitor, click the "plotter" icon in the top-right of the IDE to see your waveform. The format for displaying a graph is something like this:

  Serial.print("Trace1:");
  Serial.print(Data1);
  Serial.print(","); // delimiter
  Serial.print("Trace2:");
  Serial.println(Data2);

OK great, I'll give that a go. Will that display purely a square wave then? I was initially only able to see a square wave when hooked up to the oscilloscope, will this still be the case or can it be displayed on the plotter?

The serial line plotter will not replace an oscilloscope.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.