# Arduino to Modbus, HELP!

**URL:** <https://forum.inductiveautomation.com/t/arduino-to-modbus-help/37760>\
**Category:** Ignition\
**Created:** [August 15, 2020, 2:50pm UTC](https://forum.inductiveautomation.com/t/arduino-to-modbus-help/37760 "2020-08-15T14:50:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [August 15, 2020, 2:50pm UTC](https://forum.inductiveautomation.com/t/arduino-to-modbus-help/37760/1 "2020-08-15T14:50:07Z")

</div>

I need help with a project I am working on, I am very new to Ignition and python, We have a gauge that needs to be aligned with respect to a string, the gauge outputs its position with respect to the string as center(Y-axis) and power(X-axis). the center(Y-axis) is pretty straight forward, it returns a positive value if the gauge is too far back and a negative value I the gauge is too far forward. unfortunately for the power(X-axis) the gauge only returns positive values as the output signal is a square function.  
I am having trouble with two things, one is how to get values from an expression tag into an OPC tag, I am using a SQL query to pull averages of the power(x-axis) and center(y-axis) values from the local database and I want to save it into the OPC tag I created to send it to the Arduino.

2nd problem.  
I am having trouble writing the logic to flip the direction in power(x-axis) if the power values get bigger over time. I wanted to write something that will create like a moving average and compare it to the current value and maybe then move the gauge and then waits to check if the power(x-value) got smaller, else it flips the direction of the motor and moves the gauge the other way.

for my setup, I am using an Arduino UNO connected to ignition through Modbus. and the Arduino is connected to two stepper motors

any help would be much appreciated

(forgive my terrible code)

```auto
def tensiongaugemovement():
	        import time
			import threading
            import system
            import sys
			
			
			# adding Tag paths
			averagepowervalue = "Tension Interface/Status/Average Tension Gauge Power Reading"
			averagecentervalue = "Tension Interface/Status/Average Tension Gauge Fiber Center"
			center_gauge = "Tension_Arduino/Center_Gauge" # this is the center value going to the arduino
			power_gauge = "Tension_Arduino/Power_Gauge" # this is the power value going to the arduino 
			center_potentiometer = "Tension_Arduino/Center Potentiometer" # potentiometer value coming from the center
			power_potentiometer = "Tension_Arduino/Power Potentiometer"	# potentiometer value coming from the power
			center = "Tension_Arduino/Center" # current center value, need to pull this data from the local database, average of last 5 mins
			power = "Tension_Arduino/Power" # same for power
			linespeed = "Line Heater Controllers/Linespeed 1" #pull in current linespeed
			
			
			
			if (linespeed > 10):
							
					center_gauge = center # send center values to arduino		

					for counter in range(5, 0 , -1):
							average = [long1, long2, long3, long5, long5]
							if (x < len(average)):
									average[x] = averagepowervalue
									x += 1
							else: 
								power_average = sum(average)/len(average)
								power_gauge = power_average
								x = 0
								counter = 5
							time.sleep(1000)		
					
					

					
					if(power_average - power < 0):
							power_gauge = -power_gauge; #flip direction of the motor 
					
					else
					# do nothing

```

Thank you

Gijo

---

<div class="post-metadata">

**Author:** ![miyanalu](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@miyanalu](https://forum.inductiveautomation.com/u/miyanalu)\
**Post date:** [August 15, 2020, 3:12pm UTC](https://forum.inductiveautomation.com/t/arduino-to-modbus-help/37760/2 "2020-08-15T15:12:39Z")

</div>

Hey, You will need to call a system.tag.readAll

```auto
tags = ["Tags/T1", "Tags/T2", "Tags/T3", "Tags/T4", "Tags/T5"]
values = system.tag.readAll(tags)

```

Similarly a system.tag.writeAll() to write data

```auto
tags = ["Tags/T1", "Tags/T2", "Tags/T3", "Tags/T4", "Tags/T5"]
values = [2, 4, 8, 16, 32]
values = system.tag.writeAll(tags,values)

```

Not too sure what the Long1, Long2 etc are doing. But I will encourage that this code run on a gateway event to ensure that is runs all the time and not on a client/session level

---

<div class="post-metadata">

**Author:** ![gijovarghese8](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/gijovarghese8/32/20571_2.png) [@gijovarghese8](https://forum.inductiveautomation.com/u/gijovarghese8)\
**Post date:** [August 15, 2020, 3:22pm UTC](https://forum.inductiveautomation.com/t/arduino-to-modbus-help/37760/3 "2020-08-15T15:22:55Z")

</div>

Hello Miyanalu,

Thank you for taking the time to help me out.

will system.tag.writeAll() be able to write each tag with five values? I wanted to take a long term average of the power value and then make the motors move. so like how can I make the script wait for the next updated reading?

Thank you

Gijo

---

<div class="post-metadata">

**Author:** ![miyanalu](https://avatars.discourse-cdn.com/v4/letter/m/e9bcb4/32.png) [@miyanalu](https://forum.inductiveautomation.com/u/miyanalu)\
**Post date:** [August 15, 2020, 3:33pm UTC](https://forum.inductiveautomation.com/t/arduino-to-modbus-help/37760/4 "2020-08-15T15:33:17Z")

</div>

call system.tag.readBlocking(). This can be seen in this manual link.

[https://docs.inductiveautomation.com/display/DOC80/system.tag.readBlocking](https://docs.inductiveautomation.com/display/DOC80/system.tag.readBlocking)

No problem

---

<div class="post-metadata">

**Author:** ![Ty\_McDiarmid](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ty_mcdiarmid/32/61684_2.png) [@Ty\_McDiarmid](https://forum.inductiveautomation.com/u/Ty_McDiarmid)\
**Post date:** [June 28, 2023, 5:57pm UTC](https://forum.inductiveautomation.com/t/arduino-to-modbus-help/37760/5 "2023-06-28T17:57:08Z")

</div>

Very interesting project, I'm curious on what compelled you to use Ignition with Arduino. Sorry I'm late to the party.
