<root>
<document scada='0' schema='103' license='10,CompMaker,WNV3JN' title='Developer Project' description='' fcversion='655618' target='ARD.ATMEGA.Arduino Uno R3 PDIP' >
	<config data='' clkspd='16000000' simspd='2' usewdt='0' constif='0' commport='3' Use3V3='0' />
	<plugins >
		<dll_models enabled='1' />
	</plugins>
	<supplement use='0' head='' body='' />
	<debug >
		<watch expr='Speed_Control' />
		<watch expr='Motor_Output' />
	</debug>
	<traces />
	<ghost >
		<FK2 >
			<data name='FK2D0' port='4294967295' pin='4294967295' />
			<data name='FK2D1' port='4294967295' pin='4294967295' />
			<data name='FK2D2' port='4294967295' pin='4294967295' />
			<data name='FK2D3' port='4294967295' pin='4294967295' />
			<data name='FK2D4' port='4294967295' pin='4294967295' />
			<data name='FK2D5' port='4294967295' pin='4294967295' />
			<data name='FK2D6' port='4294967295' pin='4294967295' />
			<data name='FK2D7' port='4294967295' pin='4294967295' />
			<data name='FK2D8' port='4294967295' pin='4294967295' />
			<data name='FK2D9' port='4294967295' pin='4294967295' />
			<data name='FK2D10' port='4294967295' pin='4294967295' />
			<data name='FK2D11' port='4294967295' pin='4294967295' />
			<data name='FK2D12' port='4294967295' pin='4294967295' />
			<data name='FK2D13' port='4294967295' pin='4294967295' />
			<data name='FK2D14' port='4294967295' pin='4294967295' />
			<data name='FK2D15' port='4294967295' pin='4294967295' />
			<data name='FK2A0' port='4294967295' pin='4294967295' />
			<data name='FK2A1' port='4294967295' pin='4294967295' />
			<data name='FK2A2' port='4294967295' pin='4294967295' />
			<data name='FK2A3' port='4294967295' pin='4294967295' />
			<data name='FK2A4' port='4294967295' pin='4294967295' />
			<data name='FK2A5' port='4294967295' pin='4294967295' />
		</FK2>
		<ICD >
			<data name='AnalogPrescaleValue' value='19' />
			<data name='DigitalSampleRate' value='100000' />
			<data name='BreakpointCount' value='8' />
			<data name='CallStackDepthCount' value='8' />
			<data name='ClockPort' value='3' />
			<data name='ClockPin' value='1' />
			<data name='DataPort' value='3' />
			<data name='DataPin' value='0' />
			<data name='UseDefaultPins' value='1' />
			<data name='WrapEnabled' value='1' />
			<data name='CommsDelayOverridden' value='0' />
			<data name='CommsDelay' value='11' />
			<data name='CalculatedCommsDelay' value='11' />
			<data name='AnalogEB2PrescaleValue' value='2' />
			<data name='DigitalEB2SampleRate' value='100000' />
		</ICD>
		<pins >
			<digital A='0' B='0' C='0' D='0' E='0' F='0' G='0' H='0' I='0' J='0' K='0' L='0' M='0' N='0' O='0' P='0' Q='0' R='0' S='0' T='0' U='0' V='0' W='0' X='0' Y='0' Z='0' />
			<analog A='0' B='0' C='0' D='0' E='0' F='0' G='0' H='0' I='0' J='0' K='0' L='0' M='0' N='0' O='0' P='0' Q='0' R='0' S='0' T='0' U='0' V='0' W='0' X='0' Y='0' Z='0' />
		</pins>
	</ghost>
	<components >
		<settings autoimg='0' center='1' unitscale='0' fixedscale='0' fixedx='25' fixedy='25' fixedz='25' headcode='0' />
		<definition guid='ece223e6-72d5-41bd-96bc-205b45c48b41' vstate='40' vmin='0' vmaj='1' srcleaf='' visiblename='' description='' category='' category2='' category3='' bIs2dOnly='0' bIs3dOnly='0' catenable='1' author='' manuname='' manucode='' sysinfo='0' keywords='' dynamic='1' scadaCompatible='0' embeddedCompatible='1' showmacros='1' iconpath='' />
		<component class_type='root' codename='ComponentRoot' panelId='-1' x='0' y='0' z='0' xsz='1' ysz='1' zsz='1' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' scadavisible='1' interactive='1' solid='1' layer='0' poslock='0' comp2dType='0' >
			<resources />
			<properties />
			<values />
			<events />
			<apis />
			<variables >
				<variable public='0' >
					<def class_type='variable' name='Duty' type='u8' description='' isconst='0' isHidden='0' isinit='1' usrinit='0' setinit='0' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='false' type='b1' description='' isconst='1' isHidden='0' isinit='1' usrinit='0' setinit='0' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='Frequency_PWM' type='u32' description='' isconst='0' isHidden='0' isinit='0' usrinit='0' setinit='' />
				</variable>
				<variable public='0' >
					<def class_type='variable' name='true' type='b1' description='' isconst='1' isHidden='0' isinit='1' usrinit='1' setinit='1' />
				</variable>
			</variables>
			<macros >
				<macro >
					<flowline name='Main' description='' statediag='0' >
						<return name='Return' type='v0' description='' isconst='0' isHidden='0' isinit='0' usrinit='' setinit='' />
						<command class_type='call' title='Component Macro' cmdcolor='4487093' cmdcolor_sec='11066367' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' component='UART1' macro='Initialise' />
						<command class_type='native' title='Timer1 set up for pin B2' ccode='void setupPWM(uint32_t frequency, uint8_t dutyCycle) {
    uint16_t prescaler;
    uint8_t prescalerBits;
    uint16_t ocrValue;

    // Dynamically select prescaler and calculate OCR1A
    if (frequency &gt; 19800) {         // Use prescaler 1
        prescaler = 1;
        prescalerBits = (1 &lt;&lt; CS10);
    } else if (frequency &gt; 2470) {   // Use prescaler 8
        prescaler = 8;
        prescalerBits = (1 &lt;&lt; CS11);
    } else if (frequency &gt; 618) {    // Use prescaler 64
        prescaler = 64;
        prescalerBits = (1 &lt;&lt; CS11) | (1 &lt;&lt; CS10);
    } else if (frequency &gt; 154) {    // Use prescaler 256
        prescaler = 256;
        prescalerBits = (1 &lt;&lt; CS12);
    } else {                         // Use prescaler 1024
        prescaler = 1024;
        prescalerBits = (1 &lt;&lt; CS12) | (1 &lt;&lt; CS10);
    }

    // Calculate TOP value for the desired frequency
    ocrValue = (F_CPU / (prescaler * frequency)) - 1;

    // Clear Timer1 settings
    TCCR1A = 0;
    TCCR1B = 0;

    // Set Timer1 in Fast PWM mode with TOP = ICR1
    TCCR1A |= (1 &lt;&lt; WGM11);     // Fast PWM mode with ICR1 as TOP
    TCCR1B |= (1 &lt;&lt; WGM13) | (1 &lt;&lt; WGM12);  // Fast PWM mode with ICR1 as TOP

    // Set PB2 (OC1B) as output
    DDRB |= (1 &lt;&lt; PB2);

    // Set TOP value
    ICR1 = ocrValue;

    // Handle duty cycle
    if (dutyCycle == 0) {
        // For 0% duty, disconnect PWM and force output low
        TCCR1A &amp;= ~((1 &lt;&lt; COM1B1) | (1 &lt;&lt; COM1B0));  // Disconnect PWM
        PORTB &amp;= ~(1 &lt;&lt; PB2);  // Force output low
        OCR1B = 0;
    } else if (dutyCycle &gt;= 100) {
        // For 100% duty, disconnect PWM and force output high
        TCCR1A &amp;= ~((1 &lt;&lt; COM1B1) | (1 &lt;&lt; COM1B0));  // Disconnect PWM
        PORTB |= (1 &lt;&lt; PB2);   // Force output high
        OCR1B = ocrValue;
    } else {
        // Normal PWM operation
        TCCR1A |= (1 &lt;&lt; COM1B1);    // Clear OC1B on Compare Match, set at BOTTOM
        uint16_t dutyCycleValue = ((uint32_t)ocrValue * dutyCycle) / 100;
        OCR1B = dutyCycleValue;
    }

    // Set prescaler and start timer
    TCCR1B |= prescalerBits;
}' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
						<command class_type='native' title='Timer2' disable='1' ccode='void setupPWM(uint32_t frequency, uint8_t dutyCycle) {
    uint16_t prescaler;
    uint8_t prescalerBits;
    uint8_t ocrValue;

    // Dynamically select prescaler and calculate OCR2A
    if (frequency &gt; 19800) {         // Use prescaler 1
        prescaler = 1;
        prescalerBits = (1 &lt;&lt; CS20);
    } else if (frequency &gt; 4950) {   // Use prescaler 8
        prescaler = 8;
        prescalerBits = (1 &lt;&lt; CS21);
    } else if (frequency &gt; 2470) {   // Use prescaler 32
        prescaler = 32;
        prescalerBits = (1 &lt;&lt; CS21) | (1 &lt;&lt; CS20);
    } else if (frequency &gt; 1240) {   // Use prescaler 64
        prescaler = 64;
        prescalerBits = (1 &lt;&lt; CS22);
    } else if (frequency &gt; 618) {    // Use prescaler 128
        prescaler = 128;
        prescalerBits = (1 &lt;&lt; CS22) | (1 &lt;&lt; CS20);
    } else {                         // Use prescaler 256
        prescaler = 256;
        prescalerBits = (1 &lt;&lt; CS22) | (1 &lt;&lt; CS21);
    }

    // Calculate TOP value for the desired frequency
    uint16_t tempOCR = (F_CPU / (prescaler * frequency)) - 1;
    
    // Constrain to 8-bit range
    ocrValue = (tempOCR &gt; 255) ? 255 : tempOCR;

    // Clear Timer2 settings
    TCCR2A = 0;
    TCCR2B = 0;

    // Set Timer2 in Fast PWM mode with TOP = OCR2A
    TCCR2A |= (1 &lt;&lt; WGM21) | (1 &lt;&lt; WGM20);  // Fast PWM mode
    TCCR2B |= (1 &lt;&lt; WGM22);     // Fast PWM mode with OCR2A as TOP

    // Set PD3 (OC2B) as output
    DDRD |= (1 &lt;&lt; PD3);

    // Set TOP value
    OCR2A = ocrValue;

    // Handle duty cycle
    if (dutyCycle == 0) {
        // For 0% duty, disconnect PWM and force output low
        TCCR2A &amp;= ~((1 &lt;&lt; COM2B1) | (1 &lt;&lt; COM2B0));  // Disconnect PWM
        PORTD &amp;= ~(1 &lt;&lt; PD3);  // Force output low
        OCR2B = 0;
    } else if (dutyCycle &gt;= 100) {
        // For 100% duty, disconnect PWM and force output high
        TCCR2A &amp;= ~((1 &lt;&lt; COM2B1) | (1 &lt;&lt; COM2B0));  // Disconnect PWM
        PORTD |= (1 &lt;&lt; PD3);   // Force output high
        OCR2B = ocrValue;
    } else {
        // Normal PWM operation
        TCCR2A |= (1 &lt;&lt; COM2B1);    // Clear OC2B on Compare Match, set at BOTTOM
        uint8_t dutyCycleValue = ((uint16_t)ocrValue * dutyCycle) / 100;
        OCR2B = dutyCycleValue;
    }

    // Set prescaler and start timer
    TCCR2B |= prescalerBits;
}' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
						<command class_type='calculation' title='Calculation' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' >
							<exp exp='Frequency_PWM = 120000 // = 120KHz' />
						</command>
						<command class_type='loop' title='Loop' cmdcolor='4767473' cmdcolor_sec='11337727' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' type='0' exp='1' >
							<flowline >
								<command class_type='call' title='Component Macro' cmdcolor='4487093' cmdcolor_sec='11066367' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' component='Potentiometer1' macro='GetAverageByte' >
									<return exp='Duty' />
									<argument exp='255' />
									<argument exp='255' />
								</command>
								<command class_type='call' title='Component Macro' cmdcolor='4487093' cmdcolor_sec='11066367' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' component='MapFunction1' macro='MapByte' >
									<argument exp='Duty' />
									<argument exp='0' />
									<argument exp='255' />
									<argument exp='0' />
									<argument exp='100' />
									<argument exp='1' />
								</command>
								<command class_type='native' title='Works withthe above c code' ccode='setupPWM(FCV_FREQUENCY_PWM, FCV_DUTY);' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' language='C' />
								<command class_type='call' title='Component Macro' cmdcolor='4487093' cmdcolor_sec='11066367' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' component='UART1' macro='SendString' >
									<argument exp='&quot;\n\rDuty = &quot;' />
								</command>
								<command class_type='call' title='Component Macro' cmdcolor='4487093' cmdcolor_sec='11066367' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' component='UART1' macro='SendNumber' >
									<argument exp='Duty' />
								</command>
								<command class_type='delay' title='Delay' cmdcolor='4206456' cmdcolor_sec='10785756' cmdcolor_txt='16777215' cmdgradient='4' cmdopacity='0.64' exp='100' type='1' />
							</flowline>
						</command>
					</flowline>
				</macro>
			</macros>
			<component class_type='ref' guid='11ca2f38-de81-4812-9338-4b5a7a89a658' vmin='0' vmaj='1' codename='Potentiometer1' panelId='0' x='177' y='-116.5' z='0' xsz='25' ysz='25.036' zsz='25' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' scadavisible='1' interactive='1' solid='1' layer='2' poslock='0' comp2dType='0' >
				<resources />
				<properties />
				<values >
					<value target='adc_base1::channel' data='0' />
					<value target='adc_base1::vrefvol' data='500' />
					<value target='adc_base1::vrefop' data='1' />
					<value target='adc_base1::convspd' data='007' />
					<value target='adc_base1::actime' data='40' />
					<value target='adc_base1::bits' data='10' />
					<value target='VrefForCalculations' data='' />
					<value target='BitDepth' data='10' />
					<value target='SetValue' data='2.112084' />
					<value target='RetainValue' data='1' />
					<value target='ModifiedConnection' data='An 0' />
					<value target='ComponentLabel' data='001' />
					<value target='LabelColour' data='0' />
					<value target='GdiText2::sText' data='Speed Control' />
					<value target='ConnectionLabels' data='1' />
					<value target='ShowPinValue' data='1' />
					<value target='Style' data='3' />
					<value target='Gauge1::brFill' data='288230376147764163' />
					<value target='Gauge1::colFrameOutline' data='9868950' />
					<value target='Gauge1::brFrameFill' data='261490253360251843' />
					<value target='Gauge1::colPointerOutline' data='9868950' />
					<value target='Gauge1::brPointerFill' data='261490253360251843' />
					<value target='Gauge1::colText' data='0' />
					<value target='Gauge1::colTickMarkOutline' data='0' />
					<value target='Gauge1::brTickMarkFill' data='12829635' />
					<value target='Gauge1::colCapOutline' data='12829635' />
					<value target='Gauge1::brCapFill' data='261490253360251843' />
					<value target='Gauge1::sMainImage' data='' />
					<value target='Gauge1::sCapImage' data='' />
					<value target='GdiGauge1::nPointerKnob' data='  0' />
					<value target='GdiSwitch1::nType' data='  3' />
					<value target='UpperBoundValue' data='5.000050' />
					<value target='adc_base1::ScopeTraces' data='0' />
					<value target='StoredColourStyle' data='3' />
					<value target='FlowcodeType' data='0' />
					<value target='Enabled' data='1' />
					<value target='Product' data='ARD' />
				</values>
				<events />
				<apis />
				<variables />
				<macros />
			</component>
			<component class_type='ref' guid='ccdce31d-a1a3-437a-88ba-5431745ddf6e' vmin='0' vmaj='2' codename='MapFunction1' panelId='0' x='256.5' y='-286' z='1' xsz='25.18' ysz='25' zsz='25' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' scadavisible='1' interactive='1' solid='1' layer='2' poslock='0' comp2dType='0' >
				<resources />
				<properties />
				<values />
				<events />
				<apis />
				<variables />
				<macros />
			</component>
			<component class_type='ref' guid='c73616d1-7ad0-45d9-aada-69ccfa4c5efc' vmin='0' vmaj='3' codename='UART1' panelId='0' x='106.495' y='-284' z='2' xsz='25.1778' ysz='25.1392' zsz='25' xang='0' yang='0' zang='0' xquat='0' yquat='0' zquat='0' wquat='1' visible='1' scadavisible='1' interactive='1' solid='1' layer='2' poslock='1' comp2dType='0' >
				<resources />
				<properties />
				<values >
					<value target='cal_uart::CHANNEL' data='001' />
					<value target='cal_uart::BAUD_LIST' data='004' />
					<value target='cal_uart::BAUD' data='9600' />
					<value target='cal_uart::DBITS' data='008' />
					<value target='cal_uart::RETURN' data='001' />
					<value target='cal_uart::ECHO' data='000' />
					<value target='cal_uart::UseTX' data='1' />
					<value target='cal_uart::TX' data='$PORTD.1' />
					<value target='cal_uart::OutputPins' data='010' />
					<value target='cal_uart::TXAlt' data='0' />
					<value target='cal_uart::UseRX' data='0' />
					<value target='cal_uart::RX' data='$PORTD.0' />
					<value target='cal_uart::InputPins' data='001' />
					<value target='cal_uart::RXAlt' data='0' />
					<value target='cal_uart::FLOWEN' data='000' />
					<value target='cal_uart::RTS' data='$PORTB.2' />
					<value target='cal_uart::CTS' data='$PORTB.1' />
					<value target='label' data='UART' />
					<value target='cal_uart::ScopeTraces' data='0' />
					<value target='cal_uart::ConsoleData' data='1' />
					<value target='cal_uart::ConsoleFormat' data='001' />
					<value target='cal_uart::ConsoleColumns' data='64' />
					<value target='cal_uart::DataSource' data='000' />
					<value target='cal_uart::com_port' data='000' />
					<value target='cal_uart::Injector' data='000' />
					<value target='cal_uart::APIUseDDR' data='' />
					<value target='cal_uart::APIDDRPin' data='' />
					<value target='cal_uart::APIDDRRXState' data='' />
					<value target='cal_uart::APIUseFC' data='' />
					<value target='cal_uart::APICTSPin' data='' />
					<value target='cal_uart::APIRTSPin' data='' />
				</values>
				<events />
				<apis />
				<variables />
				<macros />
			</component>
		</component>
	</components>
	<scadaresourcelookup />
	<keymap />
	<panel2d shadows='1' lighting='2' brightness='0' >
		<background rgb='7292207' img='' style='0' />
		<camera xe='0' ye='0' ze='1974.68' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
		<viewport dx='435' dy='748' zoom='26.655' fix_topleft='0' />
		<page x='1000' y='1000' show='0' rgb='7360576' />
		<winpos ID='7005' RectRecentFloat='1030,99,1920,892' RectRecentDocked='0,0,435,777' RecentFrameAlignment='16384' RecentRowIndex='0' IsFloating='0' MRUWidth='32767' PinState='0' IsMaximized='0' IsVisible='0' />
	</panel2d>
	<panel3d shadows='0' lighting='2' brightness='0' perspective='1' >
		<background rgb='8409120' img='' style='0' />
		<table rgb='7360576' img='' style='0' size='0' />
		<camera xe='0' ye='0' ze='781.66' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
		<camerakey0 xe='0' ye='0' ze='300' xt='0' yt='0' zt='0' xquat='0' yquat='0' zquat='0' wquat='1' />
		<camerakey1 xe='-3.67394e-14' ye='0' ze='-300' xt='0' yt='0' zt='0' xquat='0' yquat='1' zquat='0' wquat='6.12323e-17' />
		<camerakey2 xe='0' ye='-300' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-0.707107' yquat='0' zquat='0' wquat='0.707107' />
		<camerakey3 xe='-3.67394e-14' ye='300' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-4.32978e-17' yquat='0.707107' zquat='0.707107' wquat='4.32978e-17' />
		<camerakey4 xe='-300' ye='-6.66134e-14' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-0.5' yquat='0.5' zquat='0.5' wquat='0.5' />
		<camerakey5 xe='300' ye='-6.66134e-14' ze='6.66134e-14' xt='0' yt='0' zt='0' xquat='-0.5' yquat='-0.5' zquat='-0.5' wquat='0.5' />
		<camerakey6 xe='-173.205' ye='-173.205' ze='173.205' xt='0' yt='0' zt='0' xquat='-0.424708' yquat='0.17592' zquat='0.339851' wquat='0.820473' />
		<winpos ID='7004' RectRecentFloat='1270,351,1920,801' RectRecentDocked='0,0,418,812' RecentFrameAlignment='16384' RecentRowIndex='0' IsFloating='0' MRUWidth='32767' PinState='0' IsMaximized='0' IsVisible='0' />
	</panel3d>
	<panels2d count='1' >
		<panel2d_0 id='0' name='2D Panel' >
			<winpos ID='1171' RectRecentFloat='1131,357,1558,796' RectRecentDocked='1278,183,1920,995' RecentFrameAlignment='16384' RecentRowIndex='0' IsFloating='1' MRUWidth='32767' PinState='0' IsMaximized='0' IsVisible='1' />
			<background fill='288230376151053813' showgrid='1' gridstyle='1' gridsize='10' gridbrush='71776119075691740' snaptogrid='1' />
		</panel2d_0>
	</panels2d>
	<layout >
		<view type='0' name='Main' mode='0' placement='LAAAAAAAAAABAAAA-----------4----4f---wUAAAAeAAAAlwUAACcDAAA' zoom='100' scrollx='0' scrolly='0' flags='0' />
	</layout>
</document>
</root>
