Go Down

Topic: Quick suggestion on approaching this problem (Read 2 times) previous topic - next topic

FullyJosh


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
Code: [Select]

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


Shouldn't it be?
Code: [Select]

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.

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

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.

dc42

Try this:

Code: [Select]
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.
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

FullyJosh

#14
Jan 01, 2013, 01:05 am Last Edit: Jan 01, 2013, 01:18 am by FullyJosh Reason: 1
My current code is this
Code: [Select]
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");
 }
}




Try this:

Code: [Select]
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'.

Go Up