Quick suggestion on approaching this problem

I'm new to Arduino but old to C programming... could this possibly just be the simple assignment rather than test?

As you're already setting store to HIGH or LOW before the test...

Rather than

if ((store=HIGH) && (chqC>=thres)){

Shouldn't it be?

if ((store==HIGH) && (chqC>=thres)) {

DerekErb:
I'm new to Arduino but old to C programming... could this possibly just be the simple assignment rather than test?

As you're already setting store to HIGH or LOW before the test...

Rather than

if ((store=HIGH) && (chqC>=thres)){

Shouldn't it be?

if ((store==HIGH) && (chqC>=thres)) {

I can't believe I made that mistake! Sadly it didn't fix the problem. I just tested it again now.

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.

Try this:

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.

My current code is this

const int LaserC=2;
const int CellC=A0;

int thres=830;
int chqC=0;
int store=0;

void setup() 
{
  Serial.begin(9600);

  pinMode(LaserC,OUTPUT);


}

void loop() 
{

  digitalWrite(LaserC,HIGH); 


  chqC= analogRead(CellC);

  if (chqC<thres)
  {
    store=HIGH;
  }    

  else if (chqC>=thres) 
  {
    store=LOW;
  }



  if ((store==HIGH) && (chqC>=thres))
  {
    Serial.print("a");
  }
}

dc42:
Try this:

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.

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.

  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?

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.

I have that line so that the store value can get overwritten.

You need the else statement so that the store value can get overwritten, not the else if statement.

PaulS:
You need the else statement so that the store value can get overwritten, not the else if statement.

 else  {
       store=LOW;
         }

Something like that? Just tried running the code again and I'm having any luck.

Just tried running the code again and I'm having any luck.

Luck has nothing do with writing code that works. Debugging does. I don't see any Serial.print() statements in your code that tell you what is happening.

I left them out because I was scared that they would interfere with the AAC keys readings.
I wrote a separate code to test the laser and make sure it is working and I was using the right values.

FullyJosh:

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.

What exactly is the "virtual keyboard", and how have you connected it?

dc42:

FullyJosh:

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.

What exactly is the "virtual keyboard", and how have you connected it?

- YouTube I recorded a quick video to demonstrate the behaviour. Note that I decided to use an L.E.D. now because I thought it may have been some power issues with the actual laser but it appears it doesn't make a difference they are both behaving in the same way.

I am using a program called Mixcraft. I turned on the virtual keyboad in the program which allows me to use my computer keyboard. I don't think it's the virtual keyboard software's problem. I know this only because I also tried opening a word document and noticed that most times "a" doesn't appear, or it appears very delayed and some are dead on.

Anyone else have an idea on why the code would work in the serial monitor but not with aac keys enabled?

What data does Mixcraft expect? How is it supposed to know how long a note is?

dc42:
What data does Mixcraft expect? How is it supposed to know how long a note is?

Mixcraft will turn my computer keyboard into an actual musical keyboard. When I press the "a" key it plays the note and it will hold the note until I release my finger.

That's the problem then. There is no way that a serial port sending ASCII codes can convey information about how long a key was pressed. I think a Leonardo could do it, because I believe it can emulate a USB PC keyboard, for which the up- and down-codes are sent separately.

dc42:
That's the problem then. There is no way that a serial port sending ASCII codes can convey information about how long a key was pressed. I think a Leonardo could do it, because I believe it can emulate a USB PC keyboard, for which the up- and down-codes are sent separately.

Problem with that is that the Leonardo doesn't have enough analog pins.

Do you know how I can fix the issue where your code works flawlessly in the serial monitor but starts glitching when I turn on aac keys?