Quote of AWESOMENESS: “Thinking is already a conversation” ~ Paul Pangaro
Hey there!
I know it’s been awhile but here I am with another Python project. Today, you will be learning the code for blackjack! Just put it out on your editor, study the code, and explore the game on your terminal. It was quite some challenge getting toward the end of finishing up the game. And, at some point, problems and bugs constantly came up that I didn’t that it would actually get done. BUT, nevertheless, it’s finally hereeee!!
*ahem, drum rollllllllll*
*cricket sounds*
*Umm….. DRUMMM ROLLLL!!!……. please?*
*DRUM ROLLLLLLLLLL (in the background, finally)*
TADA!!!!:
#import the random library from python
from random import *
#Function Definitions
def deal_from_deck(deck):
if len(deck)<1:
create_deck(deck)
shuffle_deck(deck)
deal_cards()
def deal_cards(deck):
return deck.pop(0)
def define_cards(n):
rank_string = ("ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen","king")
suit_string = ("clubs", "diamonds", "hearts", "spades")
cards = []
for suit in range(4):
for rank in range(13):
card_string = rank_string[rank] + " of " + suit_string[suit]
cards.append(card_string)
return cards[n]
def create_deck(deck):
for i in range(52):
deck.append(i)
return
def shuffle_deck(deck):
shuffle(deck)
return
def card_value(n):
vals = (11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10)
card_vals = []
for s in range(4):
for r in range(13):
card_vals.append(vals[r])
return card_vals[n]
def card_display(n):
name = define_cards(n)
val = card_value(n)
print (name + " : " + str(val))
def show_cards(hand):
for card in hand:
card_display(card)
def hand_val(hand):
list = []
for card in hand:
list.append(card_value(card))
if sum(list) > 21:
for i in range(len(list)):
if list[i] == 11:
list[i] = 1
return sum(list)
# Initializing
drawn_hands = 0
player_wins = 0
dealer_win = 0
deck = []
player_hand = []
dealer_hand = []
playing = True
#Processing
create_deck(deck)
shuffle_deck(deck)
for i in range(2):
card = deal_cards(deck)
card2 = deal_cards(deck)
player_hand.append(card)
dealer_hand.append(card2)
# put in a loop for player hand
print()
print("--------------------")
print("Dealer shows: ")
card_display(dealer_hand[0])
print("--------------------")
while playing == True:
player_val = hand_val(player_hand)
print()
print()
print("* * * * * * * * * * *")
print("Your current hand is...")
show_cards(player_hand)
print(hand_val(player_hand))
print("* * * * * * * * * * *")
print()
print()
if player_val < 22:
response = input("Would you like another card?")
if response == "yes":
player_hand.append(deal_cards(deck))
else:
playing = False
else:
playing = False
print("sorry you busted")
print("Thanks for playing")
exit()
# check if it is higher than 21, if yes then end program, dealer hand wins
show_cards(dealer_hand)
playing = True
while playing == True:
if hand_val(dealer_hand) < 17:
dealer_hand.append(deal_cards(deck))
else:
playing = False
print()
print()
print("****************")
print("Dealer has:")
print(show_cards(dealer_hand))
print("****************")
print()
print()
if hand_val(dealer_hand) > 21:
print("The dealer busted")
show_cards(dealer_hand)
print("You win!")
exit()
elif hand_val(dealer_hand) > hand_val(player_hand):
print("Dealer wins")
show_cards(dealer_hand)
exit()
elif hand_val(player_hand) > hand_val(dealer_hand):
print("You win!")
show_cards(player_hand)
exit()
else:
print("It's a draw")
exit()
This project is just extremely awesome and awe-spiring that I can’t wait for you guys to try it out!
We. Are. Going. To. Make. An. Arduino. Robot. Arm!!!!!! I know, I know super cool. Now, without further ado, let’s jump right into this project.
The following are what you will need to gather:
1x Arduino UNO Board
1x Breadboard
1x USB Cable Type A/B
2x Button
2x 10k-Ohm Resistor
3x Potentiometer
5x LED (A.K.A light-emitting diode, A.K.A miniature looking light bulb-ish)
5x 220 Ohm Resistor
20x Jumper Wires
Step 1:
First, let’s start by putting the potentiometers, the buttons, and the LED’s on our breadboard.
We will be placing 3 potentiometers, 2 buttons, and 5 LED’s. For your reference, the photo above can help. Yes, I did say 5 LED’s but I forgot to place the fifth LED in this photo, but you should put 5.
Here are some important basics to understand about an LED:
Cathode (-, shorter side)
Anode (+, longer side)
Step 2:
RESISTORS. Resistors. resistors.
Here, in this photo, I’ve put 5 LED’s
Place five of your 220 Ohm Resistors on each of the Anodes (+) sides of each of your LED’s and the other end of the resistor to the negative side lane on your breadboard which we will later connect to the GND on your Arduino board. I’ve placed another photo above showing where I’ve placed my 220 Ohm Resistors.
Here, I circled each of the legs (Anode side) which I connected my 220 Ohm Resistors to
Now, take both of your 10k-Ohm Resistors. You will have to place one side of your resistor to only one of the button’s leg. Do this for both buttons. Here is another picture but of the buttons and resistor placing.
We have finished placing all the resistors we will be needing in this project. You guys are doing an awesome job, btw!
Step 3:
This step will be really quick.
All we need to is get two jumper wires, first. Then, connect the Negative lane on your breadboard to your GND on your Arduino, and, the positive lane of on your breadboard to the 5V on your Arduino (you can find this right beside the GND, usually).
Here is the overview of everything so far
Done with this step. Great job!
Step 4:
For this step, you will be needing 5 of your jumper wires.
We will be connecting each of the LED’s on the breadboard onto the Arduino UNO board with your jumper wires.
Here is the first LED I connected. We will be connected the other leg (cathode, shorter side) of the LEDs to these numbers on the Arduino in order:
2
3
4
7
8
I will also be numbering the LEDs on the next photo so that it will be easier to know which we will be connecting to which.
This shows where I connected my jumper wires to the LEDs
We will connect:
LED 1 to 2 on the Arduino board
LED 2 to 3 on the Arduino board
LED 3 to 4 on the Arduino board
LED 4 to 7 on the Arduino board
LED 5 to 8 on the Arduino board
Step 5:
I know that wiring could be a tedious job but it’s okay because I know you can do it.
Wiring is very important because it connects. It works as that bridge that can help cars go back and forth and connect with other cities. If you have a bulb and a power source you can’t just put it beside each other or clink it together to light the bulb up (even that would be super cool if that’s how easy it was), you need to connect it through wires. So, let’s continue? Yes.
In this step, we will be wiring the buttons and the potentiometers.
Here in this photo I got two jumper wires and connected one end to the remaining unconnected leg of the buttons and connected the other side of the jumper wire to the positive (+) lane on the breadboard
That was easy, wasn’t it?
We are nearly done!
Let’s start wiring the potentiometers now.
For the potentiometer, it’s going to be slightly harder.
In this photo, I’ve connected 3 jumper wires to the right side of the potentiometer then connected the other end of the wire to the Negative lane of the breadboard (A.K.A GND).
And, then, I got 3 more wires and connected it to the left side of my potentiometer then connected the other end to the Positive Lane on the breadboard.
I’ve added the numbers on top of each potentiometer so that it’s easier to understand how to connect it to the Arduino.
As you may see on the picture to your left, I’ve added 3 white jumper wires to each of the middles of the potentiometers because we will connect this to the Arduino Board. Connect potentiometer 1 to the A0 on the Arduino UNO Board | Connect potentiometer 2 to the A1 on the Arduino UNO Board | Connect Potentiometer 3 to the A3 on the Arduino UNO Board.
Step 6:
In this step, grab two more jumper wires and connect both ends of the lanes on each side of the breadboard to each other. I used the black and red wire here.
You will need to connect the Negative lanes on each side to each other and the Positive lane on each side to each other.
Tada! Next step.
Step 7:
Seeeeervoooo Moooootooooors.
We are nearing the last of our steps. KEEP GOING!
We will add the servo motors to our breadboard and I put mine side by side to each other.
I connected three jumper wires each for three of my servos, meaning 9 jumper wires in total.
About servo motors in Arduino:
They usually have 3 plug pins (this is where I connected my jumper wires to)
Take a look at your servo motor. You will probably notice there are three colored wires. Each color represents 3 different things. The colors could actually be different sometimes but, as you see here, my colors are brown, red, and yellow.
The brown is the Ground/Negative (-)
The red is Positive (+) [This wire is usually located at the center of all the three wires]
The yellow is the Signal
Your wires on your servo motors might be different and you might be thinking “WHAT!?!?” but it’s okay (that’s what I thought too) here is how you can compare it to mine to find the similarities:
The Ground/Negative is usually | Black or Brown (in my case, it’s brown)
The Positive is always Red
The Signal can be either | Orange, Yellow, or White (in my case, it’s Yellow)
Better?
Awesome, let’s proceed.
These are what you need to connect with your Servos:
Connect each of the GND/Negative (Black or brown wire) on each of your servo motors to the positive lane on your breadboard (I used a white jumper wire, check the photo above for reference)
Connect each of the Positive (Red wire) on each of your servo motors to the negative lane on your breadboard (I used an orange jumper wire)
Lastly, connect each of the signals (Orange, Yellow, or white wire) of your servo motors to: 5, 6, and, 9 on your Arduino
I know it’s a bit of a mess. But, there are three wires that you can find colored: 1x black & 2x green. These are the wires that connect to the Arduino
Step 8:
This is our last time! Congratulations!!!
Connect your Arduino to your computer, fire up the Arduino app on your computer, copy paste the code below and press Upload.
Now, the challenge whether you choose to accept it or not is to build your own robot arm out of cardboard or you can check this —-> here which was made by Ryan Chan. Shoutout to him, btw. I hope you enjoyed this! I certainly had an awesome time and I hope you did, too!
You can teach others and share your awesome knowledge on building an arduino robot arm, I give some bragging rights.
With this project, you will be able to control two servo motors with a joystick with Arduino!
For this project you will need to gather:
Arduino Board
Breadboard
Wires | x13
Servo Motors | x2
Battery
To make this (really cool) project, you will need to:
Step 1: Battery
Connect your external battery to your breadboard.
Step 2: Wiring
Get a piece of wire and connect one end to GND on your Arduino Board and the other end to the negative channel on your breadboard.
Step 3: Servo Motors
Get your two servo motors and 6 wires. Connect the VCC and the GND of the two servos to the VCC and GND on the breadboards inputs. Then, connect your first Servo Signal Connect to the Arduino Digital PMW 3 on your Arduino Board.
In case you aren’t very familiar with the Arduino Digital PMW 3, it looks like the photo below as well as the Digital PMW 5 and the blue wire from the first servo…
Your second Servo Signal Connect should be connected to the Arduino Digital PMW 5.
Step 4: JoyStick
Connect the GND on your Joystick to the GND on the Arduino Board.
Then, connect the +5V on your joystick to the 5V on the Arduino Board.
Then, connect the VRx on your joystick to the A0 on the Arduino Board.
Then, connect the VRy on your joystick to the A1 on the Arduino Board.
The photo below shows where I connected what and where. Just look at the corresponding colors to which wire and the connection.
It also looks something like this….
Step 5: The Code
Open up your Arduino on your computer and copy the code below:
//add the servo library #include <Servo.h>
//define our servos Servo servo1; Servo servo2;
//define joystick pins (Analog) int joyX = 0; int joyY = 1;
//variable to read the values from the analog pins int joyVal;
Once you verify and save the code, you can connect your Arduino board to your computer and upload the code to your board. Tada!
Step 7: Have fun and Experiment
Yay! We’ve finally finished yet another Arduino Project. You guys are so awesome! Now, after playing around with this, you can try to experiment maybe add more things (an example, “lights, perhaps?”).
Wait, if you are having any problems uploading or your project isn’t functioning properly, you should double check your wiring and maybe check which Port your Arduino (on your computer) connected to in the tools. If there is anything else don’t be shy to comment down below!
You must be logged in to post a comment.