Reward Timer Button

by whatyadoing in Circuits > Raspberry Pi

162 Views, 0 Favorites, 0 Comments

Reward Timer Button

IMG_1419.JPEG
IMG_1421.JPEG
IMG_1420.JPEG
Sticker Reward

Do you have an addiction to your phone? Has another day been wasted? Well this button can solve your problem!


The Reward Button starts a timer that sends an email when the countdown reaches zero to alert the user to get to work. If the user does come back, the button rewards them with a sticker. However if the user chooses to ignore the email, plenty more will soon follow, spamming the phone with notifications until the button is pressed again.


This project was done as my side project for Ms. Berbawy's Principles of Engineering class.

Supplies

Resources:

Tools:

  • 3D-Printer
  • Laser Cutter
  • Vinyl Cutter
  • Hot Glue Gun
  • Gorilla Glue

Making the Button

IMG_1408.JPEG
topcap1.PNG
botcap1.PNG
ring pic.PNG
button.PNG
IMG_1330.JPEG

I used Fusion 360 to CAD the button into three parts: the main button, the base of the button, and a ring to prevent the button from falling out.

The main part of the button, which is shown in the second image, is a cup-like shape with a small extrusion around the sides. This will act as an overhang, which will be trapped between the base of the button and the ring.

The base of the button, as shown in the third image, is slightly larger than the main part. This part has holes for the vertical extruded part of the main button to slide through, as well as additional holes for the pins of the micro limit switch. It also includes a small cone-like extrusion in the center for the spring.

The last part of the button is the ring, which will be glued on the wall of the base of the button.

Make sure to test if everything will fit by combining them into a single design.

*Note: It is better to have the tolerance between the parts to be slightly larger than too tight.

I used Prusa i3 MK3S+ 3D Printer and Prusa Mini+ to 3d print the parts.

Hot glue the spring and the switch to the bottom of the button. Then test if the button slides well. If it does not, use sandpaper to smoothen the sliding.

Sticker Dispenser

IMG_1413.jpeg
mainbody.PNG
stingray.PNG
lid1.PNG

The sticker dispenser also uses three parts: the main body, the sticker slider, and the sticker cap.

The main body has a box to hold the sticker with a thin slit on one side for the sticker to slide out from. Part of the bottom layer of the box is removed for the sticker slider to slide through and push the bottom sticker out. The bottom layer of the body holds the servo motor required to turn the sticker slider. This part is combined with the sticker holder box to make sure the layer height of the slider is at the right height.

The sticker slider is a semicircle with one half being slightly higher. This creates a wall for the sticker to be pushed out from. This part also has a hole on the bottom to be able to hot glue the servo motor horn to.

The last part is a lid to keep the stickers level to be pushed by the sticker slider through the hole.

Hot glue the sticker slider to the servo horn. Then then attach the servo horn to the servo motor and hot glue it to the bottom layer of the main body.

Circuit

IMG_1331.JPEG
IMG_1410.JPEG
image_2023-05-22_091333483.png
IMG_1416.JPEG
IMG_1417.JPEG

For detecting the button, the pin used are GPIO 16, which is connected to the middle pin on the switch, and Ground, which is connected to the closest pin to the edge on the switch.

For the LED, the pins used are Ground, which runs to the Ground leg of the LED pin, and GPIO 17, which goes through a 330 Ohm resister into the positive side of the LED pin.

For the servo motor, the pins used are Ground, which goes into the brown wire on the servo motor, 5v Power, which goes into the red wire, and GPIO 27, which goes into the yellow wire.

Then, solder the wires together.

Coding

Thanks to BCRobotics and Gary Explains for their useful tutorials, I was able to code the program.


Here is the final code I used for the button:

import RPi.GPIO as GPIO
import time
import smtplib
from gpiozero import Servo
from gpiozero.pins.pigpio import PiGPIOFactory
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Removes warnings
GPIO.setwarnings(False)

# Pin Numbers
buttonPin = 36
ledPin = 11
servoPin = 27

# Pin numbering system
GPIO.setmode(GPIO.BOARD)

# Pin setup
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(ledPin, GPIO.OUT)
factory = PiGPIOFactory()
servo = Servo(servoPin, pin_factory=factory)

# Create variable for button
previous_button_state = GPIO.input(buttonPin)

# Addresses for emails
fromaddr = "rewardtimer@gmail.com"
toaddr = "abcdefedwlin@gmail.com"

# Setup email to be set
def send_email(initbody):
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "AHHHH"
body = initbody
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "mnaq sznz xbbu dknh")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)

# Time (in seconds) for alarm and reward
freeTime = 1800
rewardTime = 300
annoy = False

while True:
# Checks if button changed
button_state = GPIO.input(buttonPin)
if button_state != previous_button_state:
previous_button_state = button_state

# Checks if button is pressed
if button_state == GPIO.HIGH:

# Signals an LED
GPIO.output(ledPin, GPIO.HIGH)
time.sleep(0.5)
GPIO.output(ledPin, GPIO.LOW)

# Sends email
time.sleep(freeTime-3)
send_email("Get Back to Work!")
time.sleep(3)

# Loops to check if button in pressed within reward time available to claim
i = rewardTime * 100
while i > 0:

# Turn on LED
GPIO.output(ledPin, GPIO.HIGH)
button_state = GPIO.input(buttonPin)
time.sleep(0.01)
annoy = True

# Sticker Dispense
if button_state == 0:
GPIO.output(ledPin, GPIO.LOW)
i = 0
servo.min()
time.sleep(1)
servo.max()
time.sleep(1)
servo.min()
time.sleep(1)
servo.value = None;
annoy = False
i -= 1

# Spam emails if button not pressed again
while annoy:
send_email("Come Back!!!")
button_state = GPIO.input(buttonPin)
if button_state == 0:
GPIO.output(ledPin, GPIO.LOW)
annoy = False
time.sleep(1)

Creating the Box

makercase.PNG
box ai.PNG

Using MakerCase to create a box for the entire circuits, I created the box to make the casing for the circuits with additional holes for the Raspberry Pi wires, the button, and the sticker dispenser slit.

I cut one side out of acrylic, but this part is optional.

Positioning Support

IMG_1409.JPEG
positional support.PNG

Sometimes, the servo motor may rotate back too far, which prevents the next sticker from being pushed out. To solve this problem, I created a positional support to stop it from rotating too much. In addition, this support aligns the sticker dispenser body with the hole in the box.

Glue the support down to the base of the box.

Assembly

IMG_1423.JPEG
IMG_1422.JPEG

Attach Velcro to the bottom of the button and the top of the box.

Glue the LED into the small circular hole slot on the top.

Glue the box together, while leaving one side unglued to take out the sticker dispenser for refills.

Enjoy.