If else condition for Sound or Heart Symbol

Hi
I have a psychopy experiment for EEG experiment where i am tellling people to focus on heartbeat or hearing based on two images shown on screen.I want to add a functionality with code for the loop in the “real trials” and “real ratings” boxes in psychopy that tells the EEG system whether the subjects saw either the heart symbol or the headphone symbol (i.e. whether we instructed them to focus on their heartbeat or on the noise).

The codes are as follows

Real trials

— Prepare to start Routine “real_trials” —

    # create an object to store info about Routine real_trials
    real_trials = data.Routine(
        name='real_trials',
        components=[text_53, sound_24, image],
    )
    real_trials.status = NOT_STARTED
    continueRoutine = True
    # update component parameters for each repeat
    sound_24.setSound('Brownnoise2.flac', secs=20, hamming=True)
    sound_24.setVolume(1.0, log=False)
    sound_24.seek(0)
    image.setImage(image_instruction)
    # store start times for real_trials
    real_trials.tStartRefresh = win.getFutureFlipTime(clock=globalClock)
    real_trials.tStart = globalClock.getTime(format='float')
    real_trials.status = STARTED
    thisExp.addData('real_trials.started', real_trials.tStart)
    real_trials.maxDuration = None
    # keep track of which components have finished
    real_trialsComponents = real_trials.components
    for thisComponent in real_trials.components:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    frameN = -1
    
    # --- Run Routine "real_trials" ---
    # if trial has changed, end Routine now
    if isinstance(trials, data.TrialHandler2) and thisTrial.thisN != trials.thisTrial.thisN:
        continueRoutine = False
    real_trials.forceEnded = routineForceEnded = not continueRoutine
    while continueRoutine:
        # get current time
        t = routineTimer.getTime()
        tThisFlip = win.getFutureFlipTime(clock=routineTimer)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # *text_53* updates
        
        # if text_53 is starting this frame...
        if text_53.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            text_53.frameNStart = frameN  # exact frame index
            text_53.tStart = t  # local t and not account for scr refresh
            text_53.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(text_53, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'text_53.started')
            # update status
            text_53.status = STARTED
            text_53.setAutoDraw(True)
        
        # if text_53 is active this frame...
        if text_53.status == STARTED:
            # update params
            pass
        
        # if text_53 is stopping this frame...
        if text_53.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > text_53.tStartRefresh + isi_time-frameTolerance:
                # keep track of stop time/frame for later
                text_53.tStop = t  # not accounting for scr refresh
                text_53.tStopRefresh = tThisFlipGlobal  # on global time
                text_53.frameNStop = frameN  # exact frame index
                # add timestamp to datafile
                thisExp.timestampOnFlip(win, 'text_53.stopped')
                # update status
                text_53.status = FINISHED
                text_53.setAutoDraw(False)
        
        # *sound_24* updates
        
        # if sound_24 is starting this frame...
        if sound_24.status == NOT_STARTED and tThisFlip >= isi_time-frameTolerance:
            # keep track of start time/frame for later
            sound_24.frameNStart = frameN  # exact frame index
            sound_24.tStart = t  # local t and not account for scr refresh
            sound_24.tStartRefresh = tThisFlipGlobal  # on global time
            # add timestamp to datafile
            thisExp.addData('sound_24.started', tThisFlipGlobal)
            # update status
            sound_24.status = STARTED
            sound_24.play(when=win)  # sync with win flip
        
        # if sound_24 is stopping this frame...
        if sound_24.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > sound_24.tStartRefresh + 20-frameTolerance or sound_24.isFinished:
                # keep track of stop time/frame for later
                sound_24.tStop = t  # not accounting for scr refresh
                sound_24.tStopRefresh = tThisFlipGlobal  # on global time
                sound_24.frameNStop = frameN  # exact frame index
                # add timestamp to datafile
                thisExp.timestampOnFlip(win, 'sound_24.stopped')
                # update status
                sound_24.status = FINISHED
                sound_24.stop()
        
        # *image* updates
        
        # if image is starting this frame...
        if image.status == NOT_STARTED and tThisFlip >= isi_time-frameTolerance:
            # keep track of start time/frame for later
            image.frameNStart = frameN  # exact frame index
            image.tStart = t  # local t and not account for scr refresh
            image.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(image, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'image.started')
            # update status
            image.status = STARTED
            image.setAutoDraw(True)
        
        # if image is active this frame...
        if image.status == STARTED:
            # update params
            pass
        
        # if image is stopping this frame...
        if image.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > image.tStartRefresh + 20-frameTolerance:
                # keep track of stop time/frame for later
                image.tStop = t  # not accounting for scr refresh
                image.tStopRefresh = tThisFlipGlobal  # on global time
                image.frameNStop = frameN  # exact frame index
                # add timestamp to datafile
                thisExp.timestampOnFlip(win, 'image.stopped')
                # update status
                image.status = FINISHED
                image.setAutoDraw(False)
        
        # check for quit (typically the Esc key)
        if defaultKeyboard.getKeys(keyList=["escape"]):
            thisExp.status = FINISHED
        if thisExp.status == FINISHED or endExpNow:
            endExperiment(thisExp, win=win)
            return
        # pause experiment here if requested
        if thisExp.status == PAUSED:
            pauseExperiment(
                thisExp=thisExp, 
                win=win, 
                timers=[routineTimer], 
                playbackComponents=[sound_24]
            )
            # skip the frame we paused on
            continue
        
        # check if all components have finished
        if not continueRoutine:  # a component has requested a forced-end of Routine
            real_trials.forceEnded = routineForceEnded = True
            break
        continueRoutine = False  # will revert to True if at least one component still running
        for thisComponent in real_trials.components:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # --- Ending Routine "real_trials" ---
    for thisComponent in real_trials.components:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    # store stop times for real_trials
    real_trials.tStop = globalClock.getTime(format='float')
    real_trials.tStopRefresh = tThisFlipGlobal
    thisExp.addData('real_trials.stopped', real_trials.tStop)
    sound_24.pause()  # ensure sound has stopped at end of Routine
    # the Routine "real_trials" was not non-slip safe, so reset the non-slip timer
    routineTimer.reset()

Real Ratings

    # --- Prepare to start Routine "real_ratings" ---
    # create an object to store info about Routine real_ratings
    real_ratings = data.Routine(
        name='real_ratings',
        components=[slider_23, text_54],
    )
    real_ratings.status = NOT_STARTED
    continueRoutine = True
    # update component parameters for each repeat
    slider_23.reset()
    text_54.setText(questions_ratings)
    # store start times for real_ratings
    real_ratings.tStartRefresh = win.getFutureFlipTime(clock=globalClock)
    real_ratings.tStart = globalClock.getTime(format='float')
    real_ratings.status = STARTED
    thisExp.addData('real_ratings.started', real_ratings.tStart)
    real_ratings.maxDuration = None
    # keep track of which components have finished
    real_ratingsComponents = real_ratings.components
    for thisComponent in real_ratings.components:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    frameN = -1
    
    # --- Run Routine "real_ratings" ---
    # if trial has changed, end Routine now
    if isinstance(trials, data.TrialHandler2) and thisTrial.thisN != trials.thisTrial.thisN:
        continueRoutine = False
    real_ratings.forceEnded = routineForceEnded = not continueRoutine
    while continueRoutine:
        # get current time
        t = routineTimer.getTime()
        tThisFlip = win.getFutureFlipTime(clock=routineTimer)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # *slider_23* updates
        
        # if slider_23 is starting this frame...
        if slider_23.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            slider_23.frameNStart = frameN  # exact frame index
            slider_23.tStart = t  # local t and not account for scr refresh
            slider_23.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(slider_23, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'slider_23.started')
            # update status
            slider_23.status = STARTED
            slider_23.setAutoDraw(True)
        
        # if slider_23 is active this frame...
        if slider_23.status == STARTED:
            # update params
            pass
        
        # Check slider_23 for response to end Routine
        if slider_23.getRating() is not None and slider_23.status == STARTED:
            continueRoutine = False
        
        # *text_54* updates
        
        # if text_54 is starting this frame...
        if text_54.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            text_54.frameNStart = frameN  # exact frame index
            text_54.tStart = t  # local t and not account for scr refresh
            text_54.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(text_54, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'text_54.started')
            # update status
            text_54.status = STARTED
            text_54.setAutoDraw(True)
        
        # if text_54 is active this frame...
        if text_54.status == STARTED:
            # update params
            pass
        
        # check for quit (typically the Esc key)
        if defaultKeyboard.getKeys(keyList=["escape"]):
            thisExp.status = FINISHED
        if thisExp.status == FINISHED or endExpNow:
            endExperiment(thisExp, win=win)
            return
        # pause experiment here if requested
        if thisExp.status == PAUSED:
            pauseExperiment(
                thisExp=thisExp, 
                win=win, 
                timers=[routineTimer], 
                playbackComponents=[]
            )
            # skip the frame we paused on
            continue
        
        # check if all components have finished
        if not continueRoutine:  # a component has requested a forced-end of Routine
            real_ratings.forceEnded = routineForceEnded = True
            break
        continueRoutine = False  # will revert to True if at least one component still running
        for thisComponent in real_ratings.components:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # --- Ending Routine "real_ratings" ---
    for thisComponent in real_ratings.components:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    # store stop times for real_ratings
    real_ratings.tStop = globalClock.getTime(format='float')
    real_ratings.tStopRefresh = tThisFlipGlobal
    thisExp.addData('real_ratings.stopped', real_ratings.tStop)
    trials.addData('slider_23.response', slider_23.getRating())
    trials.addData('slider_23.rt', slider_23.getRT())
    # the Routine "real_ratings" was not non-slip safe, so reset the non-slip timer
    routineTimer.reset()
    thisExp.nextEntry()

I am a little clueless on how to do this
I understand we need to add a code segment on psychopy by copying relevant variables and working on it.
Can you give me an idea how to proceed

Depends a bit on which specific EEG system you’re using. This looks like a builder study, so if you open the PsychoPy builder there are components for both serial port and parallel port EEG outputs. You can add one and then make the “start signal” a variable, and define the variable in a code component depending on which image is displayed.

You can also search for the EEG brand on this forum (e.g., EGI, Brainvision, whatever you’re using) and you’ll probably find something useful.