Introduction to Xcode – Swift

This week we had our first workshop on Xcode. We are going to be learning the newer language of Swift rather than Objective-C as it is a much simpler language and more up-to-date.
We started off by looking at the playground section of Xcode, so we could have a play around with the language to see how much is different to what we previously know.Screen Shot 2015-03-11 at 13.25.54

 

There is a fair amount of new language to learn but it is all very simple and makes logical sense, such as words just being abbreviated like above; ‘var’ is a variable, and ‘str’ is a string. However, if you declare what the variable will be, Swift will automatically detect what type of variable it is, such as below I have taken away ‘str’ and it still knows that it is a string from the type of information that has been inputted. Screen Shot 2015-03-11 at 13.54.44

 

As shown below this works for all different variables, including integers (‘int) and booleans (‘bool’).

Screen Shot 2015-03-11 at 14.05.25

 

To declare a constant you use ‘let’, constants cannot be changed but variables can be. Some other points I had not come across before are ‘?’ and ‘!’.
? = optional
! = removes the optional value

In the workshop we then went on to put the new language into practice by learning how to insert a button and get an outcome from it:

Screen Shot 2015-03-11 at 14.18.38Screen Shot 2015-03-11 at 14.19.01

 

When the button is pressed a label message appears on screen saying ‘Button pressed’, and we also set it to print a line in our compiler to test it is working. We also set it so that when the button is pressed 4 times, the message disappears.

From this first workshop it has made me realise all the possibilities and also how it can be very simple to create something very effective. This has made be very excited for upcoming workshops and to get started on the project. Next workshop we will be putting our knowledge into practice by making a simple random facts app.

User Response

Due to the problems I had when building my design in the public space, I was not able to test my work on many people; only people who had helped me with filming and testing along the way. So from the people who had seen my work, here are some of the responses:

“It’s interesting to see how code inputted on Processing/Arduino can create a 3D installation”

“I liked how it was displayed as you could get close to it and see how the liquid behaved both from the top as well as the sides”

“It was intriguing to see something different from other projects which made it stand out for me and probably would do for anyone else walking by too who would normally just see things on a screen”

“I found it created an enigma at first which drew me into it and made me want to look closer”

“It’s good to see how the magnets worked with the ferrofluid, even though it didn’t completely work it still showed the process of how it could have worked”

“I really liked the effect the magnet had on the liquid; very eye-catching!”

“I liked the simplicity of the design”

I am very pleased with these responses and i’m sure if I had time to display my working design, I would have had even more responses and more variety of answers. I said in my Audience Reception Theory post that I would have my own intended views of how audiences would perceive my design:

  1. 3D installations are bringing design closer to real-life
  2. The ferrofluid is almost life-like
  3. Audience engagement is key to making some designs work
  4. Science can be very interesting when combined with interactive design

I am pleased that a few of these readings have been met – it is clear that the audience knew to look closer as it was a very ‘eye catching’ subject, and that they ‘really liked’ the magnets with the ferrofluid and found it like an ‘enigma’. So at least two of my intended outcomes were met, and if more people would have seen my final design they may have thought about the fluid being life-like or how 3D design is making things more realistic to the audience.

Final Outcome – Ferrofluid with weaker magnets

After having problems with the first magnets I used being too strong, I ordered a pack of 10 weaker magnets to be delivered next day. The good thing about the magnets I ordered is that they are very thin and can be stacked on top of another to create more strength if needed. At first I just tried out the system with just 1 magnet on each end, but this didn’t do a lot in the way of spikes in the fluid, although the good sign was that the motor was still spinning even with the magnets close to the fluid. I then jumped ahead and stacked another two on top of each end, this produced more of a spike when next to the liquid, however it still made the motor jitter and get stuck (first two clips in video below). So I went back down to two magnets on each end, this was the perfect compromise as although the spikes it created weren’t as impressive as when I used the bigger magnets, there were still visible spikes and the motor would also spin which is what I was not achieving before.

(Ferrofluid is very hard to clean so please excuse the dirty perspex box in the video!)

Although the motor is still a little jittery in some places and the sensor sometimes throws out some random data, I am very pleased with how hard I have worked to finally get an outcome close to what I had imagined. The main thing I am pleased that worked is the Arduino circuit with the sensor and motor, as this is what I had spent a lot of time on with coding and wiring. If I hadn’t managed to get the ferrofluid to work today I still would have been happy with using the LED’s as my final outcome, because all I my intention was, was to build an installation using a sensor as the mode of interaction, with some kind of visual display as an output, and this is what I have created.

 

LED’s – Possible Solution

The main idea I wanted to portray through my work, is the idea of the audience creating an interesting visual through the use of a sensor. As I had not created this idea through my first attempt use of ferrofluid I thought hard about what other ways I could create a visual without using such technical equipment where things could go wrong. So I had a thought for a possible solution – using the same frame on the motor but replace the magnets with LED’s, so it would be the same concept as I would have used with the ferrofluid but with light instead of ferrofluid spikes. The closer someone is to the sensor the faster the motor spins, therefore giving different lighting effects. I had to do this at home as I had control over the lights so I could film it in the dark, I do not have the control over the lights in the foyer of Weymouth House so it would not be as effective as if I did it at home. I asked my friend to walk backwards and forwards within the space to see the difference in speeds. I had a worry that the sensor would not pick up information in the dark but it still worked and I got the results I had intended. I attached batteries with wires to the wooden sticks, and then just added in the LED’s to the end of the wires.

 

I am pleased that I managed to come up with a different solution so quickly, and that it is an interesting visual but still using the same Arduino set-up. This meant I did not have to alter any code or any of the wiring which I would have had to if I had changed to a stronger motor.

My next task: I ordered some weaker magnets to try and see if they will work better with the motor and the ferrofluid like before, but without the motor getting stuck.

 

 

Building the installation and testing

Today I build my installation using the resources I could:

  1. I started off my using a rubber battery end protector which fitted the point of the motor perfectly.
  2. I then glued two wooden coffee stirrers together and affixed them to the rubber battery protector.
  3. From this I then trimmed the wood so it would fit in the base of the perspex box I bought.
  4. I then attached the magnets to each end of the wooden stirrers.
  5. I finally attached the whole thing into the point of the motor.

This was the basis for the mechanical part of my installation. The weight of the magnets made the base of the motor struggle a little, but I knew all I had to do was secure it in place for it to work properly again.

I then had to construct the physical form of my installation, I already had the perspex box but I needed something to raise it off the table to be able to put the motor under, so I asked around at uni and found a large block of polystyrene which i cut up into even blocks and put under each corner.

IMG_0064

(A little make-shift I know, but it did the job!)

The video below shows the process after this:

The first clip shows everything working fine, I used another section of polystyrene which already had a hole in it the perfect size for the motor to be held in. I taped this to the table so it would not move and jitter, meaning the only thing to move would be head of the motor. So everything is working, until I put the ferrofluid into the container. The attraction between the magnets and the fluid was too strong for the motor to continue turning. The motor struggled to turn but every now and then it would complete a full rotation.
I tried adding on an extra bit of cardboard on top of the magnets so there was a greater distance between the magnets and the fluid, this helped a little bit and managed to see a few rotations, but I was still not getting the outcome I was expecting.

I then decided to change the set up, and moved to a different table. This time I put the motor and the magnets underneath the table and the box with the fluid on the top. The sensor, Arduino and battery rest on the lower table, while the motor with the magnets is closely underneath the table, resting on the polystyrene. I had a slightly better outcome from this as there was more occasional rotations, but I still did not have the outcome I was wanting.

DSC_1440

 

The video shows how the motor and fluid was moving when no one was interacting with it – it would jitter and slowly move, then sometimes rotate. It then shows me interacting with it up close – when the motor is supposed to be turning fast, it gets stuck, then when I move out the way (at 1:30) it continues its rotation. This shows that when it is meant to be moving slower, it has the time to move slowly, but when it is meant to be turning fast, it gets more stuck and doesn’t move.

The only two possibly solutions I can think to fix this problem are:

  1. A bigger, more powerful motor
  2. Weaker magnets

So within the time-frame, I am not going to be able to get a bigger motor as this would require a change in the coding and they are also very expensive. So I am going to order some weaker magnets and try again to see if it will make a difference.

A Working Prototype

After a very long time experimenting with different ‘if’ statements and trying to figure out the best solution to the problem of the sensor and motor being very slow and jittery, I finally came to an outcome decision which I am happy with. I started off by creating different ‘if’ statements for a range of distances, this was still giving out very random results and crashing quite often. I then tried an ‘if else’ statement, to declare that if nothing is detected in the first range, then check the second range, and if nothing is detected in the second range then it must be in the third range. This still did not give out reliable results. So I went back to doing normal ‘if’ statements and tweaked my code, after many different attempts I found a code that worked; In my loop, I created three ‘if’ statements, for a different output speeds for the motor, depending on how close or far away someone is, in a range of 0-400cm.

Screen Shot 2015-01-23 at 12.19.13

The first statement has an output speed of 25 when someone is within 200-400cm away. The second statement has an output speed of 150 when someone is within 100-200cm away. And when someone is as close as 100cm or less, the output speed is 200. The video below is an example of the two extremes, firstly when my hand is close, the motor spins fast, and when I move my hand away it jumps to the slowest speed as the background was between 200-400cm away.

The next video shows the three stages of speeds – starting with the fastest close up, then the middle speed and finally the slowest speed. It is a bit unreliable as the ping sensor sometimes throws out some random information, as I can see in the serial monitor, but overall it works and gives the effect I was hoping for – that if someone is closer it spins faster and if someone is far away or no one is there it spins very slowly.

This is my working prototype, so my next stage is to construct my installation and test it in the public space.

Ping and Stepper – Problems

After I managed to get the stepper motor to work, I combined the two sets of codes from the Ping Sensor and the stepper motor, so I would be a step closer to a working final outcome. However, I encountered a problem when I combined them and ran the sketch – the motor would run much slower and more jittery than when it was run separately, and the Ping sensor was also very slow and would only flash once every second, meaning the input data it was receiving was very varied and unreliable.

I tried to overcome the problem by adding an extra power source of a 9V battery to the circuit but the problem still persisted, so from this I concluded it must be something to do with the code rather than power/circuit.

In my code ‘myStepper.step(STEPS);’ seemed to what was causing the problem as it was going through the loop and and not going to the next element before completing this that step, so my next task is to replace that line of code with an input code from the sensor. This input from the sensor will trigger the motor to run, ie when someone engages with the installation from 2.5 meters away, the motor will start to turn slowly. If they come closer it will turn faster with a threshold of 80 for example. (See my previous post about speed of steps.) So next challenge = sensor input code to trigger motor.

Arduino Motor shield – Motor Finally turning!

The Arduino motor shield arrives, in size it is the same as the Arduino uno and just sits on top of the Uno:

IMG_0043

This is what the motor shield looks like when on top of the Arduino Uno. A motor shield is good as it allows you to control the speed and direction of the motor easily. It is even better for me as it will allow me to power a motor with a separate power supply of up to 12v, which is perfect as my stepper motor is 12V.

I put the motor shield on top of the Arduino Uno and wired up the elements I needed. Below is an image of how I wired up my stepper motor to the Arduino.

IMG_0041 2

I found an example code online to make the motor turn clockwise for a rotation, then anticlockwise:

 

So I used this code but then made my own adjustments to it to have a longer and faster rotation. I added the black wire onto the motor so it made it easier to see it turning in the videos.

 

So as you can see from the code in this video, I changed the steps per revolution from 48 to 200, this made the revolution last longer. I also changed the stepper speed from 30 to 50, making it turn faster. This is all part of my experimentation to see how fast I want the fastest speed to be of my final outcome. My next challenge is to add in the Ping Sensor and merge the codes of the two to get them both working off the same board.

Challenges along the way

Since starting to put together the electrical elements and the code for the Arduino, I have had numerous challenges that have held me back. Starting off with the Stepper motor:

I needed to use a stepper motor as I needed a 360 degree continuous rotation, rather than a servo motor which is not a continuous rotation. I also needed a stepper motor powerful enough to pull magnets with, so I went for the bi-polar 12V motor. The problem with this is that the Arduino can only handle 5V, so all my attempts to make the motor work through the Arduino were unsuccessful as there wasn’t enough power. So I tried adding more power using a 9V battery. I went back to basics and used an LED to test that the circuit I was using with the battery was working:

(Stupidly didn’t get the battery in shot but it was attached to the yellow and orange wires to the right of the shot.)

I then tried switching in the stepper motor instead of the LED, but I could not get the wiring right to power it, and it had not been documented anywhere online to help me. After a lot of research it was clear that most stepper motor examples used some sort of motor shield or driver. So I have ordered in a shield and a driver to try both of them out. I would say that one of my main difficulties has been trying to find help online, as I am a first-time user for Arduino I was hoping to find tutorials and simple diagrams for wiring, however for the electronics I was hoping to use there were not that many. I understand a lot of the code from my knowledge of the Processing language, but it is the wiring which I needed to understand. Because there are many different types of stepper motors, it is hard to find help specific to each motor. However I am hoping that once I get to use the motor shield, it will be basic to understand and follow instructions to get the motor spinning. I will then be able to progress with my idea and incorporate the input from the sensor.

 

Ferrofluid and magnets arrived – it works!

For my final outcome I ordered some Ferrofluid and 2, small, strong Neodymium magnets. It came with a petri dish and pipette. This is a quick post showing it working.

 

I put the petri dish on top of my glass desk and used the magnet from underneath the glass. The thickness of the glass meant the magnet didn’t get too close to the fluid and made the perfect spike form. If the magnet gets too close to the fluid it does not create the spikes as they have such a strong attraction. I will need to bear this in mind for when I put my final outcome together as there will need to be a small distance between the magnet and the liquid for the spikes to be able to form.

My next step is to get the stepper motor I ordered working, and then I will be able to link the input of the Ping sensor to create the speed change in the motor.