Mitsubishi Melfa Basic VI - If..Then..Elseif..Endif syntax errors - robotics

Using Mitsubishi's RT Toolbox3 and Melfa Basic VI programming language, I am trying to get the syntax correct for an If...Then...Elseif...Endif statement.
I would like this robot to assess 3 external Inputs and based on their state run different Subroutines. If none of these inputs are true I want the robot to go to a "Waiting" position, we'll label this position posXXX.
Here is what is currently running in this robot. This script selects the proper subroutine but doesn't account for the condition when none of the inputs are true. When I attempt to insert the "Else" Goto posXXX I get syntax errors.
If M_In(11) = 1 Then *Box1
If M_In(12) = 1 Then *Box2
If M_In(13) = 1 Then *Box3
Thank you.

Below is the script that resolved my question.
*BoxEval
Mvs posPostScan
If M_In(10) = 1 Then
GoSub *Box6
Break
ElseIf M_In(11) = 1 Then
GoSub *Box1
Break
ElseIf M_In(12) = 1 Then
GoSub *Box2
Break
ElseIf M_In(13) = 1 Then
GoSub *Box3
Break
ElseIf M_In(14) = 1 Then
GoSub *Box4
Break
ElseIf M_In(15) = 1 Then
GoSub *Box5
Break
Else
GoSub *BoxEval
EndIf

Related

Using loop to solve if number < than 200 previous numbers, return 1

I'm writing a small program in lua and I was hoping to get some pointers on what would be the most correct way to approach this. Basically, if my number is bigger than the last 200 numbers, return 1, else return 0.
I figure the best way would be a loop?
Let's say x is the position of my number in a table, and then I want to check that it's bigger than the previous 200 numbers and return 1 at the end if it is and 0 if it's not.
while (x > x-a)
do
isbigger = 1;
a = a+1;
return isbigger;
end
I'm not sure if that's correct syntax wise, it also would technically return 1 everytime it goes through the loop and I just want it at the end if true that my number is bigger than the 200 previous one. Also, how do get out of the loop if its false (I'd guess with a break)?
Thanks for any help and have a nice day!
If you are looping over an array of values, you should use a for loop. you also do not want to return isbigger from inside the loop as it will prematurely end the loop
local last = 10
local myNumber = 123
local numbers = {}
--Setup our example numbers table
math.randomseed(os.clock())
for i = 1, 40 do
numbers[i] = math.random(1,200)
end
--Run comparison on our the last x values
local isBigger = true
for i = #numbers, #numbers - last, -1 do
print(myNumber, numbers[i], myNumber > numbers[i])
isBigger= isBigger and myNumber > numbers[i]
end
print("isBigger is: " .. isBigger)
return isBigger and 1 or 0
Example Output
123 181 false
123 6 true
123 77 true
123 78 true
123 145 false
123 130 false
123 104 true
123 114 true
123 6 true
123 4 true
123 15 true
isBigger is: false
The for loop above is better for understanding what is happening, but this one is better for use as it will exit once it has found an result that shows myNumber is not bigger.
local isBigger = true
for i = #numbers, #numbers - last, -1 do
print(myNumber, numbers[i], myNumber > numbers[i])
if not (myNumber > numbers[i]) then
isBigger = false
break
end
end

Can anybody please tell me how to set or reset a bit in lua..?

I want to perform set and reset of particular bit in a number. As I'm using lua 5.1 I can't able to use APIs and shifting operators so it is becoming more and more complex so please help me finding this
bit library is shipped with the firmware.
Read the documentation: https://nodemcu.readthedocs.io/en/release/modules/bit/
You can do it without external libraries, if you know the position of the bit you wish to flip.
#! /usr/bin/env lua
local hex = 0xFF
local maxPos = 7
local function toggle( num, pos )
if pos < 0 or pos > maxPos then print( 'pick a valid pos, 0-' ..maxPos )
else
local bits = {} -- populate emtpy table
for i=1, maxPos do bits[i] = false end
for i = maxPos, pos +1, -1 do -- temporarily throw out the high bits
if num >= 2 ^i then
num = num -2 ^i
bits [i +1] = true
end
end
if num >= 2 ^pos then num = num -2 ^pos -- flip desired bit
else num = num +2 ^pos
end
for i = 1, #bits do -- add those high bits back in
if bits[i] then num = num +2 ^(i -1) end
end
end ; print( 'current value:', num )
return num
end
original value: 255
current value: 127
pick a valid pos, 0-7
current value: 127
current value: 255

Roblox MouseButton1Down click not working

I tried several ways, but nothing worked. There's my code now:
script.Parent.MouseButton1Down:Connect(function()
print("adding money to your account...")
if game.Players.LocalPlayer.leaderstats.Money.Value == 0 then
game.Players.LocalPlayer.leaderstats.Money.Value = 1
return
end
game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value + 1
print("done")
end)
I know using this game.Players.LocalPlayer is not the best way, I already tried like this:
script.Parent.MouseButton1Down:Connect(function(plr)
print("adding money to your account...")
if plr.leaderstats.Money.Value == 0 then
plr.leaderstats.Money.Value = 1
return
end
plr.leaderstats.Money.Value = plr.leaderstats.Money.Value + 1
print("done")
end)
Wsha's answer is correct but here's the explanation.
Lets say we have a basic addition script here:
number1 = 0
And we want to add 1 to that. By just typing:
number1 + 1
You're essentially adding 1 to the variable itself, and not the variables value. So by referencing the variables value using:
number1 = number1 + 1
The script knows that the new value in the number 1 variable is the current value add 1.
This should suffice
script.Parent.MouseButton1Down:Connect(function(plr)
print("adding money to your account...")
game.Players.LocalPlayer.leaderstats.Money.Value =
game.Players.LocalPlayer.leaderstats.Money.Value + 1
print("done")
return

Lua with calculator script

I'm trying to create a calculator for my own use . I don't know how to make it so that when the user inputs e.g. 6 for the prompt lets the user type in 6 numbers. So if I wrote 7 , it would give me an option to write 7 numbers and then give me the answer, And if i wrote 8 it will let me write 8 numbers...
if choice == "2" then
os.execute( "cls" )
print("How many numbers?")
amountNo = io.read("*n")
if amountNo <= 2 then print("You cant have less than 2 numbers.")
elseif amountNo >= 14 then print("Can't calculate more than 14 numbers.")
elseif amountNo <= 14 and amountNo >= 2 then
amountNmb = amountNo
if amountNmb = 3 then print(" Number 1")
print("Type in the numbers seperating by commas.")
local nmb
print("The answer is..")
The io.read formats are a bit limiting.
If you want a comma-separated list to be typed, I suggested reading a whole line and then iterating through each value:
local line = io.input("*l")
local total = 0
-- capture a sequence of non-comma chars, which then might be followed by a comma
-- then repeat until there aren't any more
for item in line:gmatch("([^,]+),?") do
local value = tonumber(item)
-- will throw an error if item does not represent a number
total = total + value
end
print(total)
This doesn't limit the count of values to any particular value—even an empty list works. (It is flawed in that it allows the line to end with a comma, which is ignored.)
From what I understand, you want the following:
print "How many numbers?"
amountNo = io.read "*n"
if amountNo <= 2 then
print "You can't have less than 2 numbers."
elseif amountNo >= 14 then
print "Can't calculate more than 14 numbers."
else
local sum = 0
for i = 1, amountNo do
print( ('Enter number %s'):format(i) )
local nmb = io.read '*n'
sum = sum + nmb
end
print( ('The sum is: %s'):format(sum) )
end
If the user separates the numbers with commas, they don't need to state how many they want to add, you can just get them all with gmatch. Also with the right pattern you can ensure you only get numbers:
local line = io.input()
local total = 0
-- match any number of digits, skipping any non-digits
for item in line:gmatch("(%d+)") do
total = total + item
end
With input '4 , 6 , 1, 9,10, 34' (no quotes), then print(total) gives 64, the correct answer

Balloon game with Corona SDK

I am totally new to game development for iPhone/iPad.
I have got my code working and all the 10 balloons are floating in the air but I have a few questions:
The balloons should be in the sequence or random order. They move the tendon to the edge and then the player should move back the balloons to the right place with the mouse. How?
What are the right dimensional numbers (x, y) so my balloons are equally displayed and positioned on the screen?
My random function keeps popping out more balloons by simple click.
I would like the user to perform some math operations, for instance add two random balloons and display the right answer on the screen so the result can move back to the right edge of balloon placement. how to code this? How can I use 2 different level of difficulties? (L1, L2)
How to make my balloons to move to the different edges on the screen?
How can a user move back the balloons with mouse to the right places?
How can I tie my balloons to a rope (horizontally)? so the user can make a choice.
My background image is about 3MB original(1024 x 768) to match well with iPad resolution, can I change the size without affecting the display in iPad?
I feel like the local balloon1, 2, 3, is repeated too much, and same goes to moveBalloon and applyLinear. Is there a way of shortening them? or is it normal since there are 10 balloons?
I have added sound to the first balloon by simple click, should duplicate the same function for the rest of the 9 balloons (another mess)? I will use the same sound to all.
Your feedback is much appreciated.
If you want multiple balloons, it would be MUCH MUCH easier to use a table. You can have as many baloons as you want with very little effort.
Balloons = {} -- track all baloons
Function addBalloon(x,y,xVel,yVel)
Tab = {x = x, y = y, vel = {x = xVel, y = yVel}}
Table.insert(balloons,tab)
End
Function moveAllBalloons()
For_,i in pairs(balloons) do
i.x = i.x + i.vel.x
i.y = i.y + i.vel.y
End
End
Function isPlaying
For _,i in pairs(balloons)
If --[[mouse.x]] <= i.x - (balloon.width/2) and --[[other parameters here]] then
PlaySound
End
End
End
for different difficulties, you can do something like
if L1 then
Num1 = math.random(3,15)
Num2 = math.random(3,15
OpFind = math.random(3)
If opfind == 1 then
Operation = "+"
Elseif opfind == 2 then
Operation = "-"
Elseif opfind ==3 then
Operation = "*"
End
ElseIf L2 then
num1 = math.random(7,50)
Num2 = math.random(7,50)
OpFind = math.random(4)
If opfind == 1 then
Operation = "^"
Elseif opfind == 2 then
Operation = "%"
Elseif opfind ==3 then
Operation = "*"
Elseif opfind == 4 then
Operation == "/"
End
End

Resources