clc
clear

  a = arduino()
  writePWMVoltage(a,"D10",0)
  % Set up figure, get properties, and label
  figure
  
  h = animatedline;
  ax = gca;
  ax.YLim = [0 10];
  xlabel('Time (s)')
  ylabel('Voltage')
  % Start a timer
  tic
  startTime = toc;
  % Loop for 20 secounds
  i = 1;
  while toc < 10
    % Random data
    v(i) = readVoltage(a,'A0');
    if i == 1
    max = v(1);
    min = v(1);
    end
    if v(i) > max
        max = v(i);
    else if v(i) < min
        min = v(i);
    end
    end

    % Get current time and add to animation
    t(i) = toc - startTime;
    addpoints(h,t(i),v(i))
    % Update axes
    if t(i) < 5
        ax.XLim = [0 10];
        drawnow
    else
        ax.XLim = [t(i)-5 t(i)+5];
        drawnow
    end
    i = i+1;
  end

    while toc < 25 && toc >  10
    % Random data
    v(i) = readVoltage(a,'A0');
    voltz =  ((max - min)-(v(i)- min))/(max - min);
    if voltz > 1
        voltz = 1;
    end
    if voltz < 0 
            voltz = 0;
    end
    writePWMDutyCycle(a,"D10",voltz);
    % Get current time and add to animation
    t(i) = toc - startTime;
    addpoints(h,t(i),v(i))
    % Update axes
    if t(i) < 5
        ax.XLim = [0 10];
        drawnow
    else
        ax.XLim = [t(i)-5 t(i)+5];
        drawnow
    end
  end
    i = i + 1;

writePWMVoltage(a,'D10',0)