Hi! I will try to be as informational and concise as possible so as to respect everyone’s time and knowledge.
I am working on building a robotic security camera prop. I have 2 servos, powered externally, connected to an Arduino Nano. The servos are responsible for up/Down and Left/Right movement. To control the servo movement, I am using a Nintendo Wii Nunchuk and an library to interface it with the Nano.
I have successfully managed to get the servos to move based on the joystick’s position and have also gotten the built-in LED to blink while everything else is running:
#include <Servo.h>
#include <Wire.h>
#include "Nunchuk.h"
const int ledPin = LED_BUILTIN;
Servo leftright;
Servo updown;
int pos1 = 0;
int pos2 = 0;
int ledState = LOW;
unsigned long interval = 1000;
unsigned long previousMillis = 0;
void setup() {
Wire.begin();
nunchuk_init_power();
nunchuk_init();
leftright.attach(12);
updown.attach(8);
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (nunchuk_read()) {
pos1 = map(nunchuk_joystickX(), -98, 102, 0, 180);
pos2 = map(nunchuk_joystickY(), 94, -109, 180, 0);
leftright.write(pos1);
updown.write(pos2);
}
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
}
else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
delay(20);
}
Here is what I’m struggling with:
I’d like to press the C button on the Nunchuk and have the LED blink quicker. I’d also like to press the Z button and have the Left/Right servo sweep back and forth from 0-180 degrees. However, the methods I’ve tried to implement this in code have not worked. I need help understanding what I’m doing wrong and how it should be coded.
As far as the library elements go:
nunchuk_buttonZ() and nunchuk_buttonC() return either 0 or 1, depending on if the button is pressed or not.
This is the code that does not work:
#include <Servo.h>
#include <Wire.h>
#include "Nunchuk.h"
const int ledPin = LED_BUILTIN;
Servo leftright;
Servo updown;
int pos1 = 0;
int pos2 = 0;
int pos3 = 90;
int ledState = LOW;
int z = nunchuk_buttonZ;
unsigned long interval = 1000;
unsigned long previousMillis = 0;
void setup() {
Wire.begin();
nunchuk_init_power();
nunchuk_init();
leftright.attach(12);
updown.attach(8);
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (nunchuk_read()) {
if (z == 1){
for (pos3 = 0; pos3 <= 180; pos3 += 1) {
leftright.write(pos3);
delay(15);
}
for (pos3 = 180; pos3 >= 0; pos3 -= 1) {
leftright.write(pos3);
delay(15);
}
}
else {
pos1 = map(nunchuk_joystickX(), -98, 102, 0, 180);
pos2 = map(nunchuk_joystickY(), 94, -109, 180, 0);
leftright.write(pos1);
updown.write(pos2);
}
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
}
else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
delay(20);
}
}
As you can see, I tried to insert a statement to check to see if the Z button was pressed and to have the servo sweep if it was. However, when I press the Z button, nothing happens.
What am I doing wrong and how can the code be changed to have the servo sweep when Z is pressed and the LED to blink faster when C is pressed?