Library to download from tutorial on Arduino
Newping
Code to show detection of distance on serial monitor
#include <NewPing.h>
NewPing sonar(10, 11, 20);
void setup() {
Serial.begin(9600);
delay(50);
// put your setup code here, to run once
}
void loop() {
Serial.print("The Distance is: ");
Serial.println(sonar.ping_cm());
delay(1000);
}
Ma maaan
Little circuit <3
Connect to blender through blendix code (doesn't work properly)
#include <NewPing.h>
NewPing sonar(10, 11, 20);
float distance = 0; // Global variable to store the distance
void setup() {
Serial.begin(115200); // Initialize serial communication with a baud rate of 115200
delay(50);
}
void loop() {
distance = sonar.ping_cm(); // Measure the distance using the ultrasound sensor
// Check if an object is in front of the sensor within the range
if (distance > 0 && distance < 20) {
sendRandomValues(); // If an object is detected, send random values
}
// Add a delay of 1 second
delay(1000);
}
// Function to generate random meaningful words
String generateRandomText(int length) {
String randomText = "";
// Define an array of meaningful words
String words[] = {"apple", "banana", "cat", "dog", "elephant", "flower", "guitar", "house", "ice cream", "jungle"};
int numWords = sizeof(words) / sizeof(words[0]);
for (int i = 0; i < length; i++) {
int randomIndex = random(0, numWords); // Generate a random index to select a word from the array
String randomWord = words[randomIndex];
randomText += randomWord + " ";
}
randomText.trim(); // Remove the trailing space
return randomText;
}
// Function to generate random axis values
String generateRandomAxis() {
String axis = "";
float x = random(-10, 10); // Generate a random floating-point number between -10 and 10 for x
float y = random(-10, 10); // Generate a random floating-point number between -10 and 10 for y
float z = random(-10, 10); // Generate a random floating-point number between -10 and 10 for z
axis = String(x) + "," + String(y) + "," + String(z); // Concatenate the generated axis values into a single string
return axis;
}
// Function to send random values to Blender
void sendRandomValues() {
String textData = generateRandomText(3); // Generate random meaningful text with a desired length
String movementData = generateRandomAxis(); // Generate random axis values
String sendData = movementData + ";" + textData; // Combine the axis values and text data with a semicolon in between
Serial.println("Distance: " + String(distance) + " cm");
Serial.println("Random Values: " + sendData); // Print the combined data to the serial monitor for communication with Blender
delay(200); // Adjust delay as needed
}
This third code speaks to blender. The ultrasonic sensor when triggered, moves the blender model of 1m on y axis.
#include <NewPing.h>
NewPing sonar(10, 11, 20); // Ultrasonic sensor configuration
float totalDistance = 0.0; // Variable to store the total distance moved
void setup() {
Serial.begin(9600); // Initialize serial communication with a baud rate of 9600
}
// Function to send data to Blender
void sendtoBlender(float x) {
String sendData = String(x) + ",0,0," + String(x) + ",0,0," + String(x) + ",0,0;";
Serial.println(sendData); // Print the combined data to the serial monitor for communication with Blender
}
// Function to generate random meaningful words
String generateRandomText(int length) {
String randomText = "";
// Define an array of meaningful words
String words[] = {"apple", "banana", "cat", "dog", "elephant", "flower", "guitar", "house", "ice cream", "jungle"};
int numWords = sizeof(words) / sizeof(words[0]);
for (int i = 0; i < length; i++) {
int randomIndex = random(0, numWords); // Generate a random index to select a word from the array
String randomWord = words[randomIndex];
randomText += randomWord + " ";
}
randomText.trim(); // Remove the trailing space
return randomText;
}
// Function to generate random axis values
String generateRandomAxis() {
String axis = "";
float x = random(-10, 10); // Generate a random floating-point number between -10 and 10 for x
float y = random(-10, 10); // Generate a random floating-point number between -10 and 10 for y
float z = random(-10, 10); // Generate a random floating-point number between -10 and 10 for z
axis = String(x) + "," + String(y) + "," + String(z); // Concatenate the generated axis values into a single string
return axis;
}
// Function to generate and send fixed data to Blender
void generateAndSendData() {
// Increment the total distance by 1 meter
totalDistance += 1.0;
// Generate the data in the correct format for Blender
sendtoBlender(totalDistance);
}
void loop() {
// Measure the distance using the ultrasonic sensor
float distance = sonar.ping_cm();
// Check if an object is detected within the specified distance range (adjust as needed)
if (distance > 0 && distance < 20) {
// Call the function to generate and send data when an object is detected
generateAndSendData();
delay(2000); // Wait for 2 seconds to avoid rapid multiple triggers (adjust as needed)
}
}
HTML Creator