pause program until a switch is moved

hi everyone,

I’m trying to make a program to calibrate my sensors (actually store the data on an SD card to use them in another program). It’s for a line-follower robot that has to detect green, black and white. I have a switch on my robot that I can use for whatever I want. So what I wanted to do is to set the switch in ‘off-mode’ until I set the robot on the right colour, move the switch (so the robot now it’s the data that it measure now that has to be stored on the SD card), and do this 3 times. I hope this make sense.
To do this I tried to insert this:

while (digitalRead(bInput) == LOW) {}

or

while (digitalRead(bInput) == HIGH) {}

to wait until the switch is moved. But this doesn’t work (when I move the switch it don’t get out of this loop).

I really hope that I was clear.

Thanks in advance

#include <SD.h>
#include <SPI.h>
File myFile;
const int chipSelect = BUILTIN_SDCARD;

#define bOutput 7
#define bInput  8

int i;
const byte lineSensor[] = {A0, A1, A2, A3, A6, A7, A8, A9};


#include <Wire.h>
#include <Math.h>

byte i2cWriteBuffer[10];
byte i2cReadBuffer[10];

#define SensorAddressWrite 0x29 //
#define SensorAddressRead 0x29 // 
#define EnableAddress 0xa0 // register address + command bits
#define ATimeAddress 0xa1 // register address + command bits
#define WTimeAddress 0xa3 // register address + command bits
#define ConfigAddress 0xad // register address + command bits
#define ControlAddress 0xaf // register address + command bits
#define IDAddress 0xb2 // register address + command bits
#define ColorAddress 0xb4 // register address + command bits

unsigned int c_c1 = 0;
unsigned int c_c2 = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(bOutput, OUTPUT);
  digitalWrite(bOutput, HIGH);
  init_TCS34725_1();
  init_TCS34725_2();

  Serial.println("sensors initialized");
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  SD.remove("data/lMin.txt");
  SD.remove("data/lMax.txt");
  SD.remove("data/cB.txt");
  SD.remove("data/cW.txt");
  SD.remove("data/cG.txt");
  
  while (digitalRead(bInput) == LOW) {}
  Serial.println("button moved");
  TCS34725_1();
  TCS34725_1();
  myFile = SD.open("data/lMin.txt", FILE_WRITE);
  for (i=0; i<=7; i++) {
    if (myFile) {
      myFile.print(analogRead(lineSensor[i]));
      myFile.println("/");
    }
  }
  myFile.close();
  myFile = SD.open("data/cB.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(c_c1);
    myFile.println("/");
    myFile.print(c_c2);
    myFile.println("/");
  }
  myFile.close();
  
  while (digitalRead(bInput) == HIGH) {}
  TCS34725_1();
  TCS34725_2();
  myFile = SD.open("data/lMax.txt", FILE_WRITE);
  for (i=0; i<=7; i++) {
    if (myFile) {
      myFile.print(analogRead(lineSensor[i]));
      myFile.println("/");
    }
  }
  myFile.close();
  myFile = SD.open("data/cW.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(c_c1);
    myFile.println("/");
    myFile.print(c_c2);
    myFile.println("/");
  }
  myFile.close();

  while (digitalRead(bInput) == LOW) {}
  TCS34725_1();
  TCS34725_2();
  myFile = SD.open("data/cG.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(c_c1);
    myFile.println("/");
    myFile.print(c_c2);
    myFile.println("/");
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  
}

void init_TCS34725_1() {
  Wire.begin();
  
  i2cWriteBuffer[0] = 0x00;
  //i2cWriteBuffer[0] = 0xff;
  Writei2cRegisters_1(1,ATimeAddress);    // RGBC timing is 256 - contents x 2.4mS =  
  i2cWriteBuffer[0] = 0x00;
  Writei2cRegisters_1(1,ConfigAddress);   // Can be used to change the wait time
  i2cWriteBuffer[0] = 0x00;
  Writei2cRegisters_1(1,ControlAddress);  // RGBC gain control
  i2cWriteBuffer[0] = 0x03;
  Writei2cRegisters_1(1,EnableAddress);    // enable ADs and oscillator for sensor
}

void init_TCS34725_2() {
  Wire1.begin();
  
  i2cWriteBuffer[0] = 0x00;
  //i2cWriteBuffer[0] = 0xff;
  Writei2cRegisters_2(1,ATimeAddress);    // RGBC timing is 256 - contents x 2.4mS =  
  i2cWriteBuffer[0] = 0x00;
  Writei2cRegisters_2(1,ConfigAddress);   // Can be used to change the wait time
  i2cWriteBuffer[0] = 0x00;
  Writei2cRegisters_2(1,ControlAddress);  // RGBC gain control
  i2cWriteBuffer[0] = 0x03;
  Writei2cRegisters_2(1,EnableAddress);    // enable ADs and oscillator for sensor
}

void TCS34725_1() {
  Wire.begin();
  Readi2cRegisters_1(8,ColorAddress);
  c_c1 = (unsigned int)(i2cReadBuffer[1]<<8) + (unsigned int)i2cReadBuffer[0];
}

void TCS34725_2() {
  Wire1.begin();
  Readi2cRegisters_2(8,ColorAddress);
  c_c2 = (unsigned int)(i2cReadBuffer[1]<<8) + (unsigned int)i2cReadBuffer[0];
}

void Writei2cRegisters_1(byte numberbytes, byte command)
{
    byte i = 0;

    Wire.beginTransmission(SensorAddressWrite);   // Send address with Write bit set
    Wire.write(command);                          // Send command, normally the register address 
    for (i=0;i<numberbytes;i++)                       // Send data 
      Wire.write(i2cWriteBuffer[i]);
    Wire.endTransmission();

    delayMicroseconds(100);      // allow some time for bus to settle      
}

void Writei2cRegisters_2(byte numberbytes, byte command)
{
    byte i = 0;

    Wire1.beginTransmission(SensorAddressWrite);   // Send address with Write bit set
    Wire1.write(command);                          // Send command, normally the register address 
    for (i=0;i<numberbytes;i++)                       // Send data 
      Wire1.write(i2cWriteBuffer[i]);
    Wire1.endTransmission();

    delayMicroseconds(100);      // allow some time for bus to settle      
}

byte Readi2cRegisters_1(int numberbytes, byte command)
{
   byte i = 0;

    Wire.beginTransmission(SensorAddressWrite);   // Write address of read to sensor
    Wire.write(command);
    Wire.endTransmission();
    
    delayMicroseconds(100);      // allow some time for bus to settle      

    Wire.requestFrom(SensorAddressRead,numberbytes);   // read data
    for(i=0;i<numberbytes;i++)
      i2cReadBuffer[i] = Wire.read();
      
    Wire.endTransmission();
    
    delayMicroseconds(100);      // allow some time for bus to settle      
}

byte Readi2cRegisters_2(int numberbytes, byte command)
{
   byte i = 0;

    Wire1.beginTransmission(SensorAddressWrite);   // Write address of read to sensor
    Wire1.write(command);
    Wire1.endTransmission();
    
    delayMicroseconds(100);      // allow some time for bus to settle      

    Wire1.requestFrom(SensorAddressRead,numberbytes);   // read data
    for(i=0;i<numberbytes;i++)
      i2cReadBuffer[i] = Wire1.read();
      
    Wire1.endTransmission();
    
    delayMicroseconds(100);      // allow some time for bus to settle      
}

the while-loop will end when "bInput" change. Input-pin defined input(pullup)

How will the robot 'know' which color it's learning? Are you looking for a sequential 1st switch learn white; 2nd switch learn green; etc.?

You're not using INPUT_PULLUP for the pinMode on that input. Have you got an external resistor pulling it HIGH or LOW? Or is it floating when not pressed and allowed to read whatever? In that case it seems to skip your while loop because those random readings when floating just happen to include one that's the right way.

Thanks!