Your loop has an analog input task and an analot output task. The analog input task has the DAQmxCreateTask outside of the loop and the read inside of the loop. This will create one task and read the channel on each loop iteration. The analog output task has the DAQmxCreateTask INSIDE of the loop. This will create a new task with each iteration of the loop. You want to move the DAQmxCreateTask for the analog output outside of the loop.