Gameduino Snake – Final Project

My final project using the SparkFun Redboard for my physical computing class. I call it the Gameduino Snake since it is basically a small little hand held device which allows the user to play a game of snake on an LED matrix.

IMG_0277

The game uses a simple bi-color 8×8 LED matrix as a display for the game itself and for the score after the game is over. It uses a triple-axis accelerometer which is attached to the breadboard right next to the LED matrix. The accelerometer is used to detect the movement of the whole game board. Basically, the direction you tilt the device will move the snake in the same direction as it is being tilted. For example, if you tilt it forward the snake will go up and so on. It also has an Ethernet shield on the Arduino so that the player can Tweet their score after they finish their game. Finally, the Gameduino also has three separate buttons on it as well. The blue button acts as a power button and when pushed will place the snake and target on the LED matrix and start up a game. The green button acts as a reset button which is mainly to be used after the player receives a game over to reset the game and go back to the blank start screen. Finally, the red button will send a Tweet of the player’s score, which was stored in the RedBoard’s EEPROM memory after game over, and, since Twitter does not like duplicate Tweets and it is very possible to get the same score more than once, it also sends the number of seconds since the last reset. The Tweet button does have to be pushed at the main screen at the beginning, in other words it has to be reset before hand.


(the game is easier to play than the video makes it seem, it was difficult to play the game with one hand and film with the other at the same time. Also my apologizes about the difficulty in reading the computer screen, it says “Snake Score” then the score I had, 3, and then “Time Since the Last Reset” and that time, 14 s.)

Once I started thinking about my final project at the start of the term I knew I wanted to do something involving a game. After doing some research on the internet I found someone that had recreated Super Mario Bros. on an LED martix and Arduino. After seeing this I knew I wanted to use the LED matrix to make some sort of game to play on the Arduino, however, due to my beginner coding skills I knew I would have to make the game simpler than the that. After thinking for a while I decided to make the game Snake, figuring that would be a simpler game to code and make. I did think then that it would be a very neat idea to use the Ethernet shield so the player would be able to Tweet there score after their game was over. I also thought it would be cool to add the GPS module into the circuit so that the time and date would be included in the Tweet as well, however, after I did my first intermediate project I saw just how much memory the GPS uses up and did not think I would have room for it with my final project so I left that feature out. Also, I had originally planned to control the snake using four push buttons, but my teacher suggested that I use the accelerometer to make the project a little more interesting and physical. I ended up using this and it turned out to be a fun and different way to play the game. I also had planned to incorporate a Piezo buzzer to make it so that the game plays a little tune during the game, but unfortunately I could not find a way to do this properly. I only know how to code music with the Piezo using delays and if I used the buzzer during play it then would make the game unplayable. If I used it for the game over display it would play the tune, then show the display, then play the tune, then the display and so on, not ever show them at the same time. I may have been able to solve this eventually, but due to time constraints I decided it would be better to just not worry about the device as it did not add much to the overall experience.

IMG_0279

I ran into several problems while creating this project. One of the first major issues was simply getting the snake to move correctly. At first I had it so that the RedBoard did not care about the specific x and y values read by the accelerometer and rather only cared about the orientation of the accelerometer. After I adapted a code I found online, this worked fine and would play the game just fine. However, this also meant that the player had to tilt the board an extreme amount in order to get the snake to move. This worked, but was not the best set up for the game to work properly. Seeing this I decided to test out the values and set up the code so that it would read the position and if the x and y values were greater than or less than a certain value it would move the snake appropriately. Again this worked and for the most part the game ran fine, especially after I got some help from my instructor and was able to make the snake always moving. However, yet again I ran into one small issue that hindered the game’s fun and play ability. Basically, because of the way I set up the code if the device was tilted at a diagonal angle, especially the lower right, it would cause the snake to move diagonally. This obviously was a breaking glitch in the game. After talking with my professor we were able to figure out how to fix this. To put it simply I found the values when the accelerometer is flat and set that as the basic position and then found a threshold around that point. If appropriate values are greater than that threshold then the snake moves in the direction being tilted.

IMG_0280

I ran into a few other issues as well. I originally had planned to have the blue button act as both the power button and the reset button, however, after running into some problems with trying to code that and seeing that I had room for another push button I decided to simply hook up another button which would do the resetting for me. The Tweet function also ran into some problems, since I originally had it set up to be able to send in the game over screen, but it would only work at one specific moment. I fixed this problem by having the Redboard store the score in the EEPROM memory and then have it be able to send at the start screen. It also took me a while to decide what I wanted to send in addition to the score in order to avoid the duplicate message issue. I tried some other set ups, but eventually got it to include the number of seconds since the last reset. Finally, one problem I ran into was that the game sometimes seems to crash when I use a 9volt battery. It would play the game fine, but would freeze up while in showing the game over screen. I tested it again while writing this and it seemed to work fine, however, to avoid this possible glitch I prefer to hook it up to the computer for power. This may be an issue with the battery being low on power or simply not having enough power to run all of the game features. Also I found that when using the battery that because of its weight it can be difficult to play the game. Having the battery hang of the side seems to make it so that you have to tilt with more force to get the accelerometer and snake to recognize the movement. This could probably be corrected by adjusting the thresholds in the code, or possible by adhering the battery to the bottom of the board, but it is difficult to say for sure. I am still unsure why is seemed to crash so often during tests before.

IMG_0278

If you would like to build your own version of the Gameduino then see below for sketch, schematic, parts, and code.

Parts:
(1) SparkFun Redboard/ Arduino Uno
(1) MMA8452Q Triple-Axis Accelerometer
(1) 8×8 Bi-Color LED Matrix with I2C backpack (can be one color if so desire and can be done with out the I2C backpack, but will have to use different code)
(3) Push Buttons
(2) 10K Ohm Resistors
(2) 330 Ohm Resistors
(1) Arduino Ethernet Shield
Several Wires
Optional:
9 volt battery
Battery holder

The schematic and sketch can be viewed below:

Final Project Schematic

Final Project Sketch_bb

For the power button and Tweet button you want to run 5 volts into one side of each and from the other side you want to run a 10K resistor to ground so when the button is low it will complete the current and carry the voltage to ground and then also from that side you want to run a wire to whatever digital pins you want to use, I used 7 for my power button and 6 for my Tweet button. Then for the reset button we are using the reset pin so all we have to do is run a wire from the reset pin on the Redboard to one end of the button and run the other end directly into ground. This works because the reset pin will reset the Redboard or Arduino when it receives a LOW reading, and we can accomplish this by connecting it to ground. When the button is not pushed the reset pin reads a normal reading and then when it is pushed it will receive a reading from ground, or 0 volts, or LOW and thus reset the game with out having to push the reset button on the Redboard. The LED matrix has an Adafruit I2C backpack on it so it is very simple to connect. You want to run 5 volts into the pin with a plus sign (+) on the matrix so that it receives power. Then run the minus sign (-) pin to ground so the current has a place to run to. Since the matrix backpack uses I2C communication we then have to connect the SDA and SCL pins on the backpack to the appropriate pins on the Redboard. We can either use the pins labeled SDA and SCL on the Redboard or the A4 and A5 pins work for this function as well. If you wish to use the analog pins as I did then A4 works as SDA and A5 works as SCL. Then finally to hook up the triple-axis accelerometer you need to run 3.3 volts into the pin labeled 3.3 on the accelerometer and then run a wire from the ground pin on the accelerometer to ground. This will allow the current to run in an power the device and then to run to ground to complete the circuit. Then since the accelerometer is also an I2C device you need to run the SDA pin on the accelerometer through a 330 Ohm resistor to SDA or A4 on the Redboard and the SCL pin on the accelerometer through a 330 Ohm resistor to the SCL pin or A5 on the Redboard.

The code I used can be found here:

Final_Project Gameduino Code

The comments of the code explain how most of it works, but basically the Redboard will set up its values and then if it reads a push of the blue button it will go through and draw both the target and snake from the information it received in the set up function. Then it will monitor to make sure the snake is in the playing area and that it has not hit itself, if both of these statements are true then it will call a function to detect the position of the accelerometer and move the snake accordingly, if either one is false it will call game over.

void moveSnake() { // this function will move the snake according to the accelerometer
  if (accel.available()) {
    accel.read(); //this will read the values of the accelerometer
    Serial.print("x: ");
    Serial.print(accel.cx, 3);
    Serial.print("\t");
    Serial.print("y: ");
    Serial.print(accel.cy, 3);
    Serial.print("\t");
    Serial.print("z: ");
    Serial.print(accel.cz, 3);
    Serial.print("\t");
    Serial.println(prevDirection);
    float restingX = 0; //the accelerometer at rest
    float restingY = 0;
    float threshold = 0.350; //how much it should be tilted to move the snake.


    Serial.println(accel.cy - restingY);
    Serial.println(accel.cx - restingX);
    Serial.println(abs(accel.cx - restingX));


    if (((accel.cy - restingY) > threshold) && (abs(accel.cx - restingX) < threshold)) { //if the accelerometer is tilted left
      Serial.println("Left");
      snakeY[0] = snakeY[0] + 1; // the snake will move to the left.
      prevDirection = 1;
    }

    if (((accel.cy - restingY) < -threshold) && (abs(accel.cx - restingX) < threshold)) { //if the accelerometer is tilted right
      Serial.println("Right");
      snakeY[0] = snakeY[0] - 1; // snake will move to the right
      prevDirection = 2;
    }

    if (((accel.cx - restingX) > threshold) && (abs(accel.cy - restingY) < threshold)) { //if the accelerometer is tilted up
      Serial.println("Up");
      snakeX[0] = snakeX[0] - 1; // the snake will move up
      prevDirection = 3;
    }

    if (((accel.cx - restingX) < -threshold) && (abs(accel.cy - restingY) < threshold)) { //if the accelerometer is tilted down
      Serial.println("Down");
      snakeX[0] = snakeX[0] + 1; // it will move the snake down
      prevDirection = 4;
    }

    if ((-threshold < accel.cx) && (accel.cx < threshold) && (-threshold < accel.cy) && (accel.cy < threshold)) {
      if (prevDirection == 1) {
        snakeY[0] = snakeY[0] + 1;
      }
      if (prevDirection == 2) {
        snakeY[0] = snakeY[0] - 1;
      }
      if (prevDirection == 3) {
        snakeX[0] = snakeX[0] - 1;
      }
      if (prevDirection == 4) {
        snakeX[0] = snakeX[0] + 1;
      }
    }
  }
}

This function begins by setting up the values that our x and y should be while the accelerometer, and board, are held flat. Next, we define a threshold for detecting tilting and thus movement. For example, if we want to move left we need our y value, which acts like a horizontal axis, minus our resting value to be larger than whatever we set our threshold as. Also since we want to avoid the moving diagonal glitch that I encountered in my second attempt at coding movement we have to make sure that the absolute value of the current x reading minus the resting x is less than our threshold. In other words we need to make sure our x value is between the threshold and the negative threshold meaning we cannot read two directions at once. In this case since we set our resting values as zero we probably could just use the accel.cx and accel.cy values instead of subtracting the resting value from them, however this will make it so that if we ever want to change the neutral position we can do so easily. The other three directions work in similar ways. For each direction we will also save the direction it was going in to a variable which, in order to make sure the game has enough challenge, will be activated when the accelerometer returns to a flat state and thus the snake will continue to move in whatever the last direction it had been moving in until it is tilted again.

Also while the snake is in the playing field, is moving, and has not hit itself, the game will be monitoring the position of the snake and target to see if the player scores.

    if ((snakeX[0] == targetX) && (snakeY[0] == targetY)) { //this will read if the snake is on the target
      snakeLength++; //if it is it will gain a length of one
      score++; // and the score will increase by one
      Serial.print("Score: "); // the score will then be written on the serial monitor
      Serial.println(score);
      if (snakeLength < maxSnake) { // if the snake is less than the maxium length
        makeTarget(); //then we will make a new target to go after
      }
      else {
        targetX = targetY = -1; //if it is not less than the max, then we will not
        //make any more targets
      }
    }

This checks if the snake’s head, represented by snakeX[0] and snakeY[0], is the same as the target’s coordinates. If this is false the target stays where it was and the snake stays the same length. If it is true then the snake will grow by one pixel and the score will go up by one point. The code will check how long the snake is then, if it is less than the maximum length of the snake then it will call a function and make another target for the snake to go after. If it is not less than the maximum length it will stop making targets. In my code I set the maximum length of the snake as 64, which is the entirety of the LED matrix. I did this because I only wanted the game to end if the snake died and not if it got to a certain length, if you want to alter this all you have to do is change the variable maxSnake in the code.

I am happy with how the Gameduino ended up turning out, however, there are still several improvements and modifications that could be made to it. For one, like I said it seems to behave different when hooked up with a battery. I am not sure if this is in the code, hardware, or is simply an issue of the battery adding weight to the set up. One other major improvement would be to include a wifi shield to the set up so that when one wanted to Tweet they did not have to plug in an Ethernet cable and rather can just use the wifi. This would be even more helpful if the battery could be working correctly. The device could also be expanded to have other games coded for it, like Breakout or as I said earlier Super Mario Bros. In addition if one were to use an Arduino with a larger memory it is possible that we could make the device have more than just one game at a time. Also a larger Arduino would allow for the use of the GPS module like I initially planned. A couple other improvements would be to have the Tweet include the length of the game time and not just the the time since the last reset. Also if the Piezo buzzer could be coded without delays it would be a neat little device to add to the whole set up.

Thank you for reading!

Final Project Update

After working during the weekend, I have the circuit rigged up and everything seems to be connected correctly and working properly. After looking at and adapting some sample codes, which I have cited in my code, I have gotten a nice code working which functions pretty well for the game. I have gotten it so that pushing the button will start up the game with the snake in one set spot and the target in another randomly chosen spot. Then I have it set up so that when the unit is tilted the three-axis accelerometer will detect which way and move the snake appropriately. For example, when it is tilted forward the snake moves up and when tilted right it moves the snake right, etc. Then when the snake, which is indicated by green pixels, goes over a target, red pixels, the snake will gain one more pixel in length and another random target will be set up. Also, I have it set that with each target picked up one will be added to the players score. Then right now I have it set up so that if the snake goes off of the LED screen, a game over screen, which is basically a yellow/orange spiral starting in the middle and going to the edge, will play and then the player’s score will be displayed. Now, it is set up so that if the red button is pushed it will send the score of the game to Twitter.

I still have several features to work out and include in the project. For one, at the end when I have the score displayed I would like it so that if the power button is pushed it will restart the game, that way the reset button of the Arduino does not have to be pushed. I also need a few additional features to be added to the game, for example I would like for it to give a game over when the snake touches any part of itself, like the head hitting the tail. Also since the set up of Twitter does not like duplicate messages, I included the millisecond timer to add a more random variable which would help reduce the chance of duplicates. However, right now that timer gives values that are basically meaningless, partly because the millis function will loop around if it reaches the maximum int value, which happens rather quick. I also think it would be a good idea to have the snake move by itself, like if you tilt forward and the snake moves up, when you move back to flat the snake will continue to move up instead of just stopping into one pixel until it is moved again. These are the main issues and improvements I see that should be made at the moment. Also if memory allows I would like to incorporate the Piezo buzzer and have the device play a little song while the game is going on.

Accelerometer Cart- Intermediate Project 2

The Accelerometer cart is the second intermediate project I made up. Simply it is a small little robot that receives inputs from a triple-axis acceleroometer and moves in an appropriate direction accordingly.

IMG_0254

The Accelerometer Cart uses a triple-axis accelerometer to input the direction the device is being tilted. This information is then sent to a SparkFun Redboard which then outputs it to two servo motors which will spin wheels in the appropriate direction and thus move the little robot chassis. The robot chassis was from a Parallax Inc Board of Education Shield that my instructor provided for me. You can find out more about this at this link. Basically it is metal body which has batteries and the servos tucked underneath of it. It then has a wheel attached to each servo and a rolling ball on the front to help give it more support. It then has a shield on top that the Redboard or Arduino can easily fit into. The shield also includes a little bread board to hook up items on the top. It also uses a shift register and LEDs so that when the accelerometer is tilted back towards the person holding it the cart moves backwards and the LEDs light up and that when the accelerometer is tilted to either side the LEDs on that side blink as the cart begins to turn in that direction.

This was the second project that I had to think of what I wanted to do by myself. I did not know what I wanted to do at first, but I liked the idea of using the triple-axis accelerometer to control some sort of motor. I did not know what kind of device to use with this at first, but eventually after thinking I had the idea to use it with some DC motors that had wheels attached to create a little device to drive around depending on how the acelerometer was tilted. I talked with my instructor about this idea and he approved, but we did not know at first how to find wheels that would work with the DC motors without permanently gluing them to the motors. My instructor then remembered that the school had the robot chassis around and after we made sure that no one else in the class wanted to use it he told me I could use it. With that I decided to incorporate the shift register and LEDs into the device to give it a little more. I did not know how to do this at first, but then I decided to use them as a sort of tail lights. With that I had the final design.

IMG_0252

I ran into a few difficulties while making the project. One was getting the wires to be long enough to put distance between the accelerometer and the chassis and still have it work appropriately. This also believes to what I believe to be the biggest issue with the device. Occasionally I believe one of the wires connected to the accelerometer or the chassis comes unconnected and whatever wheel is spinning at the time gets stuck spinning. A simple reset will usually solve the issue right away though and if not then making sure the wires connecting the accelerometer will more than likely fix the issue. This could be a bug in the code, but as I said I believe it is due to wires coming loose. It also took me a little time to get the LEDs and shift register to work properly, and the slight delay I put in the blinking with the “turn signal” can sometimes be a hindrance, but this is rare and far between.

IMG_0253

If you would like to build your own Accelerometer Cart then keep reading to find parts, schematics, sketches and code below.

Parts:
(1) Arduino Uno/ SparkFun Redboard
(4) Red LEDs (could be any color you want)
(1) 74HC595 shift register
(6) 330 Ohm resistors
(1) MMA8452Q Triple-Axis Accelerometer
(1) Parallax Inc BOE Robot (If you do not have or do not want to build this you could use servo motors and attach wheels, just make sure they can spin all the way around and that you can properly support it)
Several wires, long ones will come in handy, and almost be necessary.

The basic schematic and sketch can be seen below:

Intermediate Project 2 schematic_schem

Intermediate Project 2 sketch

(NOTE: the software I use to design the sketches and schematics, Fritzing, does not have any parts for the Parallax shield that I could find and I do not have the knowledge to design them myself. So I simply set up the sketches and schematics as if they were being hooked up to the servos directly. I put the little breadboard in the sketch to represent the breadboard on the top of the Parallax board and the larger breadboard is where the triple-axis accelerometer is hooked up.)

You want to hook up your triple-axis accelerometer with power running from the 3.3v pin on the Redboard to the 3.3v pin on the accelerometer and run the ground pin of the accelerometer to the ground on the Redboard. Then from the SCL pin on the accelerometer use a resistor and run it to A4 and SDA to A5. A4 and A5 act as substitutes to the SCL and SDA pins which are not present on the Parallax board. On the BOE Bot there are nice little pins in the upper right corner that allow for the easy plugging in of the servos. I plugged the right one into 10 and left into 11. Then to run the shift register you need to run 5 volts into the VCC pin and the SRCLR pin and then run the Ground pin and the OE pin to ground. Then run the SER pin on the shift register to pin 2 on the Redboard, the RCLK pin to pin 4, and the SRCLK pin to pin 3. Then run the Qb, Qc, Qd, and Qe pins through resistors into the LEDs (in that order you would do Qb = far right, Qc = mid right, Qd = mid left, Qe = far left) then into ground. This allows the shift register to only use three Redboard pins to control the four LEDs.

The code is listed below, feel free to use it and edit it as seen fit.

Intermediate_Project_2

The code is pretty straight forward and the comments should explain any confusing parts. The code basically works by monitoring the state of the accelerometer and then calls the function to print this to the serial monitor and depending on the orientation it moves the wheels and lights up the LEDs. The excerpt below shows the case for when the accelerometer is tilted forward. This first turns off any lights, in case we were moving backward anytime before this. Then it prints the orientation to the serial monitor and then it attaches the two servos to the appropriate pins. It then spins both of the wheels to move the chassis forward. The left wheel moves counterclockwise, which is why the value assigned to it is 170, since with the servo the closer to 180 it is the faster it spins. The right wheel moves clockwise, which is why it has an assigned value of 10, since the closer to 0 it is the faster it will spin in the clockwise direction. The code for each direction is similar to this, expect it plays with the values of how the wheels spin.

    case LANDSCAPE_R:

      for (index = 1; index <= 4; index++) {
        shiftWrite(index, LOW);
      }
      //this will turn off the LEDs as we move the
      //accelerometer back to flat

      Serial.print("Landscape Right");

      servoRight.attach(10); //this will attach our right servo motor and wheel
      // to pin 10

      servoLeft.attach(11); // this will attach our left servo motor and wheel to pin 11

      servoRight.write(10); //this will make it so that when the accelerometer
      // is tilted forward it will move the right wheel clockwise

      servoLeft.write(170); //this will make it so that when the accelerometer
      // is tilted forward it will move the left wheel counterclockwise so it
      // will move the same as the right and the device will move forward

      break;

As I mentioned before the device does sometimes get stuck moving in one direction, most notably when turning. I believe this is an issue of the wires coming loose, so if someone wanted to make it more stable soldering the wires to the accelerometer or at least into a better holder it would probably improve the devices stability. An even better option would to use inferred so the device could be made wireless and again improve the stability issue. The speeds could also be played with to make it a little less touchy, as well.

Thank you for reading! 🙂

Rock, Paper, Scissors, Playing Glove

Rock, Paper, Scissors Glove1

If you ever have wanted to play a game of rock, paper, scissors against yourself then now you can. The device is a glove that one wears and then all they have to do is simply play a game of rock, paper, scissors, and the computer will begin playing against them too. The glove monitors how you play and learns from it and then adapts its strategy accordingly.

To learn more about it click the link here!

Tweet-O-Temp – Intermediate Project 1

The Tweet-O-Temp is the first intermediate project I designed. The name basically says all the device does, using a SparkFun Redboard it is possible to read the current temperature and to send it to Twitter at the push of a button.

CS111 Intermediate Project 1- 2

The Tweet-O-Temp is made up of a temperature sensor that is constantly reading the temperature of wherever it is. It does this by taking the voltage running through it and converting it to degrees Celsius and then, since I live in America and we like a different system, it converts the temperature from Celsius to Fahrenheit. The device also has an Adafruit GPS breakout module which constantly is reading the position, time, and date of the device and sending it to the Arduino. The device usually records time in GMT (Greenwich Mean Time), so it had to be converted from this to Chicago Central Time. It would also have to be adjusted manually for daylight savings time too, which at the time of this post is not in effect. Finally it use an Arduino Ethernet shield and an Ethernet cable to connect to the Internet and at the push of a push button it will send the information from the GPS and temperature sensor to Twitter. The device is also set up so that it automatically sends a Tweet at a certain time of the day with the time, date, location, and current temperature.

This was the first assigned project that had no set qualifications, other than simply to experiment with different pieces of equipment and ideas to gain a wider understanding of physical computing. It took me a while to decide what I wanted to do for it. I knew I really wanted to try and use the Ethernet shield so that I could play around with my Redboard sending a message to Twitter. I first heard about this idea when hearing about the device that would Tweet when plants they were hooked up to were low on water. I really liked this idea of sending Tweets from an Arduino so I began to think of device to utilize this ability. Eventually after looking over the parts I had available I thought of the idea of using the temperature sensor and the GPS and using it as my current device is set up.

CS111 Intermediate Project 1- 3

I did run into a lot of problems along the way of designing this project. Originally I had intended for the project to use the Arduino’s built in EEPROM memory to have it so that the system would save the temperature at a given time and then would update it every two minutes. I would then have it that at the push of a different button the circuit would send the stored value and a different message to Twitter. Unfortunately due to the limited memory on the Arduino Uno/ SparkFun Redboard I did not have the space to keep this function working. I did keep the code that should accomplish this task in the main code, but I had to comment it out. If upgraded to an Arduino with more memory it may be possible to then uncomment and use this code. Also due to this shortage of memory the device seems to be unstable sometimes. If the code is reset it will work fine again, but this can be an annoying hassle. I also had to seek out a lot of help on how to organize the code correctly. Several times I would mistakenly misuse integer and float values. The GPS can also be a pain to work with as it has difficulties receiving a signal indoors. Also as stated above it took a little time to get the device to effectively convert to the correct time zone and day, since it would change the day based on the GMT time and not my time zone.

CS111- Intermediate Project 1- 1

If you would like to build your own Tweet-O-Temp keep reading below for parts, schematics, sketches, and code.

Parts:
(1) Arduino Uno/ SparkFun Redboard (a microcontroller with more memory may make the device more stable.)
(1) Arduino Ethernet Shield
(1) Ethernet Cable
(1) TMP 36 Temperature Sensor
(1) Adafruit GPS Breakout (an Adafruit GPS Ultimate Breakout should also work)
(1) Push Button
(1) 10K Ohm resistor
Several wires

The basic schematic and sketch can be seen below.

Intermediate Project 1 schematic

Intermediate Project 1 sketch_bb

You obviously want to put the Ethernet shield over the Arduino/Redboard. Then run five volts into one end of the button, and from the other end have a resistor run to ground and a cable running to a digital input pin, I used 5. This will mean that the current runs into the button and when the switch is open, or the button is unpressed, the current will flow through the resistor into ground. Then when it is closed it will flow into the pin on the Arduino which we set up as an input in the code. Then with the flat end of the temperature sensor facing you you want to run ground on the right, 5 volts on the left, and run the middle to an analog pin, I used A0. This will allow the current to run into the sensor and depending on the temperature it will send a current to the analog pin and the rest will be passed to ground. Then on the GPS you want to run 5 volts to into the VIN pin on the GPS, run the pin labeled GND on the GPS to ground, then run the RX pin to pin 2 on the Arduino and the TX pin to pin 3. This works the similarly running in the five volts into the device and to ground to complete the circuit and using the two pins on the Arduino to receive the information.

The code can be found from the link below

Intermediate_Project_1

The comments on the code explain the majority of how it works, but I will highlight the key feature of how it sends the Tweet. The Arduino monitors the state of the button and when it registers that it has been pushed it will then call the function below. This function works by first calling a function to adjust the time to the correct time zone and day. Then it reads the temperature and depending on what the temperature is will send a message to Twitter. The %d in the message are place holders which will insert the appropriate information and thus make the message actually have the appropriate readings.

void sendTweet(int degreesF) {
  getTime();
  if (degreesF >= 70) {
    sprintf(msg, "Date:%d/%d/20%d  Time:%d:%d:%d It is currently %d °F near %d°, %d°. Look's like a hot one today. Bust out the shorts! - Arduino", GPS.month, GPS.day, GPS.year, GPS.hour, GPS.minute, GPS.seconds, degreesF, int(GPS.latitudeDegrees), int(GPS.longitudeDegrees));
  }

  if ((degreesF < 70) && (degreesF >= 40)) {
    sprintf(msg, "Date:%d/%d/20%d Time:%d:%d:%d It is currently %d °F near %d°, %d°.  LIttle chilly today, better take a jacket with you. - Arduino.", GPS.month, GPS.day, GPS.year, GPS.hour, GPS.minute, GPS.seconds, degreesF, int(GPS.latitudeDegrees), int(GPS.longitudeDegrees));
  }
  if ((degreesF <= 40) && (degreesF >= 0)) {
    sprintf(msg, "Date:%d/%d/20%d Time:%d:%d:%d It is currently %d °F near %d°, %d°.  Better bundle up, looks cold out there. - Arduino", GPS.month, GPS.day, GPS.year, GPS.hour, GPS.minute, GPS.seconds, degreesF, int(GPS.latitudeDegrees), int(GPS.longitudeDegrees));

  }
  if (degreesF < 0) {
    sprintf(msg, "Date:%d/%d/20%d Time:%d:%d:%d It is currently %d °F near %d°, %d°.  It's freezing out there, you should stay in. -Arduino", GPS.month, GPS.day, GPS.year, GPS.hour, GPS.minute, GPS.seconds, degreesF, int(GPS.latitudeDegrees), int(GPS.longitudeDegrees));
  }

  if (twitter.post(msg)) {
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
  delay(3000);
}

As I have already mentioned the project could use some major improvements. Again, using a device with a larger memory should make the circuit more stable. Also it would allow for the ability to store values on the Arduino itself which could make the device more practical when paired with a battery. Also pairing it with a Wifi shield would allow for a another more practical device which could send without being connected to the wall. Also the temperature sensor seems to be touchy about its accuracy. I am not sure it this is a problem in the code or just the device itself, I believe it is the latter. But if this could be improved this would definitely help the device to be more accurate. Also since the GPS device works much better outside it would be much better to build a case for it so that it could be stored outside and then have the button inside for easier access.

Thanks for reading!

Final Project Plan

The goal for my final project is to have it be a simple game displayed on an LED matrix. I am still planning on making the game something like Snake, again depending on if my abilities allow. Another goal is to have it set up so that after one finishes they are able to simply push a button and send their score to Twitter. This would also most likely use the GPS to state the position and time of upload. Then if time and memory allow including in the Piezo buzzer for music and sound would be an additional goal.

As stated above I want it to have the capability to play a simple game, to have it able to send a high score to the internet at the push of a button. And if time permits to play a small song along with the game.

I will need a LED matrix, an Ethernet shield, a GPS module, several buttons for controlling the game/ uploading the score to Twitter, perhaps a 9 volt battery if I want to make it mobile, and if I do want to include a song then a Piezo buzzer.

This will cover the topics of a LED matrix, Ethernet, GPS time, GPS position, buttons/switches, and if I decide it will also deal with a Piezo buzzer.

I would like to start working on it this weekend, with hopefully having a circuit done by Sunday and then begin the coding. I would like to have figured out a rough code by the time we meet for class on Tuesday so if I have any questions I can have them addressed in class. I would then like to have a finalized version for Wednesday if possible.

Intermediate Project 1- Twitter temperature

Some goals I have with this project is that I want it to have the ability to read the temperature and then with the push of a button I would like it to send that temperature to Twitter. At this same time I would like it to include the time and position when and from where the tweet was sent. I would also like it so that when it reaches a certain time it will automatically make a tweet. So at like 8 in the morning it will send a tweet and that way all someone has to do is check there Twitter to see the current temperature and won’t have to check the weather. I would also like it to say a little message depending on what the temperature is. Like if it is below freezing it could say, “It’s going to be a cold one, bundle up out there”.

The planned capabilities are very similar to the goals previously state. I want a device that reads the temperature and with a push of a button or at a certain time will post it to Twitter. If I have the ability I would like to make it so the device is portable, meaning it would use a battery and then whenever it is plugged into an Ethernet cable it would update. That would give it a different capability, but after investigating the parts I found one page that said not to use the temperature sensor with anything more than 5 volts, i.e. not to use a 9 volt battery. So this my make this plan impossible to pull off. I will have to investigate further.

The article about not using a 9 volt battery can be found here.

The device will use a temperature sensor, a GPS breakout board, Ethernet shield, a push button, then all of the common items, such as wires, resistor, and the Arduino Uno/ SparkFun Redboard.

The topics it will cover will be Ethernet, GPS position, GPS time, temperature, and button and switches.

I have a rough circuit rigged up now and as far as I can tell all the parts work with individual test codes. I hope to have a rough code ready tonight and tomorrow. I will figure out about the battery in class soon and then if I am able to I will incorporate it soon. I hope to have it finalized and documented by Wednesday if everything permits.

Project 3 – Arduino Sketcher

This little project is a simple device I am calling the Arduino Sketcher. The name says the majority, but it is a simple device that using a Processing code and SparkFun Redboard can be used to draw pictures on your computer screen.

CS111 Project 3 - 2

The device is made up of four push buttons, one red, one green, one blue, and one without a color. Each one when pushed will change the color of the lines being draw to the color of the button, and in the case of the one without a color it will reset the drawing. If both the red and blue button are pushed and released at the same time it will make the color magenta, and the same goes for other color combinations. Thought this is very touchy and can be difficult to use. A RGB LED is displayed on the screen to show which color you are currently using. There is a soft potentiometer that when pushed at the top will move the line up, and when pushed down will move the line down. There is also a regular knob potentiometer which when turned to the right moves the drawing line to the right and when to the left moves the line to the left.

The assignment called for us to create an interface to use with this Processing code that our instructor provided to us. This interface needed a way to draw, change the color, and reset the canvas. I thought about it for a while and eventually decided that using the soft potentiometer could be an interesting and good idea to use for the drawing. I originally thought that I would be able to include all four directions on one soft pot, using the different ranges that the different spots on the soft pot provided for each direction. I soon changed this idea, because I thought it would make the soft pot too crowded. I then tried thought I would simply use two of these, but after experimenting with the knob potentiometer I found this was a fairly good interface device, and decided to pair it with the soft pot. I had decided pretty early that using the buttons and the RGB LED to control and display the color being used to draw with.

CS111 Project 3

I ran into a few issues while working on the project. Mainly it took me a while to figure out the code for how to get the sketching program to draw a line accordingly. My issue seemed to be I had a Serial.println() in my code which would interfere with the Serial communication of the Arduino and Processing code. I also had a line of code which would move the line back to its starting point and therefore make it so I could draw only one line at a time. I also had a wiring issue at one point which resulted in the color being switched to red anytime I pushed the lower part of the soft pot.

CS111 Project 3 - 3

If you’d like to build your own Arduino Sketcher keep reading for sketches, schematics, and code.

Parts:

(1) Arduino Uno/SparkFun Redboard
(1) solder less breadboard
(4) push buttons
(1) RGB LED
(1) soft potentiometer
(1) potentiometer
(3) 330 Ohm resistors
(6) 10K Ohm resistors
Several Wires

The basic sketch and schematic are below.

Project 3 sketch_bb

Project 3 schematic

For each of the potentiometers you want to run 5 volts into the right pin, from the middle pin you want to run a wire to one of the analog input pins, in my case I used A0 for the knob potentiometer and A1 for the soft pot. You also want a 10K Ohm resistor running to ground from the middle pin. Then you want to run a wire from the left most pin into ground. For the buttons you want to run 5 volts into one end and then out of the other end you want to run a 10K Ohm resistor to ground and a wire to any digital pin, I used 7, 8, 3, and 2. Then for your RGB LED you want to run the ground pin to ground and use a 330 resistor to direct the current to the wires which connect to other digital pins, I used 4, 5, and 6.

The Arduino code used can be viewed below.

Project_3

The comments in the code should explain the majority of what the code does, but basically it monitors the state of all the devices and calls a function when appropriate. For example, when the state of the red button changes it calls the colorRed() function which can be seen below.

void colorRed() { //turns the LED and the color of drawing red
  digitalWrite(LEDblue, LOW);
  digitalWrite(LEDgreen, LOW);
  digitalWrite(LEDred, HIGH);
  Serial.write('c');
  Serial.write(255);
  Serial.write(0);
  Serial.write(0);
}

Similarly when the soft pot reads a value between two numbers it will call a function to move the line appropriately, such as movedown() seen below

void movedown() { //moves the line down
  Serial.write('d');
  Serial.write(xVal);
  yVal = yVal += 1;
  Serial.write (yVal);
  delay(100);
}

I set up the code calling so many functions so the loop() function can be rather simple and simply call a function when it is needed.

I could not upload the Processing code that was used, but it was provided by my instructor. I made one small change to the code, I changed the point so that the lines draw to the sides, and not to the middle. I did this by changing prevX from width/2; to width; and did the same to prevY and height.

The project could use a few improvements. As state earlier changing to colors that are mixes, such as magenta, yellow, and cyan, are very difficult to work properly. In addition we do not have access to the colors white or black. I almost coded these in, but given the difficulty of getting the colors when pushing only two buttons, I doubt it would be able to press and release three at the same time. One way to fix this would be to move the project to a bigger breadboard and add more buttons. I only had four to work with and it was too late to gather more by the time I realized my initial play, which is what I used, was not the most effective. The knob potentiometer is also a little rough on the hand to use after a while, I tried to correct this by using a pen to make a make shift knob. However, this does not stay put all the time and it sometimes moves without moving the knob pot, causing occasional glitches while drawing. And on rare occasions the soft pot will say it is receiving input when no one is touching it. This could just be the hardware, but it could also be a small glitch in the code or wiring and could possible be corrected. There is also a bit of a delay caused when one switches colors it makes it so we have to wait a few seconds for the device to begin drawing again. I believe this is just caused by the Processing code working through each command and byte, but if it could be corrected it would be a nice way to clean it up.

Thank you for reading!

Updated Topic Plan

After looking over the topics I wanted to address and my rough ideas I found a few ideas to narrow down on. (Note: parts in bold are topics that will be implemented if time allows and the project does not become more than what was previously expected.)

For one independent project I still like the idea of using the temperature sensor, Ethernet connection, the GPS time, and the GPS position. I would like to set this up so that the device will read the temperature of wherever you are and post it to social media. My plan is that the post will include the time and location and a little message depending on the temperature, such as if it is warm out it could say something about wearing shorts today. One useful way to do this would be to have it read the temperature all the time and then upload it when plugged into the Ethernet. That way it could be taken with some one and not be as stuck in one place. This would require a battery to keep power going to the device. I also think it would be a good idea to set it so that it will post the temperature at a designated time in the morning. In other words at like 9 am it would check and post so if you check your feed you will know what temperature to expect.

Next I would like to use the triple-axis accelerometer and a DC motor to create a small little device that will move forward or backward depending on how the accelerometer is moved. I would most likely add a shift registor to this project to add LEDs to make the device look nicer and fancier. If time and my skills allow I could also use a servo motor to attached to the DC motor wheels so it can turn. This will depend if I have the ability to design an appropriate design.

For the final I am planning on sticking with the game idea. I would use the LED matrix for the display of the game. Right now I am considering a game like Snake, but we will have to see what I am capable of coding on the device. I would also plan to use the Ethernet so that the player can post their score to social media if they want. I could include the GPS position so it says where the player is, but given that social media usually states where the poster is at the time, this may be redundant and not included. I think using the Piezo buzzer or speaker to play a little tune during the game would be a good idea as well. Also, though it was not initially on my topics list, including a LCD display could be good so the viewer sees their score before they decide to post it. I may be able to do this on the LED matrix too, we will see.

Tweet-A-Pot

Tweet-a-pot

If you enjoy a good cup of coffee and are one of those people who is never apart from their social media for long then this is a project you’d want to make. The Tweet-a-Pot monitors your Twitter account for a specific hashtag and upon receiving this hashtag turns on a coffee machine which will make you a pot of coffee. Then if you Tweet again or Tweet a specific hashtag it will stop the machine.

If you want to know more, see it in action, or build your own then click here