Can I get some help finding a bug in my arduino code? (Solved) - Page 1
Forum section: electronics / general

author: EvilGeniusSkis
author_rank: Regular Contributor
timestamp: October 25, 2018, 10:41:35 pm
content: my project is a pan/tilt setup controlled by a Wii nunchuk. The bug is between lines 104 and 114, which are supposed to toggle sending the raw nunchuk values over serial with a button press, but nothing happens. I tested the button using the "button"example sketch and after changing the pin mapping to how i had it wired up, it worked.My code, which compiles fine. the lines of slashes are just a grouping thing that helps me with organisation,Code:///////////////////////////////////////////// Load relevent libraries#include <NintendoExtensionCtrl.h>#include <Servo.h>/////////////////////////////////////////////Declare servos and other I\OServo TiltServo;Servo PanServo;const int ErrorLED = 10;const int LaserPin = 11;const int DebugButton = 13;//////////////////////////////////////////// Declare variablesNunchuk nchuk;int PanVal;int TiltVal;int LaserMessageState = LOW;int DebugState = LOW;///////////////////////////////////////////Sets ErrorLED and LaserPin as outputs, DebugButton as an input, sets the servo pins and starts talking to the nunchuk and computervoid setup() {pinMode(ErrorLED, OUTPUT);pinMode(LaserPin, OUTPUT);//pinMode(DebugButton, INPUT);Serial.begin(9600);nchuk.begin();TiltServo.attach(9);PanServo.attach(8);/////////////////////////////////////////////////////// If the nunchuk isn't connected, send out a string informing the user and flash the error ledwhile (!nchuk.connect()) {Serial.println("Nunchuk not detected!");digitalWrite(ErrorLED, HIGH);delay(500);digitalWrite(ErrorLED, LOW);delay(500);}//////////////////////////////////////////////////////////////Tells user the laser turret is ready and turns off the error ledSerial.println("----- Laser Turret Initialized -----");digitalWrite(ErrorLED, LOW);///////////////////////////////////////////////////////////}void loop() {boolean success = nchuk.update(); // Get new data from the controller///////////////////////////////////////////////////////////// If the nunchuk isn't connected, send out a string informing the user and flash the error ledif (!success) {Serial.println("Controller disconnected!");digitalWrite(ErrorLED, HIGH);delay(500);digitalWrite(ErrorLED, LOW);delay(500);}//////////////////////////////////////////////////////////////// if the nunchuck is connected, turn off the error led and allow main code to runelse {digitalWrite(ErrorLED, LOW);//////////////////////////////////////////////////When the Z button is pressed, turn the laser on and send a string once, otherwhise do nothingboolean zButton = nchuk.buttonZ();if (zButton == true) {digitalWrite(LaserPin, HIGH);if (LaserMessageState == LOW) {Serial.println("Imma Firin Ma Laser!!!");LaserMessageState = HIGH;}}else if (zButton == false) {digitalWrite (LaserPin, LOW);LaserMessageState = LOW;}////////////////////////////////////////////////When the C button is pressed, use the joystick to control the servosboolean cButton = nchuk.buttonC();if(cButton == true){int joyY = nchuk.joyY();int joyX = nchuk.joyX();TiltVal = joyY;PanVal = joyX;TiltVal = map(TiltVal, 34, 231, 100, 0);PanVal = map(PanVal, 20, 220, 0, 180);TiltServo.write(TiltVal);PanServo.write(PanVal);}//////////////////////////////////////////////////Use the accelerometers to controll the servoselse {int accelX = nchuk.accelX();int accelY = nchuk.accelY();TiltVal = accelY;PanVal = accelX;TiltVal = map(TiltVal, 290, 500, 100, 0);PanVal = map(PanVal, 290, 710, 0, 180);TiltServo.write(TiltVal);PanServo.write(PanVal);}}////////////////////////////////////////////////// Toggles sending all the data from the nunchuk to the computer, the button is nomaly highif (DebugState == HIGH){nchuk.printDebug();}if (DebugButton == LOW and DebugState == LOW) {DebugState = HIGH;delay(1000);}if(DebugButton == LOW and DebugState == HIGH){DebugState = LOW;delay(1000);}////////////////////////////////////////////////}I've also tried the relevant lines asCode:if(DebugButton == LOW) and (DebugState == HIGH){But that gives a compiler error saying "expected identifier before '(' token" while highlighting line 111.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 25, 2018, 11:15:09 pm
content: C will not understand 'and' the way you do. 'and' = '&&' in C. and dont forget the bigger extra bracket for all the conditions.

author: Mr.B
author_rank: Unknown
timestamp: October 26, 2018, 12:16:17 am
content: Code:if((DebugButton == LOW) && (DebugState == HIGH)){

author: cv007
author_rank: Frequent Contributor
timestamp: October 26, 2018, 01:30:02 am
content: DebugButton == LOWDebugButton will never equal LOW (I assume is 0) as DebugButton is 13 -> 13 != 0 (and also code probably optimized away because of that)you most likely want digitalRead(DebugButton) == LOW== has higher precedence than 'and' (valid for c++) so the parens are not needed but does not hurt (and is always a good idea when in doubt)edit-//pinMode(DebugButton, INPUT);don't know why that is commented out, and it may not matter, but I would run that code even if not needed plus you also have the opportunity to use INPUT_PULLUP where you wouldn't need an external pullup resistor for the switchI shouldn't start rewriting your code (because there is never an end), but your two if statements can be reduced to-if(digitalRead(DebugButton) == LOW){debugState = not debugState;delay(1000);}

author: EvilGeniusSkis
author_rank: Regular Contributor
timestamp: October 26, 2018, 03:57:42 am
content: Quote from: Mechatrommer on October 25, 2018, 11:15:09 pmI probably wouldn’t have figured that one out because “and” turned green like “if” and “else”.Quote from: cv007 on October 26, 2018, 01:30:02 amSeeing your suggested optimization made me realize the problem, I forgot digitalRead so it was never checking the button’s state in the first place.The fix was:Code:if ((digitalRead(DebugButton) == LOW) && (DebugState == LOW))The 13 in relation to DebugButton is what pin it’s connected to.As for pinMode(DebugButton, LOW) being commented out, that was from trying to debug the debug.I’ll rember INPUT_PULLUP in the future.