<commit_before>
**The etch-a-sketch is one of the most frustrating drawing toys from childhood. Can we combine some code and motors to make this toy produce nice pictures?** <a href="#bingo"> Yes! <a/> 

<style>
.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
}

</style>

## Background
The Etch-A-Sketch was released by The Ohio Art Company in 1960. Ever since then, it's been a staple toy in homes, daycares, waiting rooms and schools -- over 100 million have been sold worldwide. It was even a big enough deal to be a character in Toy Story 1 and 2!

<img src="https://vignette.wikia.nocookie.net/pixar/images/b/bc/DetectiveScene_TS2Etchb4.jpg" style="height:250px;" class="center"/> 

 The popularity of the device is sort of interesting because it has lots of annoying problems:
- knobs only move the cursor up/down/left/right. Diagonals and curves require a steady precision that most children 6+ simply don't have. 
- you can't draw two objects that are not connected.
- No ability to undo small mistakes. If you want to undo something you'll have to redo your whole masterpiece.

## The Plan

**Write a program that uses some motors to draw nice pictures on an etch-a-sketch.** For the reasons above, recreating pictures by hand is tedious and extremely difficult. My hope was to get to a point where my program could draw things better than I could. I've always wanted to learn more about how to use hardware with python so I was pretty excited to start this one. 

## Hardware Setup

Parts List Recipe:
- [Raspberry Pi](https://www.amazon.com/CanaKit-Raspberry-Micro-Supply-Listed/dp/B01C6FFNY4 "Amazon")
- [Etch a Sketch](https://www.amazon.com/gp/product/B01MTS465O "Amazon")
- 1x Piece of Wood
- 2x [Stepper Motor](https://www.amazon.com/gp/product/B015RQ97W8 "Amazon")
- 2x [Shaft Coupler](https://www.amazon.com/gp/product/B01LW5RREA "Amazon")
- 8x [Jumper Cables](https://www.amazon.com/gp/product/B01LZF1ZSZ "Amazon")

I've got the Amazon links above so it's extra clear what I got -- it seems like for these parts many of the manufacturers seem pretty similar, so I just picked the cheapest at the time. 

### 1. Hooking Up Motors to Raspberry Pi

Since the Raspberry Pi is just a tiny computer, we're going to be able to use all the python libraries we know and love. Additionally, it has a set of GPIO pins to use. I connect a few of these pins to each of two controllers + motors. By changing the voltage on these pins, I can make motors to rotate. [Here's a link](https://medium.com/@Keithweaver_/controlling-stepper-motors-using-python-with-a-raspberry-pi-b3fbd482f886) to the guide I followed to get this set up. 

### 2. Connecting Motors to Knobs

With some force, the knobs can be removed from the etch-a-sketch. This reveals a shaft on the device. The motor also rotates a shaft, so to connect them together I used a chinese-finger-trap-like device called a coupler. It's somewhat flexible, with two small sets of screws on either side that allow it to be tightened around each of the shafts. Here's a mini-guide!

<img src="https://raw.githubusercontent.com/sunnybala/sunnybala.github.io/master/assets/attaching-motor.png" style="height:400px;" class="center"/>

### 3. Stabilizing 

There's quite a bit of resistance on the etch a sketch knobs. Without doing anything, if we set the motor to rotate, the knob will actually stay still and the motor itself will rotate! 

I went to a local hardware store with the dimensions of the build and they were able to cut me a block of wood that I could screw the motors into to keep them stable. I glued the wood on the bottom to the etch-a-sketch body. This can be tricky if the wood is not appropriately sized. With a screw too loose and the motor will wiggle around when it's trying to rotate. If it's too tight, the motor will be squeezed against the coupler and not be able to rotate. 

Final build!


## Software Setup

The descriptions below will be mostly high level but all the code for this project is available on GitHub if you'd like to dive in at a deeper level.  

### 1. Controlling Motors

The raspberry pi python library **rpi** lets us control the motor via the pins. I created a class to represent the motor. I'll be using this in the code is in the following way:

{% highlight python %}

horizontalMotor = StepperMotor(horizontal_pins)
verticalMotor = StepperMotor(vertical_pins)

horizontalMotor.rotate(40)  # move cursor 40 units right
horizontalMotor.rotate(-40) # move cursor 40 units left<commit_msg>fixing typo<commit_after>
**The etch-a-sketch is one of the most frustrating drawing toys from childhood. Can we combine some code and motors to make this toy produce nice pictures?** <a href="#bingo"> Yes! <a/> 

<style>
.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
}

</style>

## Background
The Etch-A-Sketch was released by The Ohio Art Company in 1960. Ever since then, it's been a staple toy in homes, daycares, waiting rooms and schools -- over 100 million have been sold worldwide. It was even a big enough deal to be a character in Toy Story 1 and 2!

<img src="https://vignette.wikia.nocookie.net/pixar/images/b/bc/DetectiveScene_TS2Etchb4.jpg" style="height:250px;" class="center"/> 

 The popularity of the device is sort of interesting because it has lots of annoying problems:
- knobs only move the cursor up/down/left/right. Diagonals and curves require a steady precision that most children 6+ simply don't have. 
- you can't draw two objects that are not connected.
- No ability to undo small mistakes. If you want to undo something you'll have to redo your whole masterpiece.

## The Plan

**Write a program that uses some motors to draw nice pictures on an etch-a-sketch.** For the reasons above, recreating pictures by hand is tedious and extremely difficult. My hope was to get to a point where my program could draw things better than I could. I've always wanted to learn more about how to use hardware with python so I was pretty excited to start this one. 

## Hardware Setup

Parts List Recipe:
- [Raspberry Pi](https://www.amazon.com/CanaKit-Raspberry-Micro-Supply-Listed/dp/B01C6FFNY4 "Amazon")
- [Etch a Sketch](https://www.amazon.com/gp/product/B01MTS465O "Amazon")
- 1x Piece of Wood
- 2x [Stepper Motor](https://www.amazon.com/gp/product/B015RQ97W8 "Amazon")
- 2x [Shaft Coupler](https://www.amazon.com/gp/product/B01LW5RREA "Amazon")
- 8x [Jumper Cables](https://www.amazon.com/gp/product/B01LZF1ZSZ "Amazon")

I've got the Amazon links above so it's extra clear what I got -- it seems like for these parts many of the manufacturers seem pretty similar, so I just picked the cheapest at the time. 

### 1. Hooking Up Motors to Raspberry Pi

Since the Raspberry Pi is just a tiny computer, we're going to be able to use all the python libraries we know and love. Additionally, it has a set of GPIO pins to use. I connect a few of these pins to each of two controllers + motors. By changing the voltage on these pins, I can make motors to rotate. [Here's a link](https://medium.com/@Keithweaver_/controlling-stepper-motors-using-python-with-a-raspberry-pi-b3fbd482f886) to the guide I followed to get this set up. 

### 2. Connecting Motors to Knobs

With some force, the knobs can be removed from the etch-a-sketch. This reveals a shaft on the device. The motor also rotates a shaft, so to connect them together I used a chinese-finger-trap-like device called a coupler. It's somewhat flexible, with two small sets of screws on either side that allow it to be tightened around each of the shafts. Here's a mini-guide!

<img src="https://raw.githubusercontent.com/sunnybala/sunnybala.github.io/master/assets/attaching-motor.PNG" style="height:400px;" class="center"/>

### 3. Stabilizing 

There's quite a bit of resistance on the etch a sketch knobs. Without doing anything, if we set the motor to rotate, the knob will actually stay still and the motor itself will rotate! 

I went to a local hardware store with the dimensions of the build and they were able to cut me a block of wood that I could screw the motors into to keep them stable. I glued the wood on the bottom to the etch-a-sketch body. This can be tricky if the wood is not appropriately sized. With a screw too loose and the motor will wiggle around when it's trying to rotate. If it's too tight, the motor will be squeezed against the coupler and not be able to rotate. 

Final build!


## Software Setup

The descriptions below will be mostly high level but all the code for this project is available on GitHub if you'd like to dive in at a deeper level.  

### 1. Controlling Motors

The raspberry pi python library **rpi** lets us control the motor via the pins. I created a class to represent the motor. I'll be using this in the code is in the following way:

{% highlight python %}

horizontalMotor = StepperMotor(horizontal_pins)
verticalMotor = StepperMotor(vertical_pins)

horizontalMotor.rotate(40)  # move cursor 40 units right
horizontalMotor.rotate(-40) # move cursor 40 units left
{'fraction_non_alphanumeric': 0.07399251266240917, 'fraction_numerical': 0.014093811935696983, 'mean_word_length': 4.068638392857143, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 16, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3093487', 'n_tokens_mistral': 2801, 'n_tokens_neox': 2516, 'n_words': 1320}