Hello, thanks for your help. Oddly it turned out my breadboard was broken somehow and I managed to fix the problem by replacing it. Now the code works if I put it in the loop. And this is the one:
//Debug
Serial.println("Loop");
byte inputFromICP = 0;
digitalWrite(LATCH, 0);
digitalWrite(CLOCK, 0);
digitalWrite(CLOCK, 1);
digitalWrite(LATCH, 1);
for(j = 0; j < 8; j++)
{
valueFromSR = digitalRead(DATA);
if(valueFromSR)
{
int a = (1 << j);
inputFromICP = inputFromICP | a;
}
digitalWrite(CLOCK, LOW);
digitalWrite(CLOCK, HIGH);
}
if(switchVar != inputFromICP)
{
switchVar = inputFromICP;
//Debug
Serial.print("Button: ");
Serial.print(inputFromICP, DEC);
Serial.print(" Binary code: ");
Serial.println(inputFromICP, BIN);
Serial.println("-----------------------");
}
What does not work now is if I put this code with the main code I wrote days ago. Serial monitor just goes: “Loop, Loop…” over and over again, no matter what button connected to my shift register I press. And frankly, it’s driving me crazy.
This is my whole code. I have NO CLUE of what is obstaculating the code to work as planned, I have got the single functions first then put them together and they work fine, the problem appears only when I run the ReadShiftRegisters() function with the rest of the code.
////////////////////////////////////////////////////////
///// LIBRARIES
////////////////////////////////////////////////////////
#include <Joystick.h>
////////////////////////////////////////////////////////
///// DEFINING PINS
////////////////////////////////////////////////////////
int LATCH = 3, CLOCK = 2, DATA = 4;
int dcsX = A2, dcsY = A1, dcsC = 13; //dcsC: TrackIR Centering
int HUD = A0;
//D7 -> 1 -> 1
//D6 -> 2 -> 10
//D5 -> 4 -> 100
//D4 -> 8 -> 1000
//D3 -> 16 -> 10000
//D2 -> 32 -> 100000
//D1 -> 64 -> 1000000
//D0 -> 128 -> 10000000
////////////////////////////////////////////////////////
///// VARIABLES
////////////////////////////////////////////////////////
int j, valueFromSR, totalShiftRegisterPins = 8;
int dcsXlast, dcsYlast, HudLastStep = 0, currentStep = 0;
byte switchVar = 0;
////////////////////////////////////////////////////////
///// JOYSTICK TYPE
////////////////////////////////////////////////////////
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
7,0,
false, false, false,
false, false, false,
false, false,
false, false, false);
////////////////////////////////////////////////////////
///// SETUP /////
////////////////////////////////////////////////////////
void setup()
{
//Debug
Serial.begin(115200);
//Shift register
digitalWrite(LATCH, OUTPUT);
digitalWrite(CLOCK, OUTPUT);
digitalWrite(DATA, INPUT);
//Joystick
pinMode(dcsX, INPUT_PULLUP);
pinMode(dcsY, INPUT_PULLUP);
pinMode(dcsC, INPUT_PULLUP);
//Hud
pinMode(HUD, INPUT);
Joystick.begin();
}
////////////////////////////////////////////////////////
///// LOOP /////
////////////////////////////////////////////////////////
void loop()
{
ReadShiftRegisters();
ReadHUD();
ReadButtons();
ReadDCSvalues();
delay(100);
}
////////////////////////////////////////////////////////
///// READ SHIFT REGISTERS
////////////////////////////////////////////////////////
void ReadShiftRegisters()
{
//Debug
Serial.println("Loop");
byte inputFromICP = 0;
digitalWrite(LATCH, 0);
digitalWrite(CLOCK, 0);
digitalWrite(CLOCK, 1);
digitalWrite(LATCH, 1);
for(j = 0; j < 8; j++)
{
valueFromSR = digitalRead(DATA);
if(valueFromSR)
{
int a = (1 << j);
inputFromICP = inputFromICP | a;
}
digitalWrite(CLOCK, LOW);
digitalWrite(CLOCK, HIGH);
}
if(switchVar != inputFromICP)
{
switchVar = inputFromICP;
//Debug
Serial.print("Button: ");
Serial.print(inputFromICP, DEC);
Serial.print(" Binary code: ");
Serial.println(inputFromICP, BIN);
Serial.println("-----------------------");
}
}
////////////////////////////////////////////////////////
///// READ HUD BRIGHTNESS (POTENTIOMETER) (Converted to button)
////////////////////////////////////////////////////////
void ReadHUD()
{
//Hud light is divided i 6 currentStep: buttons 6 e 7 (5,6) -> currentStep are: 0, 204.6, 409.2, 613.8, 818.6, 1023
int HudBrtValue = analogRead(HUD);
//Creating steps
if(HudBrtValue == 0) { currentStep = 0; }
if(HudBrtValue > 0 && HudBrtValue < 204.6) { currentStep = 1; }
if(HudBrtValue > 204.6 && HudBrtValue < 409.2) { currentStep = 2; }
if(HudBrtValue > 409.2 && HudBrtValue < 613.8) { currentStep = 3; }
if(HudBrtValue > 613.8 && HudBrtValue < 818.6) { currentStep = 4; }
if(HudBrtValue > 818.6 && HudBrtValue < 1024) { currentStep = 5; }
//Coding whether to use brightness up or down
if(currentStep > HudLastStep) { Joystick.pressButton(5); delay(100); HudLastStep = currentStep; Joystick.releaseButton(5); }
if(currentStep < HudLastStep) { Joystick.pressButton(6); delay(100); HudLastStep = currentStep; Joystick.releaseButton(6); }
if(currentStep > HudLastStep) { Joystick.pressButton(5); delay(100); HudLastStep = currentStep; Joystick.releaseButton(5); }
if(currentStep < HudLastStep) { Joystick.pressButton(6); delay(100); HudLastStep = currentStep; Joystick.releaseButton(6); }
if(currentStep > HudLastStep) { Joystick.pressButton(5); delay(100); HudLastStep = currentStep; Joystick.releaseButton(5); }
if(currentStep < HudLastStep) { Joystick.pressButton(6); delay(100); HudLastStep = currentStep; Joystick.releaseButton(6); }
if(currentStep > HudLastStep) { Joystick.pressButton(5); delay(100); HudLastStep = currentStep; Joystick.releaseButton(5); }
if(currentStep < HudLastStep) { Joystick.pressButton(6); delay(100); HudLastStep = currentStep; Joystick.releaseButton(6); }
if(currentStep > HudLastStep) { Joystick.pressButton(5); delay(100); HudLastStep = currentStep; Joystick.releaseButton(5); }
if(currentStep < HudLastStep) { Joystick.pressButton(6); delay(100); HudLastStep = currentStep; Joystick.releaseButton(6); }
/*
//Debug
Serial.print("Trigger val: ");
Serial.println(HudBrtValue);
Serial.print("Current step val: ");
Serial.println(currentStep);
Serial.print("Last step val: ");
Serial.println(HudLastStep);*/
}
////////////////////////////////////////////////////////
///// READ BUTTONS (STRAIGHT TO ARDUINO)
////////////////////////////////////////////////////////
void ReadButtons()
{
if(digitalRead(dcsC) == HIGH) { Joystick.releaseButton(4); }
if(digitalRead(dcsC) == LOW) { Joystick.pressButton(4); }
}
////////////////////////////////////////////////////////
///// READ ANALOG LEFT,RIGHT,UP,DOWN (Converter to button)
////////////////////////////////////////////////////////
void ReadDCSvalues()
{
int currentX = analogRead(dcsX);
int currentY = analogRead(dcsY);
if((currentX > 1000 && currentY > 1000) || (currentX < 50 && currentY < 50) || (currentX > 1000 && currentY < 50) || (currentX < 50 && currentY > 1000)) { dcsXlast = currentX; dcsYlast = currentY; }
//Avoid to repeat same command if I hold the analog in the same position
if(currentX != dcsXlast)
{
//Right
if(currentX > 50) { Joystick.pressButton(1); dcsXlast = currentX; }
//Left
if(currentX < 300) { Joystick.pressButton(2); dcsXlast = currentX; }
//Centre
if(currentX > 50 && currentX < 1000) { Joystick.releaseButton(1); Joystick.releaseButton(2); dcsXlast = currentX; }
}
//Verifico che l'ultimo stato è diverso per evitare di ripetere in loop il comando sull'asse Y
if(currentY != dcsYlast)
{
//Up
if(currentY > 1000) { Joystick.pressButton(0); dcsYlast = currentY; }
//Down
if(currentY < 50) { Joystick.pressButton(3); dcsYlast = currentY; }
//Centre
if(currentY > 50 && currentY < 1000) { Joystick.releaseButton(0); Joystick.releaseButton(3); dcsYlast = currentY; }
}
}