-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revised book, introduction and image processing changes
- Loading branch information
1 parent
e3fe074
commit c6d4115
Showing
45 changed files
with
11,863 additions
and
3,293 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Electronics | ||
|
||
In this chapter, we will cover the a brief introduction to electronics and an overview of Arduino and how it can be used to solve the mobile games. We also have seperate section on various sensors that you can use to detect what's on the screen and different various of automating the touch. | ||
|
||
We used Arduino in this book for the projects because it is easy to setup and use. Feel free to use the microcontroller that you are comfortable with. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Introduction to Arduino | ||
|
||
Arduino is an open-source platform used for building electronics projects. Arduino consists of both a physical programmable circuit board (often referred to as a microcontroller) and a piece of software, or IDE (Integrated Development Environment) that runs on your computer, used to write and upload computer code to the physical board. | ||
|
||
Jeremy Blum's Arduino tutorials on the YouTube are one of the best out there, watch the first three videos to get the basic understanding of Arduino. | ||
|
||
Link: https://www.youtube.com/playlist?list=PLV009FNOX7Tf-XSyghg2vrSYXw1QcCHaX | ||
|
||
<iframe width="560" height="315" src="https://www.youtube.com/embed/videoseries?list=PLV009FNOX7Tf-XSyghg2vrSYXw1QcCHaX" frameborder="0" allowfullscreen></iframe> | ||
|
||
##### Software | ||
Arduino also has an open-source software component which is similar to C++. The Arduino integrated development environment (IDE) allows you to write code, compile it, and then upload it to your Arduino for stand alone use in prototyping and projects. | ||
A typical Arduino C/C++ sketch consist of two functions that are compiled and linked with a program stub main() into an executable cyclic executive program: | ||
|
||
***setup()***: a function that runs once at the start of a program and that can initialize settings. | ||
|
||
***loop()***: a function called repeatedly until the board powers off. | ||
|
||
Sample program: (for blinking an LED) | ||
```C | ||
#define LED_PIN 13 | ||
|
||
void setup() { | ||
pinMode(LED_PIN, OUTPUT); // Enable pin 13 for digital output | ||
} | ||
|
||
void loop() { | ||
digitalWrite(LED_PIN, HIGH); // Turn on the LED | ||
delay(1000); // Wait one second (1000 milliseconds) | ||
digitalWrite(LED_PIN, LOW); // Turn off the LED | ||
delay(1000); // Wait one second | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Introduction to Electronics | ||
|
||
Electronics deals with the use of circuits that involve various electrical components. Everything from the clock you see to find time to the smartphone in your hand connecting you to your friends every second are the applications of it. In this section, we will give a brief overview of the electronics that we need to get started with working on the book. | ||
|
||
If you are a beginner, this video series on YouTube is a great way to start- “Building Electronic Circuits” by The Motivated Engineer. It starts off by giving you an overview of the basics of the electronic components generally used and teaches you how to work on some cool projects such as infra red sensors and wearable circuits. Watching and practicing the circuits in the first five videos in this series would give you a decent level of understanding to continue reading the sections below. These videos are optional to watch for people who have worked on electronic circuits before. | ||
|
||
Link: https://www.youtube.com/playlist?list=PLmcMMZCV897om7Wuqz882Jdp9lGj9HYHs | ||
|
||
<iframe width="560" height="315" src="https://www.youtube.com/embed/videoseries?list=PLmcMMZCV897om7Wuqz882Jdp9lGj9HYHs" frameborder="0" allowfullscreen></iframe> | ||
|
||
Next, we explain a simple circuit that is used very commonly while trying to solve mobile games. | ||
|
||
### Voltage Divider Circuit | ||
|
||
 | ||
|
||
A voltage divider circuit is commonly used in many electronic circuits for a wide range of applications including adjusting level of signal, measurement of voltages, etc. It is used in multimeter and wheatstone bridge. It produces an output voltage which is a fraction of the input voltage. The value of the output voltage depends on the values of the resistors used. | ||
|
||
The output voltage of the above circuit can be calculated by the following equation. | ||
|
||
Vout = ((R2) / (R1 + R2)) * Vin | ||
|
||
We will use this circuit in the next chapter when we talk about the Light Dependent Resistor (LDR). | ||
|
||
LDR can be used in place of one of the resistors. The resistance of the LDR decreases if the incident light increases, and vice-versa. This change in resistance can be used to produce a change in voltage at the output which is important in many applications. For example, when we use a microcontroller, we can detect the lighting in a room by just measuring the voltage at the output of a voltage divider circuit in which an LDR is used in place of one of the resistors. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Sensors for automating games | ||
|
||
This section lists the various electronic sensors that can be used detect the differences on the phone screen. The common and main idea for all of these sensors is that it senses the light that is following and somehow convert it into an understandable format. For examples, LDR converts the change in light intensity that is falling on top of it. | ||
|
||
### Light Dependent Resistor(LDR) | ||
|
||
 | ||
|
||
An LDR is commonly used for wide range of applications because of it provides decently accurate information of the external lighting and at the same time economical. It is basically a light controlled resistor- which means that the resistance across it's terminals changes according to the light incident on it. This can be used in projects where you want to sense the lighting in the surrondings. One of it's common applications is to be used to turn on lights automatically in the evening. A video tutorial on how you can build such a circuit is below. | ||
|
||
Link: https://youtu.be/_uglvulpofQ | ||
|
||
<iframe width="560" height="315" src="https://www.youtube.com/embed/_uglvulpofQ?list=PLmcMMZCV897om7Wuqz882Jdp9lGj9HYHs" frameborder="0" allowfullscreen></iframe> | ||
|
||
|
||
#### How it works | ||
It works on the principle of photo conductivity. When light is incident on top of a LDR, the elctronics and holes are seperated- hence the conductivity increases i.e., resistivity decreases. When the light is not incident, they are very few freely moving holes and electrons- so the conductivity is less i.e., the resistivity is high. | ||
|
||
#### Circuit for LDR | ||
Assume that LDR is connected at Z1 and Z2 is a resistor. As discussed in the previous chapter, the LDR can be used in a voltage divider circuit to convert this change in resistance because of the external light to change in voltage. We are doing this because the microcontroller can only detect the change in voltage. | ||
|
||
 | ||
|
||
This is how the LDR must be connected to the Arduino. This circuit diagram has been created using fritzing. | ||
|
||
 | ||
|
||
#### Source code for using LDR with Arduino | ||
|
||
This is the code that you can use in Arduino to read the values from the LDR with the appropriate circuit. We will display the values returned by the LDR on the serial monitor so that we can observe the changes in the value returned in real time. | ||
|
||
Let's setup serial communication first. | ||
```C | ||
void setup() | ||
{ | ||
// initialize serial communications at 9600 bps: | ||
Serial.begin(9600); | ||
} | ||
``` | ||
|
||
Let's connect the LDR input to A0 pin on Arduino. | ||
|
||
```C | ||
void loop() | ||
{ | ||
// reading the value from sensor and storing it in a variable | ||
sensorValue = analogRead(A0); | ||
|
||
// print the output on serial monitor | ||
Serial.print("Sensor Value = "); | ||
Serial.println(sensorValue); | ||
|
||
// use a delay to see values clearly | ||
delay(20); | ||
} | ||
``` | ||
|
||
#### Sensor placement on screen | ||
|
||
Here's how the LDR can be placed on the top of the screen. | ||
|
||
 | ||
|
||
The output voltage from the LDR circuit changes depending on the screen color (either white or black). We have to choose a value in between both of them with we call the threshold to differentiate the colors. This threshold value can be found by watching the values of white and black on the serial monitor. | ||
|
||
Once the sensor threshold value is found, we can use the following code to perform tasks accordingly. | ||
|
||
```C | ||
void setup() | ||
{ | ||
thresholdValue = 500; // obtained from observation of serial monitor | ||
} | ||
|
||
void loop() | ||
{ | ||
// reading the value from sensor and storing it in a variable | ||
sensorValue = analogRead(A0); | ||
|
||
if(sensorValue > thresholdValue) | ||
{ | ||
// perform task when screen white | ||
} | ||
else | ||
{ | ||
// perform task when screen dark | ||
} | ||
|
||
delay(20); | ||
} | ||
``` | ||
|
||
#### Sensor protection from ambient light | ||
|
||
This threshold value could change depending on the ambient light in the room. So, we use a shield for the LDR to keep it. Shielding is optional but we suggest you to do that. This will make the values that LDR returns consistent irrespective of the external light. We make sure that the color of the shield is dark so that it would block more light. | ||
|
||
 | ||
|
||
#### Examples of application | ||
|
||
An LDR can be used to detect the different of bright and dark regions on the screen. If you have a dark region, the LDR has high resistance and vice-versa. This is appropriately reflected in the values sensed by the microcontroller so that appropriate action can be taken. | ||
|
||
This is used in the games Piano Tiles and Ready Steady Bang because the primary concept in this games is the identify the difference in intensities of light on screen. | ||
|
||
### RGB Sensors | ||
|
||
Instead of an LDR, we can also use an RGB sensor to differentiate colors on the screen. The additional advantage of the RGB sensor from the LDR is that, it can know the exact color on the screen unlike LDR with can you find the brightness of the color coming from the screen. | ||
|
||
An RGB sensor can clearly differentiate between blue and green, whereas the same would be hard for an LDR to do. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Touch Simulation | ||
|
||
Simulating touch is one of the critical parts of any game. There are multiple ways in which we can simulating the touch. | ||
|
||
### Mechanical | ||
|
||
The touch can be simulated by a mechanical system consisting of a stylus connected to servo motor. The stylus must be connected to the servo in such a way that when the servo rotates, the stylus hits the screen. | ||
|
||
This is the code that you can use to control the touch in this case. Let us assume that the mechanical arrangement is adjusted in such a way that the angle when the stylus touches the screen is 0 degrees and that the stylus doesn't touch the screen for 30 degrees. | ||
|
||
```C | ||
// import the servo motor library | ||
#include <Servo.h> | ||
|
||
Servo myservo; // create servo object to control a servo | ||
|
||
void setup() | ||
{ | ||
myservo.attach(9); // attaches the servo on pin 9 to the servo object | ||
} | ||
|
||
void loop() | ||
{ | ||
// below are the angles for touching | ||
// 0 - touch, 30 - no touch | ||
|
||
myservo.write(30); // simulate no touch | ||
delay(1000); | ||
|
||
myservo.write(0); // simulate touch | ||
delay(1000); | ||
} | ||
``` | ||
|
||
But the problem with this method is that it is relatively slow. It is constrained by the speed of servo movement. To overcome this problem, we can use one of the following two ways. | ||
|
||
|
||
### Using Relay | ||
|
||
This and the next method can only work on capacitive touch screens. For this we have to understand how they work. The electrodes apply a low voltage to the conductive layer creating a uniform electrostatic field. When a finger hits the screen a tiny electrical charge is transferred to the finger to complete the circuit creating a voltage drop at that point on the screen. The location of this voltage drop is recorded by the controller. | ||
|
||
 | ||
|
||
Fig: Depiction of how touch screen works. | ||
*Credit for image: The Curious Engineer, YouTube* | ||
|
||
We are going to use this concept, except that in the place of a finger, we use the ground pin on the arduino to transfer the charge on the screen. To have more surface area on the display of the screen, we use a coin. Relays are directly connected to the output pin of the Arduino. As shown in the figure, it is equivalent to a touch if the voltage given is high as there is a path for the current to flow to the ground. It is equivalent to not touching, if the voltage given is low. | ||
|
||
 | ||
 | ||
|
||
Here's how you have to connect the relay in a circuit to simulate touch. | ||
|
||
 | ||
|
||
Here's the code to simulate touch and no touch alternatively with a time period of two seconds. | ||
|
||
```C | ||
void setup() | ||
{ | ||
pinMode(4, OUTPUT); | ||
} | ||
|
||
void loop() | ||
{ | ||
// below are the values for touching | ||
|
||
digitalWrite(4, HIGH); // simulate no touch | ||
delay(1000); | ||
|
||
digitalWrite(4, LOW); // simulate touch | ||
delay(1000); | ||
} | ||
``` | ||
#### Touch simulation on screen | ||
|
||
Here's how the touch circuit can be placed on the top of the screen. | ||
|
||
 | ||
|
||
We should also make sure that there is enough contact between the wire from the output of the relay and the surface of the screen. Hence, we can use a coin or aluminium foil for this purpose. | ||
|
||
#### Debugging | ||
|
||
I must concede that getting the touch to work at first it hard. While using this circuit, try to have your phone in developer mode so that you exact understand when where the screen is being touched. | ||
|
||
If you circuit doesn't work directly, it's probably because the ground of your mobile and that of the circuit do not match. For overcoming this diffculty, you can try the following: make sure your circuit is not placed on an iron conductor, change the material used to increase the surface area of touch, try to plug both your circuit and the mobile into USB ports from the same computer, etc., | ||
|
||
### Using Transistors | ||
|
||
This method can work faster than a relay too. You have to use a transistor in place of a relay for switching between open circuit and ground. The code would be the same as above. |
Oops, something went wrong.