summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSudhakarKuma2021-05-03 19:27:11 +0530
committerSudhakarKuma2021-05-03 19:27:11 +0530
commit5f297288cde71936a6c11b98e8f7940053fa78c8 (patch)
treeb2ebcfb017c05aeb086e4755c85f463f563da284
parent01c8d8fcf307ae5a84862ac89b70102dbdc006eb (diff)
downloadFLOSS-Arduino-Book-5f297288cde71936a6c11b98e8f7940053fa78c8.tar.gz
FLOSS-Arduino-Book-5f297288cde71936a6c11b98e8f7940053fa78c8.tar.bz2
FLOSS-Arduino-Book-5f297288cde71936a6c11b98e8f7940053fa78c8.zip
Maintain consistency in pot codes
-rw-r--r--tools/openmodelica/windows/Arduino.mo15
-rw-r--r--user-code/pot/OpenModelica/pot-threshold.mo51
-rw-r--r--user-code/pot/arduino/pot-threshold/pot-threshold.ino7
-rw-r--r--user-code/pot/julia/pot-threshold.jl6
-rw-r--r--user-code/pot/python/pot-threshold.py12
-rw-r--r--user-code/pot/scilab/pot-threshold.sce3
-rw-r--r--user-code/pot/scilab/pot-threshold.zcosbin6421 -> 6525 bytes
7 files changed, 45 insertions, 49 deletions
diff --git a/tools/openmodelica/windows/Arduino.mo b/tools/openmodelica/windows/Arduino.mo
index 47954f5..6680ee6 100644
--- a/tools/openmodelica/windows/Arduino.mo
+++ b/tools/openmodelica/windows/Arduino.mo
@@ -704,7 +704,7 @@ Arduino.SerialCommunication.Functions.<b>ieeesingle2num</b>(hexa);
import sComm = Arduino.SerialCommunication.Functions;
import strm = Modelica.Utilities.Streams;
Integer ok(fixed = false);
- Integer analog_in(fixed = false);
+ Integer val(fixed = false);
Integer digital_out(fixed = false);
Integer c_ok(fixed = false);
algorithm
@@ -714,25 +714,22 @@ Arduino.SerialCommunication.Functions.<b>ieeesingle2num</b>(hexa);
if ok <> 0 then
strm.print("Unable to open serial port, please check");
else
- analog_in := sComm.cmd_analog_in(1, 2) "read analog pin 2";
- strm.print("Potentiometer Readings: " + String(analog_in));
- if analog_in >= 0 and analog_in < 320 then
+ val := sComm.cmd_analog_in(1, 2) "read analog pin 2";
+ strm.print("Potentiometer Readings: " + String(val));
+ if val >= 0 and val < 320 then
digital_out := sComm.cmd_digital_out(1, 11, 1) "Turn ON LED";
sComm.delay(1000);
digital_out := sComm.cmd_digital_out(1, 11, 0) "Turn OFF LED";
- elseif analog_in >= 320 and analog_in <= 900 then
+ elseif val >= 320 and val < 900 then
digital_out := sComm.cmd_digital_out(1, 10, 1) "Turn ON LED";
sComm.delay(1000);
digital_out := sComm.cmd_digital_out(1, 10, 0) "Turn OFF LED";
- elseif analog_in > 900 and analog_in <= 1023 then
+ elseif val > 900 and val <= 1023 then
digital_out := sComm.cmd_digital_out(1, 9, 1) "Turn ON LED";
sComm.delay(1000);
digital_out := sComm.cmd_digital_out(1, 9, 0) "Turn OFF LED";
end if;
end if;
- digital_out := sComm.cmd_digital_out(1, 9, 0) "Turn OFF LED";
- digital_out := sComm.cmd_digital_out(1, 10, 0) "Turn OFF LED";
- digital_out := sComm.cmd_digital_out(1, 11, 0) "Turn OFF LED";
//Threshold 1
//Threshold 2
when time >= 10 then
diff --git a/user-code/pot/OpenModelica/pot-threshold.mo b/user-code/pot/OpenModelica/pot-threshold.mo
index e10f675..e7ec19c 100644
--- a/user-code/pot/OpenModelica/pot-threshold.mo
+++ b/user-code/pot/OpenModelica/pot-threshold.mo
@@ -9,34 +9,31 @@ model pot_threshold
algorithm
when initial() then
ok := sComm.open_serial(1, 2, 115200) "At port 2 with baudrate of 115200";
- sComm.delay(2000);
- if ok <> 0 then
- strm.print("Unable to open serial port, please check");
- else
- for i in 1:100 loop
- val := sComm.cmd_analog_in(1, 2) "read analog pin 2";
- strm.print("Potentiometer Readings:" + String(val));
- if val >= 0 and val < 320 then
- digital_out := sComm.cmd_digital_out(1, 11, 1) "Turn ON LED";
- sComm.delay(1000);
- digital_out := sComm.cmd_digital_out(1, 11, 0) "Turn OFF LED";
- elseif val >= 320 and val <= 900 then
- digital_out := sComm.cmd_digital_out(1, 10, 1) "Turn ON LED";
- sComm.delay(1000);
- digital_out := sComm.cmd_digital_out(1, 10, 0) "Turn OFF LED";
- elseif val > 900 and val <= 1023 then
- digital_out := sComm.cmd_digital_out(1, 9, 1) "Turn ON LED";
- sComm.delay(1000);
- digital_out := sComm.cmd_digital_out(1, 9, 0) "Turn OFF LED";
- end if;
- end for;
- end if;
end when;
- when terminal() then
+ if ok <> 0 then
+ strm.print("Unable to open serial port, please check");
+ else
+ val := sComm.cmd_analog_in(1, 2) "read analog pin 2";
+ strm.print("Potentiometer Readings: " + String(val));
+ if val >= 0 and val < 320 then
+ digital_out := sComm.cmd_digital_out(1, 11, 1) "Turn ON LED";
+ sComm.delay(1000);
+ digital_out := sComm.cmd_digital_out(1, 11, 0) "Turn OFF LED";
+ elseif val >= 320 and val < 900 then
+ digital_out := sComm.cmd_digital_out(1, 10, 1) "Turn ON LED";
+ sComm.delay(1000);
+ digital_out := sComm.cmd_digital_out(1, 10, 0) "Turn OFF LED";
+ elseif val > 900 and val <= 1023 then
+ digital_out := sComm.cmd_digital_out(1, 9, 1) "Turn ON LED";
+ sComm.delay(1000);
+ digital_out := sComm.cmd_digital_out(1, 9, 0) "Turn OFF LED";
+ end if;
+ end if;
+//Threshold 1
+//Threshold 2
+ when time >= 10 then
c_ok := sComm.close_serial(1) "To close the connection safely";
end when;
- //Run for 100 iterations
- //Threshold 1
- //Threshold 2
- annotation(experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 1));
+ annotation(
+ experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 1));
end pot_threshold;
diff --git a/user-code/pot/arduino/pot-threshold/pot-threshold.ino b/user-code/pot/arduino/pot-threshold/pot-threshold.ino
index ea7f88d..946e9a5 100644
--- a/user-code/pot/arduino/pot-threshold/pot-threshold.ino
+++ b/user-code/pot/arduino/pot-threshold/pot-threshold.ino
@@ -10,17 +10,18 @@ void setup() {
pinMode(RGB_RED, OUTPUT);
pinMode(RGB_GREEN, OUTPUT);
pinMode(RGB_BLUE, OUTPUT);
- for(i = 0; i < 10; i++){
+ for(i = 0; i < 20; i++){
val = analogRead(POT);
+ Serial.println(val);
if(val >= 0 & val < 320) { //threshold 1
digitalWrite(RGB_RED, HIGH);
delay(1000);
digitalWrite(RGB_RED, LOW);
- }else if(val >= 320 & val <= 900) { //threshold 2
+ }else if(val >= 320 & val < 900) { //threshold 2
digitalWrite(RGB_GREEN, HIGH);
delay(1000);
digitalWrite(RGB_GREEN, LOW);
- }else if(val > 900 & val <= 1023) { //threshold 3
+ }else if(val >= 900 & val <= 1023) { //threshold 3
digitalWrite(RGB_BLUE, HIGH);
delay(1000);
digitalWrite(RGB_BLUE, LOW);
diff --git a/user-code/pot/julia/pot-threshold.jl b/user-code/pot/julia/pot-threshold.jl
index 28a59ea..ae9fbd7 100644
--- a/user-code/pot/julia/pot-threshold.jl
+++ b/user-code/pot/julia/pot-threshold.jl
@@ -5,18 +5,18 @@ ser = ArduinoTools.connectBoard(115200)
ArduinoTools.pinMode(ser, 9, "OUTPUT")
ArduinoTools.pinMode(ser, 10, "OUTPUT")
ArduinoTools.pinMode(ser, 11, "OUTPUT")
-for i = 1:10
+for i = 1:20
val = ArduinoTools.analogRead(ser, 2)
println(val)
if (val >= 0 && val < 320)
ArduinoTools.digiWrite(ser, 11, 1)
sleep(1)
ArduinoTools.digiWrite(ser, 11, 0)
- elseif (val >= 320 && val <= 900)
+ elseif (val >= 320 && val < 900)
ArduinoTools.digiWrite(ser, 10, 1)
sleep(1)
ArduinoTools.digiWrite(ser, 10, 0)
- elseif (val > 900 && val <= 1023)
+ elseif (val >= 900 && val <= 1023)
ArduinoTools.digiWrite(ser, 9, 1)
sleep(1)
ArduinoTools.digiWrite(ser, 9, 0)
diff --git a/user-code/pot/python/pot-threshold.py b/user-code/pot/python/pot-threshold.py
index c28ab71..c0f8284 100644
--- a/user-code/pot/python/pot-threshold.py
+++ b/user-code/pot/python/pot-threshold.py
@@ -26,20 +26,20 @@ class POT:
self.red = 11
for i in range(20):
val = self.obj_arduino.cmd_analog_in(1, self.pot)
- print (val)
+ print(val)
if (int(val) >= 0 and int(val) < 320):
- self.obj_arduino.cmd_digital_out(1, self.blue, 1)
+ self.obj_arduino.cmd_digital_out(1, self.red, 1)
sleep(1)
- self.obj_arduino.cmd_digital_out(1, self.blue, 0)
+ self.obj_arduino.cmd_digital_out(1, self.red, 0)
elif (int(val) >= 320 and int(val) < 900):
self.obj_arduino.cmd_digital_out(1, self.green, 1)
sleep(1)
self.obj_arduino.cmd_digital_out(1, self.green, 0)
- else:
- self.obj_arduino.cmd_digital_out(1, self.red, 1)
+ elif (int(val) >= 900 and int(val) <= 1023):
+ self.obj_arduino.cmd_digital_out(1, self.blue, 1)
sleep(1)
- self.obj_arduino.cmd_digital_out(1, self.red, 0)
+ self.obj_arduino.cmd_digital_out(1, self.blue, 0)
def exit(self):
self.obj_arduino.close_serial()
diff --git a/user-code/pot/scilab/pot-threshold.sce b/user-code/pot/scilab/pot-threshold.sce
index 3f9c92f..8331a88 100644
--- a/user-code/pot/scilab/pot-threshold.sce
+++ b/user-code/pot/scilab/pot-threshold.sce
@@ -1,7 +1,8 @@
ok = open_serial(1, 2, 115200); // port 2, baud rate 115200
if ok ~= 0 then error('Unable to open serial port, please check'); end
-for x = 1:10 //Run for 10 iterations
+for x = 1:20 //Run for 20 iterations
val = cmd_analog_in(1, 2)
+ disp(val)
if (val >= 0 & val < 320) then //threshold 1
cmd_digital_out(1, 11, 1)
sleep(1000)
diff --git a/user-code/pot/scilab/pot-threshold.zcos b/user-code/pot/scilab/pot-threshold.zcos
index 28052ce..2ad6f5e 100644
--- a/user-code/pot/scilab/pot-threshold.zcos
+++ b/user-code/pot/scilab/pot-threshold.zcos
Binary files differ