// ********** Initialize the spectrometer **********

        private void InitializeButton_Click(object sender, EventArgs e)
        {
            // Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 7 ¤¤¤¤¤¤¤¤¤¤¤¤¤");
            //Close any previously used spectrometer (in case user clicks on the button more than once)
            if (spectrometer != null)
            {
                spectrometer.Close();
            }
            // Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 8 ¤¤¤¤¤¤¤¤¤¤¤¤¤");
            //This static method searches for devices and returns a list of driver for the available devices.
            Spectrometer[] devices = Qseries.SearchDevices();
            if (devices.Length == 0)
            {
                devices = RgbSpectrometer.SearchDevices();
            }
            if (devices.Length == 0)
            {
                devices = Qstick.SearchDevices();
            }
            //Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 9 ¤¤¤¤¤¤¤¤¤¤¤¤¤");
            //If no device was found:
            if (devices.Length == 0)
            {
                InitStatusLabel.Text = "No spectrometer found.";
                MessageBox.Show("No spectrometer found.", "Cannot initialize spectrometer",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;
            }

            //Otherwise, take the first device and initialize it.
            spectrometer = devices[0];
            //Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 10 ¤¤¤¤¤¤¤¤¤¤¤¤¤");
            try
            {
                InitStatusLabel.Text = "Initializing spectrometer ...";
                statusStrip1.Update();

                spectrometer.Open();
                //Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 11 ¤¤¤¤¤¤¤¤¤¤¤¤¤");
                //Get the wavelength of each pixel (this is not actually used in this sample code)
                double[] wavelengths = spectrometer.GetWavelengths();

                //Initialize values in GUI
                ExpTimeNumericUpDown.Value = (decimal)spectrometer.ExposureTime;
                if (spectrometer is CalibratedSpectrometer)
                {
                    SensitivityCalibrationCheckBox.Checked = (spectrometer as CalibratedSpectrometer).UseSensitivityCalibration;
                    SensitivityCalibrationCheckBox.Enabled = true;
                }
                else
                {
                    SensitivityCalibrationCheckBox.Checked = false;
                    SensitivityCalibrationCheckBox.Enabled = false;
                }
                peaksTable.RowCount = numberOfPeaks;
                // Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 12 ¤¤¤¤¤¤¤¤¤¤¤¤¤");

                for (int i = 0; i < numberOfPeaks; i++)
                {
                    peaks.Add(new COG(0, 0, 0, 0, 0));
                    peaksTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 26F));
                }
                //      (spectrometer as CalibratedSpectrometer).UseExternalTrigger = true;


                for (int i = 0; i < numberOfPeaks; i++)
                {
                    //Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 13 ¤¤¤¤¤¤¤¤¤¤¤¤¤");
                    wLeft.Add(new NumericUpDown());
                    wRight.Add(new NumericUpDown());
                    threshList.Add(new NumericUpDown());
                    labelPList.Add(new System.Windows.Forms.Label());
                    labelCOG.Add(new System.Windows.Forms.Label());


                    SetWaves(wLeft[i]);
                    wLeft[i].Name = $"waveLengthLeft {(i + 1).ToString()}";
                    peaksTable.Controls.Add(wLeft[i], 2, i);

                    SetWaves(wRight[i]);
                    wRight[i].Name = $"waveLengthRight {(i + 1).ToString()}";
                    peaksTable.Controls.Add(wRight[i], 3, i);

                    SetThres(threshList[i]);
                    threshList[i].Name = $"threshold {(i + 1).ToString()}";
                    peaksTable.Controls.Add(threshList[i], 4, i);

                    SetLabelP(labelPList[i]);
                    labelPList[i].Text = $"Peak {(i+1).ToString()}";
                    peaksTable.Controls.Add(labelPList[i], 1, i);



                    //PeakTrackBox.Controls.Add(waveLengths[i]);
                }



                for (int i = 0; i < peaks.Count; i++)
                {
                    InitPeak(peaks[i], spectrometer.WavelengthCoefficients, wLeft[i], wRight[i], threshList[i]);
                    Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 16 ¤¤¤¤¤¤¤¤¤¤¤¤¤");
                }
                // Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 15 ¤¤¤¤¤¤¤¤¤¤¤¤¤");
                //peak1.SetWaveCof(spectrometer.WavelengthCoefficients);
                //peak1.SetWaveLeft((float)numericUpDown1.Value);
                // peak1.SetWaveRight((float)numericUpDown2.Value);
                //peak1.SetThresholde((float)numericUpDowntr1.Value * (float)0.01);
                //peak1.waveLeft = (float)numericUpDown1.Value;
                //peak1.waveRight = (float)numericUpDown2.Value;
                //peak1.waveLeft = (float)waveLengths[0].Value;
                //peak1.waveRight = (float)waveLengths[1].Value;
                //peak1.thresholde = (float)numericUpDowntr1.Value * (float)0.01;
                // peak2.SetWaveCof(spectrometer.WavelengthCoefficients);   //the overwriting happens here.
                //peak2.SetWaveLeft((float)numericUpDown3.Value);
                // peak2.SetWaveRight((float)numericUpDown4.Value);
                //peak2.SetThresholde((float)numericUpDowntr2.Value * (float)0.01);
                // peak2.waveLeft = (float)waveLengths[2].Value;
                // peak2.waveRight = (float)waveLengths[3].Value;
                // peak2.thresholde = (float)numericUpDowntr2.Value * (float)0.01;
                Debug.WriteLine($"¤¤¤¤¤¤¤¤¤¤¤¤¤ DEBUG 17 ¤¤¤¤¤¤¤¤¤¤¤¤¤");

                InitStatusLabel.Text = "Found " + spectrometer.DetailedDeviceName;
                MessageBox.Show("Device name: " + spectrometer.ModelName + Environment.NewLine
                                + "Manufacturer: " + spectrometer.Manufacturer + Environment.NewLine
                                + "Serial number: " + spectrometer.SerialNo + Environment.NewLine
                                + "Number of pixels: " + spectrometer.PixelCount,
                                "Spectrometer found and initialized", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                spectrometer         = null;
                InitStatusLabel.Text = "Initialization error.";
                MessageBox.Show(ex.Message, "Cannot initialize spectrometer",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /* private void numericUpDown2_ValueChanged(object sender, EventArgs e)
         *   {
         *   //peak1.SetWaveRight((float)numericUpDown2.Value);
         *   peak1.waveRight = (float)numericUpDown2.Value;
         *   }
         * private void numericUpDown3_ValueChanged(object sender, EventArgs e)
         *   {
         *   peak2.waveLeft = (float)numericUpDown3.Value;
         *   }
         *
         * private void numericUpDown4_ValueChanged(object sender, EventArgs e)
         *   {
         *   //peak2.SetWaveRight((float)numericUpDown4.Value);
         *   peak1.waveRight = (float)numericUpDown4.Value;
         *
         *   }
         * private void numericUpDown5_ValueChanged(object sender, EventArgs e)
         *   {
         *   //peaks[2].SetWaveLeft((float)numericUpDown5.Value);
         *   }
         * private void numericUpDown6_ValueChanged(object sender, EventArgs e)
         *   {
         *   //peaks[2].SetWaveRight((float)numericUpDown6.Value);
         *   }
         * private void numericUpDown7_ValueChanged(object sender, EventArgs e)
         *   {
         *   // peaks[3].SetWaveLeft((float)numericUpDown7.Value);
         *   }
         * private void numericUpDown8_ValueChanged(object sender, EventArgs e)
         *   {
         *   //peaks[3].SetWaveRight((float)numericUpDown8.Value);
         *
         *   }
         * private void numericUpDown9_ValueChanged(object sender, EventArgs e)
         *   {
         *   //peaks[4].SetWaveLeft((float)numericUpDown9.Value);
         *   }
         * private void numericUpDown10_ValueChanged(object sender, EventArgs e)
         *   {
         *   // peaks[4].SetWaveRight((float)numericUpDown10.Value);
         *   }
         *
         */
        private void InitSpectrometer()
        {
            //Close any previously used spectrometer (in case user clicks on the button more than once)
            if (spectrometer != null)
            {
                spectrometer.Close();
            }

            //This static method searches for devices and returns a list of driver for the available devices.
            Spectrometer[] devices = Qseries.SearchDevices();
            if (devices.Length == 0)
            {
                devices = RgbSpectrometer.SearchDevices();
            }
            if (devices.Length == 0)
            {
                devices = Qstick.SearchDevices();
            }

            //If no device was found:
            if (devices.Length == 0)
            {
                InitStatusLabel.Text = "No spectrometer found.";
                MessageBox.Show("No spectrometer found.", "Cannot initialize spectrometer",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Otherwise, take the first device and initialize it.
            spectrometer = devices[0];
            try
            {
                InitStatusLabel.Text = "Initializing spectrometer ...";
                statusStrip1.Update();

                spectrometer.Open();

                //Get the wavelength of each pixel (this is not actually used in this sample code)
                double[] wavelengths = spectrometer.GetWavelengths();

                //Initialize values in GUI
                ExpTimeNumericUpDown.Value = (decimal)spectrometer.ExposureTime;
                if (spectrometer is CalibratedSpectrometer)
                {
                    SensitivityCalibrationCheckBox.Checked = (spectrometer as CalibratedSpectrometer).UseSensitivityCalibration;
                    SensitivityCalibrationCheckBox.Enabled = true;
                    //checkBoxEksternTrigger.Checked = (spectrometer as CalibratedSpectrometer).UseExternalTrigger;
                }
                else
                {
                    SensitivityCalibrationCheckBox.Checked = false;
                    SensitivityCalibrationCheckBox.Enabled = false;
                }

                //      (spectrometer as CalibratedSpectrometer).UseExternalTrigger = true;
            }
            catch (Exception ex)
            {
                spectrometer         = null;
                InitStatusLabel.Text = "Initialization error.";
                MessageBox.Show(ex.Message, "Cannot initialize spectrometer",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void InitSpectrometer()
        {
            //Close any previously used spectrometer (in case user clicks on the button more than once)
            if (spectrometer != null)
            {
                spectrometer.Close();
            }

            //This static method searches for devices and returns a list of driver for the available devices.
            Spectrometer[] devices = Qseries.SearchDevices();
            if (devices.Length == 0)
            {
                devices = RgbSpectrometer.SearchDevices();
            }
            if (devices.Length == 0)
            {
                devices = Qstick.SearchDevices();
            }

            //If no device was found:
            if (devices.Length == 0)
            {
                InitStatusLabel.Text = "No spectrometer found.";
                MessageBox.Show("No spectrometer found.", "Cannot initialize spectrometer",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Otherwise, take the first device and initialize it.
            spectrometer = devices[0];
            try
            {
                InitStatusLabel.Text = "Initializing spectrometer ...";
                statusStrip1.Update();

                spectrometer.Open();

                //Get the wavelength of each pixel (this is not actually used in this sample code)
                double[] wavelengths = spectrometer.GetWavelengths();

                //Initialize values in GUI
                ExpTimeNumericUpDown.Value = (decimal)spectrometer.ExposureTime;
                if (spectrometer is CalibratedSpectrometer)
                {
                    SensitivityCalibrationCheckBox.Checked = (spectrometer as CalibratedSpectrometer).UseSensitivityCalibration;
                    SensitivityCalibrationCheckBox.Enabled = true;
                    //checkBoxEksternTrigger.Checked = (spectrometer as CalibratedSpectrometer).UseExternalTrigger;
                }
                else
                {
                    SensitivityCalibrationCheckBox.Checked = false;
                    SensitivityCalibrationCheckBox.Enabled = false;
                }

                //      (spectrometer as CalibratedSpectrometer).UseExternalTrigger = true;


                peak1.SetWaveCof(spectrometer.WavelengthCoefficients);
                peak1.SetWaveLeft((float)numericUpDown1.Value);
                peak1.SetWaveRight((float)numericUpDown2.Value);
                peak1.SetThresholde((float)numericUpDowntr1.Value * (float)0.01);


                peak2.SetWaveCof(spectrometer.WavelengthCoefficients);  //overwrite doesnt happen here
                peak2.SetWaveLeft((float)numericUpDown3.Value);
                peak2.SetWaveRight((float)numericUpDown4.Value);
                peak2.SetThresholde((float)numericUpDowntr2.Value * (float)0.01);

                Debug.WriteLine($"WAVES PEAK 1 ------------ {numericUpDown1.Value} {numericUpDown2.Value}");
                Debug.WriteLine($"WAVES PEAK 2 ------------ {numericUpDown3.Value} {numericUpDown4.Value}");


                /*
                 * InitStatusLabel.Text = "Found " + spectrometer.DetailedDeviceName;
                 * MessageBox.Show("Device name: " + spectrometer.ModelName + Environment.NewLine
                 + "Manufacturer: " + spectrometer.Manufacturer + Environment.NewLine
                 + "Serial number: " + spectrometer.SerialNo + Environment.NewLine
                 + "Number of pixels: " + spectrometer.PixelCount,
                 +     "Spectrometer found and initialized", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 */
            }
            catch (Exception ex)
            {
                spectrometer         = null;
                InitStatusLabel.Text = "Initialization error.";
                MessageBox.Show(ex.Message, "Cannot initialize spectrometer",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }