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'.