Using Maxuino to read from an ultrasonic sensor

    Jan 11 2018 | 4:54 am
    Hey all,
    I'm working with max, an arduino uno (loaded with standard firmata) with the maxuino object pretty happily to get two motors working for a robotics project, but I need to connect up an ulatrasonic sensor too. The practical connecting seems pretty easy: UCC to 5v
    Trig to PWM pin (digital)
    Echo to Normal (digital)
    Gnd to Ground But I can't seem to find anything about how to write the code to actually view the data. From what I understand I'll need to trig the sensor, and read the echo, but I'm struggling to find any code to help. Anyone have a basic patch they could share?

    • Jan 11 2018 | 7:58 am
    • Jan 11 2018 | 10:13 am
      I found this, and the theory was useful. However their process works by loading up a different script into the arduino, and currently mine is loaded with standard firmata in order to use the Maxuino object. I also no nothing about arduino programming at this point... :) I assume that the Maxuino object can read the pins that the sensor is connected to, I just don't know how, or what I need to do in order to get it to send that 10 us trigger.
    • Jan 11 2018 | 10:28 am
      Honestly, using maxuino is a mistake in first place. If You post exactly what all You need from Arduino in Your project, I will help You with it. But - forget maxuino.
    • Jan 11 2018 | 8:36 pm
      Can you elaborate more on why using Maxuino is a mistake in the first place? Any help with Arduino would be greatly appreciated.
      It's been a journey to get this far into actually getting to the motors to work, so Maxuino seemed to make things easier from a basic level. But Arduino is the part that I have no idea about, so I was contemplating learning that over the next 12 months, but I need to finish this project first. I have a tank base with two motors that I need to control, a devastator tank ( ). I also need an ultrasonic sensor so that I can stop it from crashing into things, as it will run autonomously. So all I need the arduino to interface with are the two motors, and the ultrasonic sensor. As long as I can send information to the motors, and read the information from the sensor, and get those into Max, the rest should be a lot easier for me!
    • Jan 12 2018 | 8:14 am
      The reason I dislike maxuino approach is that it does keep You from learning Arduino basics. And that becomes a problem as soon as one needs something that is not covered with maxuino. Sending pulse of that short period over serial is not reliably possible, because it is serial communication. You could send a message to arduino to do it for You, but than again - You need to program it in the arduino. There is a good example on dfrobot that can be used on arduino:
      Look at sample code, it has all it needs to control it from Max. Which Arduino board do You use ? One should check if PWM is supported on pins which are selected in sample code. Basicly each motor needs direction switch, which is simple Low - High, and 0 -255 PWM for speed. But please post here exact and detailed infos of the project. And I don't understand why You need to control it from Max if it is going to run autonomously ? So again detailed info on the project, who should control the motors, do You need scalable speed, or just fixed speed etc etc I will be glad to help You.
    • Jan 12 2018 | 12:35 pm
      and You probably need 2 ultrasonic sensors, one at front and another one at back. So to simplify things - one has to control 2 motors with direction and speed, and read distance in front and back to either stop the roboter or send it to oposite direction if it gets closer than xx cm to whatever objects. All that needs just 7 pins, 4 for motor control, and 3 for 2 Sensors, You can trigger both sensors from same pin and read 2 echos back. I can write arduino code which triggers the sensors, reports the distance in cm for front & back via serial, and listens for motor control coming from Max. But to do so, I need to know how do You want to drive that Tank. Simple forward, backward, left, right, stop with predefined speed, or all controlable in real time with something like joystick...
    • Jan 13 2018 | 10:16 am
      @Source Audio: Thanks for clarifying, I felt that would be the issue with Maxuino, in that it works at a basic level but that's it. Given some other projects I'm working on, I've just ordered a copy of Arduino for Dummies (based on my research it has max / msp examples in it), and will commit to learning that over the next month or so. In the meantime, some help with Arduino Code would be amazing. I've done some programming, so the code that you mention does make sense, I just lack the vocabulary to instigate anything more complicated. To answer your questions, the tank really only has to move forwards, so only one sensor is needed. The plan is for it to move in a single direction, until it reaches an object, then stop. It would then rotate a random amount of degrees (one motor running forward, one backward), and then repeat. In terms of why in regards to max/msp, the tank has a mounted speaker, and I'm using max/msp to run the audio side of things. Movement and sound will be synchronous with other parts of the project, so I really need it to run max/msp so that I can develop it further. So all the arduino needs to do is control those two motors, and one sensor. As for the speed of the tank itself I'm not sure, but I assume I can vary this from max? If you could help me in coding the arduino (I'm using an arduino uno), and a basic max patch that shows how to send the information there, that would be amazing. It would also help me learn a lot too!
    • Jan 13 2018 | 1:32 pm
      Ok, that's some more infos, but not as detailed as I wished. The thing is - depending on what all Arduino has to do, or in what extent it would have to comunicate with max, I would do it one or another way. But let's get going. I will make first sketch that would allow You to control 2 motors from Max, having per motor one direction toggle and speed 0 -255. That will be sent to Arduino as String of 4 Values with prepended "M" to make sure that only that values get routed to motor control. And 1 button to stop the Tank. That would send "S" to Arduino. Arduino Pins for Motors: MOTOR 1 : D2 Direction, D3 PWM Speed MOTOR 2 : D4 Direction , D5 PWM Speed --------------------------------- The Ultrasonic Sensor has to be adjusted to work properly. The main thing is to define range of operation, like maximum 50 cm. That helps to avoid waiting for ecoes if no object is in range. Depending on speed of Tank, and if You don't want it to really bump into any object,it could be 5 - 10 cm. You will have to adjust that. Sensor will send "CLOSE" to max when Tank gets closer than set distance to any object. Than You can trigger Stop and Turn ->Continue Values to motors. Doing randomising for turning will be easier for You if You do it in Max, right ? --------------------------------
      I would suggest You test Motor Control and Sensor separately, and than both together.
    • Jan 18 2018 | 4:12 am
      @Source audio,
      Wow, thanks for all this. I've open the max patch and it seems to make sense, about to load up the motor test and see how it all goes.
      In terms of the sensor, what distance is it set to at the moment? And which pin do I need to connect that to? Apologies my responses are not as detailed as you require, it's just that I was assuming I'd be able to see all the information coming into Max itself, and then I could make my patch more accurate and test things as I go. For example, I assumed the sensor would send distance information into max itself, and then I could test distance in accordance with the acceleration of the tank itself, and adjust accordingly.
      Also, 'Arduino for dummies' book arrived today :)
    • Jan 18 2018 | 4:30 am
      So, have uploaded the MOTOR_TEST.ino first (I had a read through the program, and it made sense in terms of logic, still recognising the language though). Have plugged it all into the corresponding pins. I opened the max patch, turned on the metro, and then turn on the the motor control section but clicking the checkbox, and changing the number, but nothing happened. Tried the sliders but nothing happened either. Is there something else I need to set in max for it to recognise the arduino uno interface?
    • Jan 18 2018 | 9:07 am
      I would troubleshoot the motor test in 2 phases. 1 without max - just open Arduino IDE and open serial monitor, set it to correct baud rate 57600 and write some motor values like M 1 100 1 100 or S to stop it If that works, than quit arduino - that is important thing only 1 software can communicate with the board at the time. Than load max patch and send print message to serial object to see if Your board is on port a, If not change it to match. --------------- Ultrasonic Sensor pins are easy to recognise in the code, but here again : trigPin = D6 echoPin = D7 So You would use pins 2 3 4 5 for motor and 6 7 for the sensor. In the sensor test code max distance is set to 50 cm, calculation is done by doubling time audio travels through air, expressed in microseconds. Test patch just sends measured distance in cm via serial. In motor & Sensor patch it sends "CLOSE" if Sensor is closer than 20 cm to any object. if (distanceFront < 20 and distanceFront > 0){Serial.println("CLOSE");}
    • Jan 18 2018 | 10:12 am
      I finally got sime time to solder few things together and test myself I found few mistakes, and I am sorry if You spent too much time looking for solution, and mistake was on my side. As first one needs to add pinMode statements in setup for motor direction pins, and second mistake is missing end of line in max patch. That is needed so that arduino recognises the motor sequence of 1 letter and 4 numbers. So here are corrected patches:
    • Jan 18 2018 | 10:39 pm
      @Source Audio: Thanks for checking the code, you're very right in the fact that that would have altered trouble shooting. So good news, the motors worked. It didn't work the first time, but I went back through your instructions and found my arduino is on port c, once that was changed it worked fine on the motor test. I then uploaded the sensor test, and connected up all the pins, but that doesn't seem to be working in Max. Although, when looking at the Arduino board, the 'TX' light definitely flashes when my hand reaches 50cm from the sensor, which seems to imply that the issue might be in the max patch? Or the message the arduino is sending to Max? Any ideas?
    • Jan 19 2018 | 9:27 am
      Sensor test patch sends value in cm via serial. Can You read the values in arduino serial monitor? In the Motor and sensor sketch it just sends "CLOSE" when sensor detects object closer than 20 cm. Max patch is just set to bang when CLOSE message received, if You want to read all, just connect message object to fromsymbol here is modified patch:
      There are ways to detect if arduino is present and auto connect serial to right port, or not if arduino is not present. print message to serial object outputs ports available to right outlet.That can be run through few objects to do the trick. If You want I could upload some examples...
    • Jan 21 2018 | 8:37 am
      @Source Audio: Thanks for that patch. Now i can see the numbers moving, however the button doesn't bang when it goes past 20. I can change the object to a 'sel 20', and that bangs happily, but I'm sure there is a reason you went with 'sel CLOSE'? Is there a chance it could miss the number '20' when scanning through numbers, as opposed the 'CLOSE' message that is generated by the Arduino? And any idea as to why its behaving this way?
    • Jan 21 2018 | 8:38 am
      I have no idea how to read the values in arduino serial by the way. Do I need to send a message to it like when doing the motor test?
    • Jan 21 2018 | 9:17 am
      I made this sketches so that You can test the hardware and then modify them to Your needs. The Sensor Test sketch sends all measured values via serial, The Motor and Sensor sketch sends only word CLOSE when sensor detects something closer than 20 cm, and nothing else. The reason : You want to test and calibrate sensor so that motor can be stoped on time. When that is done, You don't want that sensor values every 50 ms sent to serial, You just want to stop and turn the Tank. That's why sel CLOSE. And if You find that 20 cm is too much, just change that to some other value. If You just read the 2 sketches You will see and understand the difference. By the way - You can also read this sketches using TextEdit- it is plain text format, nothing exotic. -------------- Again - only 1 software can talk to Arduino via Serial at a time. If You use Max, You have to at least close Arduino Serial Monitor. If You wan't to upload Arduino Code, You must either quit Max, or disconnect Arduino from Max using "close" message to serial object. If Max is running and Arduino gets disconnected, that would freeze Max and You have to force Quit it. ----------------- Arduino Serial Monitor is under Tools menu. You have to set it to same baud rate as the currently running Arduino Board. That is 57600 in sketches I sent You. Than it can send and receive messages to Arduino Board.
    • Jan 21 2018 | 9:45 am
      @ Source Audio: Ok, thanks for clarifying. I didn't understand the difference between the two Arduino sketches, that the sensor test sketch would not send a 'CLOSE' message, and rather just the values. Now it makes a lot of sense in terms of testing and prototyping, so thankyou very much for offering both. It also helps me understand the language a little more. Yes, I understand that only one program can run at a time. Great, now I understand that part of the Serial Monitor too. Thankyou so much for your time and help with this. Now I can prototype this project, and will starting learning arduino in preparation for my next project (simple project, just need to control solenoids in that one!). Thanks again!!! :D
    • Jan 21 2018 | 10:10 am
      You are welcome, I am glad You found way out of maxuino and into open world of Arduino, it's worth it. If You need any further assistance, just ask, no problem. Here is one good wiki : You can find many infos and examples there.