Showing posts with label Workshops. Show all posts
Showing posts with label Workshops. Show all posts

Saturday, 14 November 2009

Arduino - Homework 1

Many hours above Arduino..but isn't this always the case with programming? especially when we have to deal with a new (for us) technology and the only "debugging-hint" it has to show is Null Reference Exception. At least at the end of the day I had the result I wished for! A great relief, as I lost my faith in the way to be honest. =P

The homework we were assigned to do on Thursday has to do with the Arduino - Processing communication. We firstly have to read the values of 2 different sensors (in the Arduino) and then to pass these values successfully to Processing in which we will display them by changing the size of 2 ellipses.

The solution
Click on the images to enlarge.

Circuit



Arduino



Processing



How? Issue #1

In the program for Arduino we read the values of the potentiometer and the light sensor from the analog input and we send them to the program written in Processing via the serial port. The numbers we want to pass via the serial port are integers and the issue was: How can I send integers from Arduino and receive neither bytes, nor ASCII characters or anything else but integers in the Processing? Keeping the Arduino part simple, we send (print) the integers as they are in the Serial port. The integers are sent one by one (one byte each) in ASCII format. So for example if the integer 1023 is sent from Arduino, according to the ASCII table, Processing would receive the bytes: 49 48 50 51. The way implemented above for reading this steam, is firstly to convert these numbers to the digits they represent in string format (in this example "1", "0", "2", "3"), then to store each string of them to a larger string which keeps the numbers received and finally when the steam stops, to convert the string which stores the whole value to an integer.

How? Issue #2
The second issue was to send / receive / read 2 different values at the same time. For doing that, following Mr. Martin's hint, we take advantage of the fact that Processing receives ASCII characters; in Arduino if we use the print-line function to write at the stream (Serial.println) instead of the print function, at the end of the message 2 ASCII characters will be added: 10 (line feed) and 13 (carriage return). Each time we reach an ASCII character which is equal to 10 we know there isn't any other digit to be added to the number we are currently receiving. The next step is to identify which number is this. Was it the first or the second in the message sent from Arduino? For this we use a boolean variable (isNext). In the case where there are more integers to be seperated we would probably use a short integer in the place of the boolean variable.

Tuesday, 10 November 2009

Processing - Homework 1

Yesterday, I missed a workshop on Processing so I tried to catch up today. I got from Pierre the snippets we ran in the class and tried to understand them. I found the whole library which is used in Processing here and that made things easier.

Then I moved on to do our homework for Thursday which is:

Modify the code below to play the animation forward / backward:
    
    int numFrames=6;
    int frame=0;
    PImage[] images = new PImage[numFrames];
    
    void setup() {
    
    size(600, 600);
    frameRate(10);
    images[0] = loadImage("b1.jpg"); 
    images[1] = loadImage("b2.jpg"); 
    images[2] = loadImage("b3.jpg"); 
    images[3] = loadImage("b4.jpg"); 
    images[4] = loadImage("b5.jpg"); 
    images[5] = loadImage("b6.jpg"); 
    }
    
    void draw() {
    if (mouseY > 20 && mouseY < 300) {
    frameRate(mouseY/10);
    }
    frame++;
    if (frame == numFrames) {
    frame=0;
    }
    image(images[frame], 0, 0);
    }

My changes (the .pde file) along with the pictures it uses, are available below:


Code (click to enlarge):



Firstly I removed the part with the mouse (added it later on again) and slowed the frame rate so I could see if my animation was working when I was trying several ways to do the exercise. For the forward / backward animation we use a boolean variable which declares if the counter (frame index) should be ascending or descending. When ascending if the counter reaches 5 (counting starts at zero) we change the value of the boolean variable from asceding to descending, and vice versa in the case of descending when the frame index reaches 0.



When it comes to random pictures, I cannot image.Google.com something else than Roger Federer =)

Saturday, 7 November 2009

Arduino - Practise

On Thursday we had our first workshop on Arduino. We left the class with an unfinished exercise, in which we had to create a toggle button with a resistor, a LED and a switch. Our homework is firstly to finish this exercise and then create a blinking toggle button. The circuit I used for both exercises is this (click to enlarge):



Toggle Button
The way I implemented the Toggle Button is the following (click to enlarge):



Thinking of the Toggle function, we notice that the state of the LED will change only when the user changes the state of the switch (presses or releases the button) and the new state of the button is the pressed state. From what I found, LOW equals to FALSE and HIGH equals to TRUE. So LOW is the released state of the button, and HIGH is its state when pressed. Implementing the idea stated above, in each "Refresh" (each time loop function is called) we read the current state of the button and compare it to the previous state and if this is true we check if the current state is the pressed state. If so, we change the LED's light from off to on and vice versa.

Blinking Toggle Button
The way I implemented the Blinking Toggle Button is (click to enlarge):



Based on the code of the Toggle Button the only change is that we add the blinking ability, which is implemented into the added if-statement. Each time the button is pressed we don't change the state of the LED but the value of a "flag" variable which declares if the LED should be blinking or not. To implement the blinking of the LED at a constant interval, we store into a variable the times the loop function has been called and in every "Refresh" we check if this number divides exactly (modulo equals zero) the interval, which is set in ticks. If so, it is time to change the state of the LED.

Looking forward to the next workshop! B-)