Bitwrite not working on a Nano

Hello

I have a Nano driving an MCP23017 multiplexer to give me more outputs.

I use these a lot, so the setup is the same (I copy/paste the code over)

To switch a channel on, I usually use:

bitWrite(Outputvalue, #, 1);

Then jump over to a short routine to turn the Output on:

Wire.beginTransmission(0x20);
Wire.write(0x13);
Wire.write(Outputvalue);
Wire.endTransmission();

But... it doesn't work on the Nano? Works fine on other processors (Teensy, Mega etc).

I can turn the output on, but I have to actually send the value (for example 8):

Wire.beginTransmission(0x20);
Wire.write(0x13);
Wire.write(8);
Wire.endTransmission();

If Outputvalue is an integer and equal to 8, there should be no difference.

Print Outputvalue right before you use it to see if it is fact what you think.

Post a complete sketch where passing a variable equal to 8 produces a result different to passing 8 as a literal constant.

You got something else going on almost certainly, without context it is impossible to know what.

There should be no difference that can be explained by which of the Arduino boads you are using.

a7

The value is a byte (tried an int too).

I'll grab the code and upload it, but I can't see what the issue is.

OK. This is demo code to trial stuff, so it's untidy and has delays in the main loop.

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();                                                          // Servo driver board is 0x40

byte Outputvalue;                                                                                                 // Value to send to the MCP23017
byte Checksum;

//------------------------------------------------------------------------------------------------------------------------
void setup() {
  Serial.begin(9600);                                                                                             // MP3 needs 9600 baud

  pwm.begin();
  pwm.setOscillatorFrequency(27000000);
  pwm.setPWMFreq(1600);                                                                                           // This is the maximum PWM frequency

  Wire.setClock(400000);

  Wire.beginTransmission(0x20);                                                                                   // Setup for first MCP23017 controller for banks A and B as inputs
  Wire.write(0x06);                                                                                               // Set to pin direction setting mode
  Wire.write(0xFF);                                                                                               // Set controller IODIR0 to all input
  Wire.write(0xFF);                                                                                               // Set controller IODIR1 to all input
  Wire.endTransmission();

  delay(200);

  Wire.beginTransmission(0x20);                                                                                   // Setup MCP23017 controller for bank A to be inputs
  Wire.write(0x00); // IODIRA register
  Wire.write(0x00); // set all of portA to outputs
  Wire.endTransmission();

  delay(200);

  Wire.beginTransmission(0x20);                                                                                   // Setup MCP23017 controller for bank B to be outputs
  Wire.write(0x01); // IODIRB register
  Wire.write(0x00); // set all of portB to outputs
  Wire.endTransmission();


  Serial.write(0xAA); Serial.write(0x0B); Serial.write(0x01); Serial.write(0x02); Serial.write(0xB8);             // Select Flash as the drive for voice sound files
  delay(1000);
  Serial.write(0xAA); Serial.write(0x04); Serial.write(0x00); Serial.write(0xAE);                                 // Stop all music files
  delay(1000);

  Serial.write(0xAA); Serial.write(0x13); Serial.write(0x01); Serial.write(31); Serial.write(221);                // Set the volume (0-31)

  delay(200);
}



//########################################################################################################################
void loop() {

  pwm.setPWM(14, 0, 0);
  pwm.setPWM(15, 1024, 3072);                                               // Drive main motor clockwise

  delay(2000);

  pwm.setPWM(15, 0, 0);
  pwm.setPWM(14, 1024, 3072);                                               // Drive main motor anti-clockwise

  delay(2000);

  pwm.setPWM(14, 0, 0);

  Wire.beginTransmission(0x20);                                             //  Turn on small motor clockwise
  Wire.write(0x13);
  Wire.write(1);
  Wire.endTransmission();

  pwm.setPWM(11, 4096, 0);                                                  // Drive small motor

  delay(2000);

  Wire.beginTransmission(0x20);                                             //  Turn on small motor anti-clockwise
  Wire.write(0x13);
  Wire.write(2);
  Wire.endTransmission();

  delay(2000);

  pwm.setPWM(11, 0, 0);                                                     // Stop small motor

  delay(2000);

  bitWrite(Outputvalue, 8, 1);                                             //  Turn on LED             NOT WORKING
  MCP23017();

  delay(2000);

  bitWrite(Outputvalue, 8, 0);                                             //  Turn off LED             NOT WORKING
  MCP23017();

  delay(2000);

  Wire.beginTransmission(0x20);                                             //  Turn on amplifer
  Wire.write(0x13);
  Wire.write(4);
  Wire.endTransmission();

  delay(300);

  Serial.write(0xAA); Serial.write(0x07); Serial.write(0x02); Serial.write(0x00); Serial.write(1); Serial.write(180);         // Play track

  delay(4000);

  Serial.write(0xAA); Serial.write(0x04); Serial.write(0x00); Serial.write(0xAE);                                             // Stop all music files

  delay(2000);




}
//########################################################################################################################


void MCP23017() {

  Wire.beginTransmission(0x20);                                                                                   // Set the outputs
  Wire.write(0x13);                                                                                               // Address PORT B
  Wire.write(Outputvalue);                                                                                        // Value to send
  Wire.endTransmission();
}

Everything turns on and off as it should, except the LED using bitWrite.

Sorry = typo I am switching bit 3 (not 8).

I added those line in for this post (and it still doesn't work)

Can you show a small sketch that does not work ?

The bitWrite() works. You have to proof that you can do something that it does not work.

Teensy: https://github.com/PaulStoffregen/cores/blob/master/teensy/wiring.h#L85

Arduino Uno, Nano and Mega: https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Arduino.h#L111

They are macros, so they can go wrong. There is a difference.

#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))
#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet( value ,  bit)  : bitClear( value ,  bit))

The Teensy has the extra '(' and ')'. I don't know how to force a bug in the Arduino macro.

The Arduino API got the extra '(' and ')': https://github.com/arduino/ArduinoCore-API/blob/master/api/Common.h#L85
Better safe than sorry, I suppose.

Yes, this came up very recently. I'm not seeing it and just now forget whether the other case ever made sense to me, I mean if I got that settled for myself.

Oh, it was the constrain macro, I think, and yes again Inthinkmit was a good examlke of how macros be fraught.

@baffled2023 - print the value of Outputval just where you use it, to see if bitWrite() failed you.

And. Rename these macros and try both versions by replacing the bitWrite() call that goes bad:

#define bitWriteA(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit)))

#define bitWriteB(value, bit, bitvalue) ((bitvalue) ? bitSet( value ,  bit)  : bitClear( value ,  bit))

And. The bitWrite() macro passes off the "heavy lifting" to macros… I used those recently here to save someone the pain of doing her own bit manipulation, the macros are really very simple, so jettisoning them is an option.

I'm not in the lab. A few minutes with a text editor should be a way to see any problem that exists.

Still seems like something we aren't being shown, nor something we aren't seeing. Won't be the last time for me. :expressionless:

a7

Please try this on all your different boards:

void setup() {
  Serial.begin(115200);
  Serial.println("hello \n");

  byte foo = 0;

  bitWrite(foo, 7, 1);
  Serial.println(foo);

  byte bar = 0;

  byte wha = 7;
  bitWrite(bar, wha, 1);

  Serial.println(bar);
}

void loop() {
}

Share the output for any boards that don't print 128 twice.

a7

Tested it in Wokwi simulation on a Arduino Uno, Mega, Nano, ESP32, ESP32-S2, ESP32-S3, ESP32-C3, Raspberry Pi Pico, STM32.
They all print 128 twice of course. It is a simulation, but the same build environment is used as the real Arduino IDE.

baffled2023, since we have not the slightest indication that something is wrong, and a bad harddrive or a glitch in the compiler is almost impossible, could it be that you made a mistake ?

The syntax of bitwrite(arg1, arg2, arg3); macro is:
where:

arg1 : Symbolic name or Numeric Address of Destination Port/Object or Variable 
like: PORTD or byte y.

arg2 : Bit position of arg1 at which the value of arg3 is to be written

arg3 : 0 (for LOW) or 1 (for HIGH)

What is Outputvlaue at the place of arg1 of bitWrite(arg1, arg2, arg3)?. The identifier name should be meaningful and sensible. The name Outputvalue is very close to arg3?

Sorry for the delay... had a slight accident :roll_eyes:

Problem solved, although I don't understand how
Tried a different Nano and it worked?

Explain that?

Anyway, the rogue Nano is now in the bin