d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > User Blogs > Personal Arduino Code's
Add Reply New Topic
Member
Posts: 4,905
Joined: Feb 19 2012
Gold: 0.00
Jun 28 2018 03:01pm
Laser Box Build

Code

/// Begin Includes ///

#include <Servo.h>
#include <Wire.h>
#include "rgb_lcd.h"

/// End Includes ///

/// Begin References ///

Servo myservo;
rgb_lcd lcd;

/// End References ///

/// Begin Variables ///

int pos = 0;
int i = 0;
int Counter = 0;
int runTime = 0;
int Speed = 0;
const int colorR = 255;
const int colorG = 69;
const int colorB = 0;

/// End Variables

void setup() {

/// Begin Servo Assignment///
myservo.attach(9); /// Servo PIN
myservo.write(pos);
/// End Servo Assignment ///

/// Begin Laser Assignment ///
pinMode(13, OUTPUT); /// Laser PIN
/// End Laser Assignment ///

/// Begin LCD Assignment ///
lcd.begin(16, 2);
lcd.setRGB(colorR, colorG, colorB);
lcd.setCursor(0, 0);
lcd.print("Laser Box 2018");
lcd.setCursor(0, 1);
lcd.print("Initializing");
/// End LCD Assignment ///

/// Begin Initial Setup ///
for (i = 0; i <= 10; i += 1) {
digitalWrite(13, HIGH);
delay(400);
digitalWrite(13, LOW);
delay(400);
}
lcd.clear();
digitalWrite(13, HIGH);
/// End Initial Setup ///

}

void loop() {

/// Begin First Cycle ///
runTime += 1;
Counter = 0;
Speed = 1;

for (i = 0; i <= 19; i += 1) {
Counter += 1;
lcd.setCursor(0, 0);
lcd.print("Cycle:");
lcd.print(Counter);
lcd.print(" Speed:");
lcd.print(Speed);
lcd.setCursor(0, 1);
lcd.print("Occurences: ");
lcd.print(runTime);
for (pos = 0; pos <= 140; pos += Speed) {
myservo.write(pos);
delay(10);
}

for (pos = 140; pos >= 0; pos -= Speed) {
myservo.write(pos);
delay(10);
}
}

digitalWrite(13, LOW);
myservo.detach();
Counter = 15;
lcd.clear();

for (i = 0; i <= 14; i += 1) {
lcd.clear();
Counter -= 1;
lcd.setCursor(0, 0);
lcd.print("Delay: ");
lcd.print(Counter);
lcd.setCursor(0, 1);
lcd.print("Occurences: ");
lcd.print(runTime);
delay(1000);
}

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Rebooting..");




digitalWrite(13, HIGH);
myservo.attach(9);
lcd.clear();

}
Member
Posts: 4,905
Joined: Feb 19 2012
Gold: 0.00
Jun 28 2018 03:45pm
Modified to add Piezo

Code

/// Begin Includes ///

#include <Servo.h>
#include <Wire.h>

/// End Includes ///

/// Begin References ///

Servo myservo;

/// End References ///

/// Begin Variables ///

int pos = 0;
int i = 0;
int Counter = 0;
int runTime = 0;
int Speed = 0;
int servoPin = 9;
int laserPin = 12;
int buzzerPin = 5;

/// End Variables

void setup() {

/// Begin Servo Assignment///
myservo.attach(servoPin); /// Servo PIN
myservo.write(pos);
/// End Servo Assignment ///

/// Begin Laser Assignment ///
pinMode(laserPin, OUTPUT); /// Laser PIN
/// End Laser Assignment ///

/// Begin Buzzer Assignment ///
pinMode(buzzerPin, OUTPUT);
/// End Buzzer Assignment ///

/// Begin Initial Setup ///
for (i = 0; i <= 10; i += 1) {
tone(buzzerPin, 1000);
digitalWrite(laserPin, HIGH);
delay(400);
noTone(buzzerPin);
digitalWrite(laserPin, LOW);
delay(400);
}
digitalWrite(laserPin, HIGH);
tone(buzzerPin, 2000);
delay(200);
noTone(buzzerPin);
delay(200);
tone(buzzerPin, 2000);
delay(200);
noTone(buzzerPin);
delay(200);
tone(buzzerPin, 2000);
delay(200);
noTone(buzzerPin);
delay(200);
tone(buzzerPin, 2000);
delay(200);
noTone(buzzerPin);

/// End Initial Setup ///

}

void loop() {

/// Begin First Cycle ///
runTime += 1;
Counter = 0;
Speed = 1;

for (i = 0; i <= 19; i += 1) {
Counter += 1;
for (pos = 0; pos <= 140; pos += Speed) {
myservo.write(pos);
delay(10);
}

for (pos = 140; pos >= 0; pos -= Speed) {
myservo.write(pos);
delay(10);
}
}

digitalWrite(laserPin, LOW);
myservo.detach();
Counter = 15;

for (i = 0; i <= 14; i += 1) {
Counter -= 1;
delay(1000);
}

digitalWrite(laserPin, HIGH);
myservo.attach(servoPin);
tone(buzzerPin, 2000);
delay(200);
noTone(buzzerPin);
delay(200);
tone(buzzerPin, 2000);
delay(200);
noTone(buzzerPin);
delay(200);
tone(buzzerPin, 2000);
delay(200);
noTone(buzzerPin);
delay(200);
tone(buzzerPin, 2000);
delay(200);
noTone(buzzerPin);

}
Go Back To User Blogs Topic List
Add Reply New Topic