Okay so I am working on this laser harp project right now. Essential there is a laser that shines on a photocell and when the laser light is blocked by a hand, a note will play on a virtual musical keyboard.
I have it setup right now and it is working however I just need to tweak it so it will be more musical sounding. When the laser is blocked it will repeatability hit the key until my hand is removed from the laser. Is there a way to have it stay down until my hand is removed so that the notes will sound more fluid?
Also note I am using AAC keys for this project.
const int LaserC=3;
const int CellC=A0;
int chqC=0;
void setup() {
Serial.begin(9600);
pinMode(LaserC,OUTPUT);
}
void loop() {
digitalWrite(LaserC,HIGH);
chqC= analogRead(CellC);
if (chqC<830){ //min value to trigger the photocell
Serial.println("hold,A");
}
}
read(button)
if (button != oldbutton)
{
do stuff
oldbutton = button
}
The problem is that the value is constantly changing. When the laser is on it the analogRead value hovers between 830-1000 depending on how much of the laser hits the photocell. Could I get that to work even if the values will be changing constantly?
Please try and bear with me. I am new to programming as a whole.
The problem is that the value is constantly changing.
You need to define a threshold. Above that triggers an action. Below that, nothing happens. Then, the new vs. old is a matter of "on this pass, am I above the threshold but was not last time, or am I below the threshold, but was not last time".
The problem is that the value is constantly changing.
You need to define a threshold. Above that triggers an action. Below that, nothing happens. Then, the new vs. old is a matter of "on this pass, am I above the threshold but was not last time, or am I below the threshold, but was not last time".
I apologize if I am totally missing where you guys are coming from but I tried doing this and now have it doing almost the exact same thing.
const int LaserC=2; //laser pin
const int CellC=A0; //photocell
int thres=830; //min value when laser is on
int chqC=0;
void setup() {
Serial.begin(9600);
pinMode(LaserC,OUTPUT);
}
void loop() {
digitalWrite(LaserC,HIGH);
chqC= analogRead(CellC);
if (chqC<thres){
Serial.print("a");
thres=chqC;
}
else if (chqC>thres) {
}
}
It is still pressing the "a" key repetitively and it's almost too quick for the keyboard to register.
Is it possible I should approach this differently and use something other than aac keys or Serial.print for that matter?
I'm essential building a glorified Arduino keyboard or game controller.
chqC= analogRead(CellC);
if (chqC<thres){
Serial.print("a");
thres=chqC;
}
else if (chqC>thres) {
}
Now, you have a threshold. What you don't have is any record of the previous reading's position relative to the threshold. Re-read what I said. You want to output a value if the previous reading was not above the threshold and the current one is. You MUST keep track of the previous reading.
if (chqC<thres){
Serial.print("a");
thres=chqC;
}
else if (chqC>thres) {
}
Now, you have a threshold. What you don't have is any record of the previous reading's position relative to the threshold. Re-read what I said. You want to output a value if the previous reading was not above the threshold and the current one is. You MUST keep track of the previous reading.
Okay so I have been trying to rationalize what you have been saying and I gave it a shot. I am totally missing the boat on something. I tried to explain what I was thinking.
chqC= analogRead(CellC);
if (chqC<thres){
store=HIGH; //store previous value as high if it is less than the threshold
}
else if (chqC>=thres) {
store=LOW; //store previous value as low if it is greater than the threshold
}
if ((store=HIGH) && (chqC>=thres)){ //if the stored value is high and the current value is grater than the threshold then print "a"
Serial.print("a");
}
}{/code]
Is this not working because the store value is getting overwritten before
if ((store=HIGH) && (chqC>=thres)){ //if the stored value is high and the current value is grater than the threshold then print "a"
Assigning a value to HIGH in the if test is probably not what you want to be doing.
I tried assigning other values. Neither seemed to do the trick. The high and low thing was just something I tried before posting when I was testing a bunch of possible solutions.
FullyJosh:
I can't believe I made that mistake! Sadly it didn't fix the problem. I just tested it again now.
I think you have fixed that problem, but presumably it's not the only problem in your code. It's not at all unusual to have multiple bugs, and you will need to locate and fix each of them before everything works. To start with, now that you know you've made this mistake in one place, I suggest you carefully review the rest of your code for any other mistakes where you used an assignment instead of a condition, or vice versa.
Your code would be a lot easier to see the structure of if every { was on its own line, and you used Tppls + Auto Format to fix the horrid indenting. Do that, and fix the other mistakes that have been pointed out, and post your code again.
const int threshold = 830;
bool cellC_active = false;
void setup()
{
Serial.begin(9600);
pinMode(LaserC,OUTPUT);
digitalWrite(laserC, OUTPUT);
}
void loop()
{
bool cellC_wasActive = cellC_active; // remember whether the cell was already active
cellC_active = (analogRead(CellC) >= threshold); // update the active/inactive state
if (cellC_active && !cellC_wasActive) // if the state has changed from inactive to active...
{
Serial.print("a"); //... then log to serial monitor
}
}
Then read about arrays to find out how to generalise it to multiple strings.
void loop()
{
bool cellC_wasActive = cellC_active; // remember whether the cell was already active
cellC_active = (analogRead(CellC) >= threshold); // update the active/inactive state
if (cellC_active && !cellC_wasActive) // if the state has changed from inactive to active...
{
Serial.print("a"); //... then log to serial monitor
}
}
Then read about arrays to find out how to generalise it to multiple strings.
Your code works MUCH better than mine and works the way I want when in the serial monitor however it is still pressing the key too fast and doesn't press the note. Is there any way I could possibly tweek this to work a little bit better?
Edit:
Okay so I have been testing out the code dc42 posted and I am noticing that when I am in the serial monitor everything is working as needed, however when I run aac keys I notice that sometimes when I go to cover the laser, that the actual laser cuts out and will blink at random on it's own. I noticed the laser do this with other codes but I figured it might have had something to do with my code being 'wonky'.
Please explain what you mean by "pressing the key too fast" and "doesn't press the note". If you have added code to play tones, post the code as you now have it.
dc42:
Please explain what you mean by "pressing the key too fast" and "doesn't press the note". If you have added code to play tones, post the code as you now have it.
I know the code is working when I test it in the serial monitor. However when I go to test it with the virtual keyboard most times a note is not being played. When it does play the note it is very quick and easily missed.
PaulS:
if (chqC<thres)
{
store=HIGH;
}
else if (chqC>=thres)
If chgC is not less than thres, is there any possible way for it not to be greater than or equal to thres?
You are correct, any time chqC is not less than thres, it is automatically assumed that it will be greater than or equal to the thres. I have that line so that the store value can get overwritten.