We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello world, for my bachelor project I am busy with an audio installation, the interface is done in processing (surprise) and arduino. Enclosed you will find three pieces of code all working well on their own.
The first part consists of two codes one is arduino and one is processing. Function: light on -the sensor catches a person comming into the room-> light out-> music on-> light on-> end/loop
The Arduinocode:
int URECHO = 3; // PWM Output 0-25000US,Every 50US represent 1cm
int URTRIG = 5; // PWM trigger pin
int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
unsigned int distanceMeasured = 0;
boolean activity;
int led = 13;
void setup() {
Serial.begin(9600); // Sets the baud rate to 9600
pinMode(URTRIG, OUTPUT); // A low pull on pin COMP/TRIG
digitalWrite(URTRIG, HIGH); // Set to HIGH
pinMode(URECHO, INPUT); // Sending Enable PWM mode command
delay(500);
Serial.println("Init the sensor");
pinMode(led, OUTPUT);
}
void loop() {
PWM_Mode();
String line = Serial.readStringUntil('\r');
Serial.println(line);
if (line.equals("on")) {
digitalWrite(led, LOW);
}
else if (line.equals("off")) {
digitalWrite(led, HIGH);
}
//delay(100);
// if (activity) {
// digitalWrite(led, HIGH);
// }
// else {
// digitalWrite(led, LOW);
// }
}
void PWM_Mode() // a low pull on pin COMP/TRIG triggering a sensor reading
{
digitalWrite(URTRIG, LOW);
digitalWrite(URTRIG, HIGH); // reading Pin PWM will output pulses
unsigned long LowLevelTime = pulseIn(URECHO, LOW) ;
if (LowLevelTime >= 45000) // the reading is invalid.
{
Serial.print("Invalid");
}
else {
Serial.print("Distance Measured=");
distanceMeasured = LowLevelTime / 50; // every 50us low level stands for 1cm
Serial.print(distanceMeasured);
Serial.println();
if (distanceMeasured > 20) {
activity = false;
}
else {
activity = true;
}
}
}
The Processing code:
import processing.serial.*;
import processing.sound.*;
int S1_WAITING_FOR_PEOPLE = 1;
int S2_PEOPLE_ENTERED = 2;
int S3_LIGHT_FADE_OUT = 3;
int S4_DARK = 4;
int S5_MUSIC = 5;
int S6_LIGHT_FADE_IN = 6;
// make sure this is the last!!!!
int TERMINATE = 7;
int mode = S1_WAITING_FOR_PEOPLE;
int no_one_when_distance_greater_then = 160; // cm
int time_before_dim = 5000; // ms
SoundFile file;
boolean is_playing = false;
Serial myPort;
int dist;
boolean someone_present = false;
int someone_present_since_time;
int music_playing_since;
int music_duration;
int in_mode_since_ms;
void setup() {
size(640, 360);
printArray(Serial.list()); // "/dev/cu.usbmodem1411" cu.usb!!!
myPort = new Serial(this, Serial.list()[1], 9600);
//myPort.readStringUntil('\n');
myPort.bufferUntil('\n');
// Load a soundfile from the data folder of the sketch and play it back in a loop
file = new SoundFile(this, "Ex.wav");
//file.loop();
music_duration = (int) file.duration() * 1000;
music_duration += 2000;
}
void draw() {
background(0);
fill(255);
text("frameCount: "+frameCount, 50, 25);
text("dist: "+dist, 50, 50);
text("mode: "+mode, 50, 75);
if (frameCount == 180) {
turn_on();
} else if (frameCount > 180) {
if ( mode == S1_WAITING_FOR_PEOPLE) {
if (someone_present == false) {
if (dist < no_one_when_distance_greater_then) {
someone_present = true;
someone_present_since_time = millis();
change_mode(mode + 1);
}
}
} else if (mode == S2_PEOPLE_ENTERED) {
text("millis: "+millis(), 50, 100);
text("someone_present_since_time: "+someone_present_since_time, 50, 125);
text("time_before_dim: "+time_before_dim, 50, 150);
if (millis() - someone_present_since_time > time_before_dim) {
change_mode(mode + 1);
}
} else if (mode == S3_LIGHT_FADE_OUT) {
turn_off();
change_mode(mode + 1);
} else if (mode == S4_DARK)
{
change_mode(mode + 1);
} else if (mode == S5_MUSIC)
{
if (is_playing == false) {
file.play();
is_playing = true;
music_playing_since = millis();
}
if (millis() - music_playing_since > music_duration) {
is_playing = false;
file.stop();
change_mode(mode + 1);
}
} else if (mode == S6_LIGHT_FADE_IN)
{
// todo
if (millis() - in_mode_since_ms > 1000) {
change_mode(mode + 1);
}
} else if (mode == TERMINATE)
{
change_mode(S1_WAITING_FOR_PEOPLE);
someone_present = false; // reset
println("turn_on();");
turn_on();
}
}
}
void change_mode(int m) {
mode = m;
in_mode_since_ms = millis();
}
void serialEvent(Serial p) {
String s = p.readString();
if (s != null) {
s = s.replace("\r\n", "");
if (s.contains("Distance Measured")) {
String[] tokens = split(s, "=");
dist = int(tokens[1]);
}
}
}
void turn_on() {
myPort.write("on");
}
void turn_off() {
myPort.write("off");
}
void keyPressed() {
if (key == 'a') {
turn_on();
}
if (key == 's') {
turn_off();
}
}
The second part is an arduino code for dimming an LED light:
const int LEDPin = 9; // the pin that the LED is attached to
const int LEDValueMax = 255; // the maximum value of the LED Brightness
const int LEDValueMin = 0; // the minimum value of the LED Brightness
int LEDValue = 255; // the start value of the LED Brightness
int speedValue = 10; // the dimming speed value
void setup() {
pinMode(LEDPin, OUTPUT); // the PWM Output
}
void loop() {
do // Decrease the LED Brightness untill it reaches minimum brightness
{
analogWrite(LEDPin, LEDValue);
delay(speedValue);
LEDValue = LEDValue - 1;
} while (LEDValue > LEDValueMin);
}
What I would love to fix is that in the first part where the light is turned off (int S3_LIGHT_FADE_OUT = 3;) it is going to dim the brightness as in the the code above. I am not sure how to get these two programs to talk to each other/ switching from int S3 to arduino dimming code.
Thanks for your efforts! Faffie
Answers
edit post, highlight code, press ctrl-o to format.
Ahh, looks much better! Thanks for the tip!! Do you have any idea about my problem ?
you want to integrate an arduino code into the processing code?
Wouldn't it be better to integrate arduino code into the arduino code?
E.g. into these lines:
or are they handling a different LED?
Chrisir
Hey Chris! Thanks for your mail. You are right I want to implement the arduino into the arduino! Yes, it is the same LED. I also tried to fix these lines, but it did not work properly. What is your propose to solve it?
in these lines, I am not sure why you say LOW when the command is "on" and vice versa:
My Proposal
My Proposal would be to have two distinct states in loop, either
I also got rid off your while loop and instead use the looping of
loop()
itselfnot tested
@Chrisir Thanks for your proposal. I have implement it into my code and wanted to upload it. Unfortunately it was not possible because there is not enough ram space. I have tried to use the Serial.print(F('')), to get rid of the problem, which was not enough. This is the code by now:
Do you have an idea? Thanks again! F
when you say
Does this refer to the number of letters in the code OR to what happens during running the code?
I can't help you with that.
Chrisir
Does the error message you get tell you something about what happens with the RAM? Does this refer to the number of letters in the code OR to what happens during running the code?
I believe your many print statements is loading your RAM. Untested and not an expert, but looking up the F macro potentially leads to this conclusion:
https://www.baldengineer.com/arduino-f-macro.html
My suggestion is for you to drop the print statements, at least for testing purposes, and try a simpler version of your code. After you demonstrate that that solves the problem, then you can go back to the drawing board and star optimizing your RAM usage.
Kf