function runHV(highVoltageControlSession, controlFrequency, frequency, voltage, cycles, name, twoChannels)

if nargin < 7
    twoChannels = false;
end

onTime = controlFrequency/frequency/2; % half the period, times cfreq to get no of samples
onTime = round(onTime); % make sure its an integer



onVoltageSignal = (2/1000)*voltage;
LEDsignal = 5;

%generate square wave
outputVoltagesOff = zeros(onTime,2);
outputVoltagesOn = zeros(onTime,2);
outputVoltagesOn(1:end,1) = LEDsignal;
outputVoltagesOn(1:end,2) = onVoltageSignal;

% create one cycle
outputVoltages = [outputVoltagesOn; 
                  outputVoltagesOff;];
% replicate to make n cycles
outputVoltages = repmat(outputVoltages, cycles, 1);
% add short off period at the beginning
outputVoltages = [zeros(10, 2); outputVoltages];


% % generate sine instead of square wave
% n = onTime*controlFrequency;
% x = 1:n;
% x = x.*(2*pi/n);
% y = 0.5*onVoltageSignal*(sin(x)+1);
% outputVoltagesOn = ones(onTime*controlFrequency,2);
% outputVoltagesOn(1:end,1) = LEDsignal;
% outputVoltagesOn(1:end,2) = y;
% 
% % create one cycle
% outputVoltages = outputVoltagesOn;
% % replicate to make n cycles
% outputVoltages = repmat(outputVoltages, nCycles, 1);
% outputVoltages = [outputVoltages; [0, 0]];



if twoChannels
    % duplicate channel 1, but negative
    % divide by two because voltage is split between two channels
    outputVoltages = [outputVoltages(:, 1), outputVoltages(:, 2) ./ 2, -outputVoltages(:, 2) ./ 2];
end


fprintf('Running %i cycles at %i V...\n', cycles, voltage)

queueOutputData(highVoltageControlSession,outputVoltages);
[data,time] = highVoltageControlSession.startForeground;

fprintf('done!\n\n')

labels = {'Displacement [mm]', 'Voltage [V]', 'Current [uA]'};

figure;
subplot(3,1,1), plot(time,(5000/10) * data(:, 2));
xlabel('Time [s]')
ylabel(labels{2})

subplot(3,1,2), plot(time,(200/10) * data(:, 3));
xlabel('Time [s]')
ylabel(labels{3})

subplot(3,1,3), plot(time, 10 * data(:, 1));
xlabel('Time [s]')
ylabel(labels{1})

if twoChannels
    labels = {'Displacement [mm]', 'Voltage Ch.1 [V]', 'Current Ch.1 [uA]', 'Voltage Ch.2 [V]', 'Current Ch.2 [uA]'};

    % two channels
    subplot(3,1,1)
    hold on
    plot(time,(5000/10) * data(:, 4));
    % legend('Ch. 1', 'Ch. 2', 'Location','NorthOutside');

    % two channels
    subplot(3,1,2)
    hold on
    plot(time,(200/10) * data(:, 5));
end

filename = sprintf('%s_%s_%iV_%iHz', datestr(now, 'yyyymmdd-HHMMss'), name, voltage, round(frequency));
save(filename, 'time', 'data', 'labels', 'voltage', 'frequency', 'cycles');
saveas(gcf, [filename '.png']);

end