Hi there everyone.
Thank you for taking the time to click on my post. I am new to programming and I am a bit struggling with some things I am trying to make my Arduino do. I have both a Uno R3 and Mega 2560 R3.
So I am working on this Metal Sculpture. It would be my sixth creation and I would like to make it tech infused... So it's basically a metal bust that would detect upcoming people. I want it to open it's eyes mechanically and electronically as a person approach and closes as they leave... not on a timer, but by it's distance... i also want an alarm to sound 1m away from it... I want it's eyes to be dim up green as it opens and dims down as it moves away and eyes shuts. I also want red for when it's within 1.5m.
So right now I have figured out the tf-mini and lights per distance. not the dimming part though. I have added a sweep code but now they seem to run at the same time and won't detect disance but servo is random and lights are random.
I will add my code and yeah... it's a mess.
I am willing to take any advice.
/*
Example code for Benewake TFMini time-of-flight distance sensor.
by Peter Jansen (December 11/2017)
This example code is in the public domain.
This example communicates to the TFMini using a SoftwareSerial port at 115200,
while communicating the distance results through the default Arduino hardware
Serial debug port.
SoftwareSerial for some boards can be unreliable at high speeds (such as 115200).
The driver includes some limited error detection and automatic retries, that
means it can generally work with SoftwareSerial on (for example) an UNO without
the end-user noticing many communications glitches, as long as a constant refresh
rate is not required.
The (UNO) circuit:
* Uno RX is digital pin 10 (connect to TX of TF Mini)
* Uno TX is digital pin 11 (connect to RX of TF Mini)
THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <SoftwareSerial.h>
#include "TFMini.h"
#include <Servo.h>
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
// Setup software serial port
SoftwareSerial mySerial(10, 11); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;
int ledR = 13;
int ledY = 12;
int ledG = 8;
Servo myservo; // create servo object to control a servo
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(ledR, OUTPUT);
pinMode(ledY, OUTPUT);
pinMode(ledG, OUTPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// Step 1: Initialize hardware serial port (serial debug port)
Serial.begin(115200);
// wait for serial port to connect. Needed for native USB port only
while (!Serial);
Serial.println ("Initializing...");
// Step 2: Initialize the data rate for the SoftwareSerial port
mySerial.begin(TFMINI_BAUDRATE);
// Step 3: Initialize the TF Mini sensor
tfmini.begin(&mySerial);
}
void loop() {
// Take one TF Mini distance measurement
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();
// Display the measurement
Serial.print(dist);
Serial.print(" cm sigstr: ");
Serial.println(strength);
// Wait some short time before taking the next measurement
delay(25);
if (dist < 450 ){
digitalWrite(ledR, HIGH); // turn the LED on (HIGH is the voltage level)
}
else{
digitalWrite(ledR, LOW); // turn the LED off by making the voltage LOW
}
if (dist < 400 ){
digitalWrite(ledR, HIGH); // turn the LED on (HIGH is the voltage level)
}
else{
digitalWrite(ledR, LOW); // turn the LED off by making the voltage LOW
}
if (dist < 350 ){
digitalWrite(ledR, HIGH); // turn the LED on (HIGH is the voltage level)
}
else{
digitalWrite(ledR, LOW); // turn the LED off by making the voltage LOW
}
if (dist < 300 ){
digitalWrite(ledR, HIGH); // turn the LED on (HIGH is the voltage level)
}
else{
digitalWrite(ledR, LOW); // turn the LED off by making the voltage LOW
}
if (dist < 250 ){
digitalWrite(ledR, HIGH); // turn the LED on (HIGH is the voltage level)
}
else{
digitalWrite(ledR, LOW); // turn the LED off by making the voltage LOW
}
if (dist < 200 ){
digitalWrite(ledR, HIGH); // turn the LED on (HIGH is the voltage level)
}
else{
digitalWrite(ledR, LOW); // turn the LED off by making the voltage LOW
}
if (dist < 150 ){
digitalWrite(ledR, HIGH); // turn the LED on (HIGH is the voltage level)
}
else{
digitalWrite(ledR, LOW); // turn the LED off by making the voltage LOW
}
if (dist < 100 ){
digitalWrite(ledR, HIGH); // turn the LED on (HIGH is the voltage level)
tone(7, 1140, 200);
}
if (dist > 450){
digitalWrite(ledG, HIGH);
}
if (dist > 100) {
for (pos = 0; pos <= 115; pos += 1) { // goes from 0 degrees to 180 degrees
}
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 115; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
else
}


