Skip to content

Commit

Permalink
Things are groovin
Browse files Browse the repository at this point in the history
  • Loading branch information
caseybarajas committed Dec 28, 2023
1 parent 6749e2d commit b353bc1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 69 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"C_Cpp.default.compilerPath": ""
}
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Idk man
33 changes: 19 additions & 14 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
from flask import Flask, request, render_template
from flask import Flask, render_template, jsonify
import serial
import time
import threading

app = Flask(__name__)
arduino = serial.Serial('/dev/ttyACM0', 9600)
arduino = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
arduino.flush()

def send_to_arduino(speed):
print(f"Sending to Arduino: {speed}")
arduino.write(f"{speed}\n".encode())
data = {"counter": 0}

def read_from_arduino():
while True:
if arduino.in_waiting > 0:
line = arduino.readline().decode('utf-8').rstrip()
data["counter"] = line

thread = threading.Thread(target=read_from_arduino)
thread.start()

@app.route('/')
def index():
return render_template('index.html')
return render_template('index.html', data=data)

@app.route('/motor', methods=['POST'])
def motor():
speed = request.form['speed']
print(f"Received speed: {speed}")
send_to_arduino(speed)
return f"Motor set to {speed}"
@app.route('/data')
def data():
return jsonify(data)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
app.run(debug=True, host='0.0.0.0')
22 changes: 11 additions & 11 deletions main.ino
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include <Servo.h>

Servo motor;

void setup() {
// Initialize serial communication at 9600 bits per second:
Serial.begin(9600);
motor.attach(9); // Attach the motor to pin 9
}

void loop() {
if (Serial.available() > 0) {
int speed = Serial.parseInt(); // Read the speed value sent from the Raspberry Pi
if (speed >= 0 && speed <= 180) {
motor.write(speed); // Set the motor speed
}
}
static int counter = 0;

// Send the counter value
Serial.println(counter);

// Increment the counter
counter++;

// Wait for a second
delay(1000);
}
65 changes: 21 additions & 44 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,28 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Motor Control Interface</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
.slider {
width: 300px;
margin: 10px;
}
</style>
</head>
<body>
<h2>Motor Control</h2>
<form id="motorControlForm">
<input type="range" min="0" max="180" value="90" class="slider" id="motorSpeed">
<p>Speed: <span id="speedValue">90</span></p>
<button type="submit">Set Motor Speed</button>
</form>

<title>Arduino Data Display</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
document.getElementById('motorSpeed').oninput = function() {
document.getElementById('speedValue').textContent = this.value;
};

document.getElementById('motorControlForm').onsubmit = function(e) {
e.preventDefault();
var speed = document.getElementById('motorSpeed').value;

fetch('/motor', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'speed=' + speed
})
.then(response => response.text())
.then(data => {
alert(data);
})
.catch(error => {
alert('Error:' + error);
});
};
$(document).ready(function(){
function fetchData() {
$.ajax({
url: '/data',
type: 'GET',
success: function(response) {
$('#counter').text(response.counter);
},
error: function(error) {
console.log(error);
}
});
}
setInterval(fetchData, 1000); // Update every second
});
</script>
</head>
<body>
<h1>Arduino Data</h1>
<p>Counter: <span id="counter">{{ data.counter }}</span></p>
</body>
</html>

0 comments on commit b353bc1

Please sign in to comment.