def start():
    """Enable scanning by setting the global flag to True."""
    global running
    global Error_signal_offset
    if reset_on_start == True:
        Error_signal_offset = GPIB.read_mv(OS,
                                           long=Long,
                                           measurement=Measurement)
    running = True
def regulation_setpoint_set():
    global Error_signal_offset
    global pulsing
    if pulsing:
        Error_signal_offset = GPIB.cursor_vbar_read_mv(OS)
    else:
        Error_signal_offset = GPIB.read_mv(OS,
                                           long=Long,
                                           measurement=Measurement)
def scanning():  #Defining what is happening when scanning our GUI

    global reset  #Global variables, these first three are necessary but again, leave them all.
    global pulsing
    global running
    global Ups
    global Downs
    global i
    global Error_signal_offset

    #########################################
    # Reset button loop
    #########################################

    if reset:  #Checks the reset parameter and runs if True
        print("-" * 60 + "\n\n\nResetting Oscilloscope\n\n\n" +
              "-" * 60)  #print visual break to indicate reset

        GPIB.measurement_setup(
            OS, IF_Channel,
            measurement=Measurement)  #Same as beginning parameters above
        GPIB.channel_settings_check(OS, IF_Channel)
        GPIB.trigger_settings_set(OS, Trigger_Channel, Trigger_Level)
        GPIB.vertical_marker_pulsing(OS, IF_Channel)

        reset = False  #Sets the reset parameter to false so not looping

    #########################################
    # Checking for the regulation loop if on
    #########################################

    if running:  # running parameter, if True, runs this loop
        root.configure(
            bg='SpringGreen2')  #Sets the background of the GUI to be green

        #########################################
        # Loop for pulsing operation
        #########################################

        if pulsing:  #Checks pulsing tag and runs this loop if true

            read_value = GPIB.cursor_vbar_read_mv(
                OS)  #Takes the current value of oscilloscope vbar

            if read_value > (
                    Error_signal_offset + Pulse_Walk_Threshold
            ):  #Checks to see if that value is outside of threshold
                Ups += 1
                Downs = 0
                if Ups > Loops_Debounce:  #Effective debounce
                    temp_freq = GPIB.freq(SG)  #Gathers the current frequency
                    GPIB.write_frequency(
                        SG, (temp_freq + Pulse_Step_Size), "HZ"
                    )  #Writes calculated frequency to the signal generator
                    print("Raised Frequency ",
                          i)  #Shows that we took a step in frequency
                    if (temp_freq + Pulse_Step_Size) > (
                            Start_Freq + Max_Threshold
                    ):  #Sees if the new frequency is outside of bounds
                        print("Error: Broken on too many steps upward")
                        root.configure(
                            bg=interlock_color)  #Sets to the interlock color
                        running = False  #Breaks loop if true
                    if OS.query(
                            "MEASU:Meas{}:State?".format(Measurement)
                    )[-2] != str(1):  #Sees if the measurement is still active
                        print("Error: Measurement Off")
                        root.configure(
                            bg=interlock_color)  #Sets to the interlock color
                        running = False  #Breaks loop if true
                    if read_value > (Error_signal_offset +
                                     Interlock_Threshold_mv):
                        print("Error: Deviation too far")
                        root.configure(
                            bg=interlock_color)  #Sets to the interlock color
                        running = False
                    time.sleep(
                        Wait_after_step)  #Sleep for after step debounce time

            #########################################
            # Repeat above loop but below the threshold instead of above
            #########################################

            if read_value < (Error_signal_offset - Pulse_Walk_Threshold):
                Downs += 1
                Ups = 0
                if Downs > Loops_Debounce:
                    temp_freq = GPIB.freq(SG)
                    GPIB.write_frequency(SG, (temp_freq - Pulse_Step_Size),
                                         "HZ")
                    print("Lowered Frequency ", i)
                    if (temp_freq - Pulse_Step_Size) < (Start_Freq -
                                                        Max_Threshold):
                        print("Broken on too many steps downward")
                        root.configure(bg=interlock_color)
                        running = False
                    if OS.query("MEASU:Meas{}:State?".format(
                            Measurement))[-2] != str(1):
                        print("Measurement Off")
                        root.configure(bg=interlock_color)
                        running = False
                    if read_value < (Error_signal_offset -
                                     Interlock_Threshold_mv):
                        print("Error: Deviation too far")
                        root.configure(
                            bg=interlock_color)  #Sets to the interlock color
                        running = False
                    time.sleep(Wait_after_step)

            time.sleep(Wait_between_reads)
            i += 1

        #########################################
        # Loop for CW operation, use same logic
        #########################################

        else:
            read_value = GPIB.read_mv(OS, long=Long, measurement=Measurement)

            if read_value > (Error_signal_offset + Walk_Threshold):
                Ups += 1
                Downs = 0
                if Ups > Loops_Debounce:
                    temp_freq = GPIB.freq(SG)
                    GPIB.write_frequency(SG, (temp_freq + Step_size), "HZ")
                    print("Raised Frequency ", i)
                    if (temp_freq + Step_size) > (Start_Freq + Max_Threshold):
                        print("Broken on too many steps upward")
                        root.configure(bg=interlock_color)
                        running = False
                    if OS.query("MEASU:Meas{}:State?".format(
                            Measurement))[-2] != str(1):
                        print("Measurement Off")
                        root.configure(bg=interlock_color)
                        running = False
                    if read_value > (Error_signal_offset +
                                     Interlock_Threshold_mv):
                        print("Error: Deviation too far")
                        root.configure(
                            bg=interlock_color)  #Sets to the interlock color
                        running = False
                    time.sleep(Wait_after_step)

            #########################################
            # Repeat above loop but below the threshold instead of above
            #########################################

            if read_value < (Error_signal_offset - Walk_Threshold):
                Downs += 1
                Ups = 0
                if Downs > Loops_Debounce:
                    temp_freq = GPIB.freq(SG)
                    GPIB.write_frequency(SG, (temp_freq - Step_size), "HZ")
                    print("Lowered Frequency ", i)
                    if (temp_freq - Step_size) < (Start_Freq - Max_Threshold):
                        print("Broken on too many steps downward")
                        root.configure(bg=interlock_color)
                        running = False
                    if OS.query("MEASU:Meas{}:State?".format(
                            Measurement))[-2] != str(1):
                        print("Measurement Off")
                        root.configure(bg=interlock_color)
                        running = False
                    if read_value < (Error_signal_offset -
                                     Interlock_Threshold_mv):
                        print("Error: Deviation too far")
                        root.configure(
                            bg=interlock_color)  #Sets to the interlock color
                        running = False
                    time.sleep(Wait_after_step)

            #time.sleep(Wait_between_reads)
            i += 1  #Update iterator

    # After .1 seconds, call scanning again (create a recursive loop)
    root.after(100,
               scanning)  #Wait the first number of ms, then continue scanning
        GPIB.vertical_marker_pulsing(OS, IF_Channel)
        os.system('cls')

        M.Write(Client, Reset_Tag, False, Bool = True)
        
    
    #########################################
    # Checking to see if we want to update regulation setpoint
    #########################################
    if regulation_setpoint_change:
        
        if pulsing:
            Error_signal_offset = GPIB.cursor_vbar_read_mv(OS)
            print("New regulation setpoint: {:.3f} mV".format(Error_signal_offset))
        else:
            Error_signal_offset = GPIB.read_mv(OS, long = Long, measurement = Measurement)
            print("New regulation setpoint: {:.3f} mV".format(Error_signal_offset))

        if Error_signal_offset > 10000:
            print("Cannot reset Error signal offset with measurement off")
            print("New regulation setpoint: 0 mV")
            Error_signal_offset = 0
            
        M.Write(Client, Regulation_Setpoint_Tag, False, Bool = True)
        M.Write(Client, float(Regulation_Entry_Tag), Error_signal_offset)
    #########################################
    # Checking for the regulation loop if on
    #########################################

    if running:  # running parameter, if True, runs this loop
        #print("Step 3")