Arduino fast/slow encoder

Support for Arduino in combination with Air Manager and Air Player

Moderators: russ, Ralph

Message
Author
surfman99
Posts: 5
Joined: Fri Jan 05, 2018 4:53 pm

Arduino fast/slow encoder

#1 Post by surfman99 »

Just wanted to share a piece of code I used to detect if the encoder is turned fast or slow. This is useful to add 10 when fast and 1 when slow (speedup the setting of the heading bug in this specific case.

Code: Select all

-- fast/slow encoder
-- A fast timer (100ms) is used to detect the time between 2 encoder move.
-- the timer is activated/reactivated at the end of each encoder move
-- if the timer is still running when the next encoder move comes then it is a fast move
-- else it is a slow move

function ApHeadingEncoder(dir)
	if timer_running(ApHdgTimer) then
		--fast change
		if (dir==1) then
			print("+10")
			ApHdg=ApHdg+10
			if(ApHdg>360) then		-- check boundaries
				ApHdg=ApHdg-360
			end
			xpl_dataref_write("CRJ/autopilot/hdg", "INT", ApHdg) --update dataref
		end
		if (dir==-1) then
			print("-10")
			ApHdg=ApHdg-10
			if(ApHdg<0) then
				ApHdg=ApHdg+360
			end			
			xpl_dataref_write("CRJ/autopilot/hdg", "INT", ApHdg)		
		end
		-- reset fast change detection timer
		timer_stop(ApHdgTimer)
		ApHdgTimer=timer_start(100,0,DummyCallback)	--100ms
	else
		--slow change
		if (dir==1) then
			print("+1")
			ApHdg=ApHdg+1
			if(ApHdg>360) then
				ApHdg=ApHdg-360
			end
			xpl_dataref_write("CRJ/autopilot/hdg", "INT", ApHdg)
		end
		if (dir==-1) then
			print("-1")
			ApHdg=ApHdg-1
			if(ApHdg<0) then
				ApHdg=ApHdg+360
			end			
			xpl_dataref_write("CRJ/autopilot/hdg", "INT", ApHdg)		
		end
		--start fast change detection timer
		ApHdgTimer=timer_start(100,0,DummyCallback)							
		
	end
end

surfman99
Posts: 5
Joined: Fri Jan 05, 2018 4:53 pm

Re: Arduino fast/slow encoder

#2 Post by surfman99 »

Forgot the timer callback. It does not need to do anything since it is the timer status itself that is used to detect fast encoder move

Code: Select all

function DummyCallback()
	dummy=0
end

User avatar
fweinrebe
Posts: 62
Joined: Mon Dec 14, 2015 10:03 am

Re: Arduino fast/slow encoder

#3 Post by fweinrebe »

surfman99: Thanks for sharing. It works beautifully in FSX as well after I made some changes. :)

I found the heading bug moving too fast so I tuned it a bit by setting the timer to 50ms and count 5 degrees in fast mode.

User avatar
PapaLima
Posts: 60
Joined: Fri Jan 05, 2018 10:49 am

Re: Arduino fast/slow encoder

#4 Post by PapaLima »

Thanks for sharing surfman

ZioZanna
Posts: 67
Joined: Wed Jul 26, 2017 8:49 pm

Re: Arduino fast/slow encoder

#5 Post by ZioZanna »

here my alternative which gives more readable code and do not need to activate timers

Code: Select all

lastms=1
SPEED_DELAY = 100
SPEED_CHANGE = 20

function isFast()
-- return true if isFast() is called faster than SPEED_DELAY
   local actualtime=os.clock()
   if (lastms+(SPEED_DELAY/1000) > actualtime) then
       lastms=actualtime
	   return true
	else
       lastms=actualtime
	   return false
	end
end

function dial_obs(direction)
 if dialtrig==1 then
  dialtrig=0
    if isFast() then change = SPEED_CHANGE else change = 1  end
    if direction == 1 then
	   value=currobs + change
	   if value > 360 then value = value - 360 end
    else
	   value=currobs - change
       if value < 0 then value = value + 360 end
    end
	fsx_variable_write("NAV OBS:1","Degrees", value)
 else
   dialtrig=1
 end
end

sogibm
Posts: 54
Joined: Fri Dec 01, 2017 2:30 am

Re: Arduino fast/slow encoder

#6 Post by sogibm »

Hello I'm an utterly newb and I need help Inserting your above script into the script I have as follows.

Code: Select all

-- Load and display text and images
img_add_fullscreen("newheadingbg.png")
img_background_compass = img_add_fullscreen("newheadingcompass.png")
img_bug = img_add_fullscreen("newheadingbug.png")
img_plane = img_add_fullscreen("plane2.png")
img_add("knobshadow.png", 376, 400, 85, 85)
-- Callback functions (handles data received from X-plane or FSX)

function new_rotation(rotation)

	img_rotate(img_background_compass, rotation * -1)

end

function new_headbug(heading, bug)

	img_rotate(img_bug, bug - heading)

end
-- Callback function which is called when the rotary encoder is turned
-- direction 1 : The dial turned clockwise
-- direciton -1: The dial turned counterclockwise
function dial_change(direction)

 if direction == -1 then
		xpl_command("sim/autopilot/heading_up")
elseif direction == 1 then
		xpl_command("sim/autopilot/heading_down")
  end

end

-- Bind to Arduino, Header P1, Pin 34 and 35
hw_dial_id = hw_dial_add ("ARDUINO_MEGA2560_A_D34" , "ARDUINO_MEGA2560_A_D35" ,  dial_change)

-- Callback function which is called when the rotary encoder is turned
-- direction 1 : The dial turned clockwise
-- direciton -1: The dial turned counterclockwise
function dial_change(direction)

 if direction == -1 then
		xpl_command("sim/instruments/DG_sync_up")
 elseif direction == 1 then
		xpl_command("sim/instruments/DG_sync_down")
  end

end

-- Bind to Arduino, Header P1, Pin 36 and 37
hw_dial_id = hw_dial_add ("ARDUINO_MEGA2560_A_D36" , "ARDUINO_MEGA2560_A_D37" ,  dial_change)

function new_knob_gyro(direction)

	if direction == -1 then
		xpl_command("sim/instruments/DG_sync_up")
		fsx_event("GYRO_DRIFT_INC")
	elseif direction == 1 then
		xpl_command("sim/instruments/DG_sync_down")
		fsx_event("GYRO_DRIFT_DEC")
	end

end

function new_knob_hdg(value)

	if value == 1 then
		fsx_event("HEADING_BUG_INC")
		xpl_command("sim/autopilot/heading_up")
	elseif value == -1 then
		fsx_event("HEADING_BUG_DEC")
		xpl_command("sim/autopilot/heading_down")
	end

end

-- DIALS ADD --
dial_knob = dial_add("gyroknob.png", 31, 395, 85, 85, new_knob_gyro)
dial_click_rotate(dial_knob,6)

dial_knob_hdg = dial_add("hdgknob.png", 376, 395, 85, 85, new_knob_hdg)
dial_click_rotate(dial_knob_hdg,6)

-- Data bus subscribe --
xpl_dataref_subscribe("sim/cockpit/gyros/psi_vac_ind_degm", "FLOAT", new_rotation)


xpl_dataref_subscribe("sim/cockpit/gyros/psi_vac_ind_degm", "FLOAT",
					  "sim/cockpit/autopilot/heading_mag", "FLOAT", new_headbug)


 
I tried on my own but utterly failed.
~Mark

ZioZanna
Posts: 67
Joined: Wed Jul 26, 2017 8:49 pm

Re: Arduino fast/slow encoder

#7 Post by ZioZanna »

i did not write any arduino code into the gauge code.

this is more confusing and less readable.

Leave the AM Gauge code without arduino and make a new gauge without graphics ( only for arduino ) instead.
You can choose to run the new arduino gauge on your panel as a normal gauge or start it by hand when if needed.

Doing like this the same arduino code can cover multiple gauges across planes.

Le me know if this helps

Andrea

User avatar
Ralph
Posts: 7933
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Arduino fast/slow encoder

#8 Post by Ralph »

That's a good suggestion. You can add the instrument to all your panels.
How to create a gauge for the Arduino without any graphics: https://www.youtube.com/watch?v=rIbLCEpWdyk

sogibm
Posts: 54
Joined: Fri Dec 01, 2017 2:30 am

Re: Arduino fast/slow encoder

#9 Post by sogibm »

ok so if i'm following you correctly, I can make a gauge without graphics that will run just my arduino code for all the gauges that I want to have rotary encoders on and I dont have to change any script or code in any of the default AM cessna gauges. this gauge will run in the background unseen since it has no graphics.
Thank you
lets see if I can do this.
~Mark

User avatar
Ralph
Posts: 7933
Joined: Tue Oct 27, 2015 7:02 pm
Location: De Steeg
Contact:

Re: Arduino fast/slow encoder

#10 Post by Ralph »

Correct.

Post Reply