Edit: Solution is found in post #3 and #4 with the buggy code in post #2
Lesson: Do not define your own digitalWrite function and ask yourself why digitalWrite does not work.
Solution: Rename your custom function to digital_write(); or something else.
I am looking for differences in digitalWrite in a libary (with Arduino.h included) to digitalWrite in the Sketch itself. If you suspect the mistake somwhere else, just tell me.
My script fails in following function in an self-written libary for Shift registers. (My code is huge so i created an abstraction able to reproduce the error)
.ino file: (only exists to spot the error)
#include <extendedoutputs.h>
ExtendedOutput exout(11, 12, 13);
//Not working code:
#define latch_pin 11
#define data_pin 12
#define clock_pin 13
byte out = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("before internal function");
delay(2000);
senddata();
Serial.println("after internal function");
delay(2000);
exout.send();
}
void senddata(){
Serial.println("Internal, Pre For");
delay(2000);
digitalWrite(latch_pin, 1);
for(int i = 0; i<8; i++){
Serial.println("Internal For");
delay(2000);
digitalWrite(clock_pin, 0);
digitalWrite(data_pin, out & (1<<(7-i)));
digitalWrite(clock_pin, 1);
}
digitalWrite(latch_pin, 1);
digitalWrite(clock_pin, 0);
digitalWrite(latch_pin, 0);
}
extendedoutputs.h file (shortened, still able to reproduce the error EDIT:: Aparently not able to reproduce the error! Error code in post #2)
#include <Arduino.h>
class ExtendedOutput{
public:
ExtendedOutput(int _latch, int _data, int _clock);
void send();
private:
byte out;
const int data_pin;
const int clock_pin;
const int latch_pin;
};
extendedoutputs.cpp file (shortened as well)
#include <extendedoutputs.h>
ExtendedOutput::ExtendedOutput(int _latch, int _data, int _clock):data_pin(_data), clock_pin(_clock), latch_pin(_latch){}
void ExtendedOutput::send(){
Serial.println("External, Pre for");
Serial.println(this->latch_pin);
delay(2000);
digitalWrite(this->latch_pin, 1);
for(int i = 0; i<8; i++){
Serial.println("External For");
delay(2000);
digitalWrite(this->clock_pin, 0);
digitalWrite(this->data_pin, !!(out & (1<<(7-i))));
digitalWrite(this->clock_pin, 1);
}
digitalWrite(this->latch_pin, 1);
digitalWrite(this->clock_pin, 0);
digitalWrite(this->latch_pin, 0);
}
The Delays are there so that the arduino prints the Serial.prints before crashing.
Serial Monitor reveals the bug:
before internal function
Internal, Pre ForInternal For
Internal For
Internal For
Internal For
Internal For
Internal For
Internal For
Internal For
after internal function
External, Pre for11
External, Pre for11
External, Pre for11
External, Pre for11
//Goes on like this 20 times, then reboots
//should be pre For once and 8 times For alone. Internal stands for the function defined in the .ino and external for the function in the .cpp
So it crashes on the first digitalWrite() in the external function, and restarts that function (also why does it do that?)
The other funtion works fine
Thanks for reading until here ![]()