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.
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:
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.
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?