Project Problem

Hey guys I don't know what's wrong.

I want to run the loop() all the time, when I press a button "CalActive", making pin 7 go high, I want it to loop RPMCal(). Then in RPMCal() I have an if pin 7 low, blink a couple times then return to loop().

Right now It wont run RPMCal() no matter what state pin 7 is( I have a pull down resistor to 7 and button to 5v)

Also If I change: if(CalActive == HIGH){.. to if(CalActive, HIGH){.. to make sure RPMCal was working. It did go to that loop in one cycle which is what should happen. However the 1 sec delay (Blink of LED)looked like .2 sec ...hmm AHHH what im I doing wrong here.

I'm 100% positive it's a code error of mine can anyone help?

Thanks!!

int Led = 10;
int Led2 = 11;
int YIn = A0;
int XIn = A1;
int TrimIn = A3;
int Debounce = 0;
int Rpm = 0;
int RPM = 0;
int TrimVal = 0;
int Cal = 7;
int CalActive = 0;
const int AnalogInPin1 = YIn;
const int analogInPin2 = XIn;
const int analogOutPin1= Led; 
const int analogOutPin2= Led2; 
int sensorValue1 = 0; 
int sensorValue2 = 0;
int outputValue1 = 0; 
int outputValue2 = 0;
int PWR = 6;
int Ind = 13;
void RPM_()
{
  RPM++;
}

void setup() {

Serial.begin(9600);
pinMode(Ind,OUTPUT);
pinMode(Led,OUTPUT);
pinMode(Cal,INPUT);
pinMode(PWR,OUTPUT);
digitalWrite(PWR, HIGH);
}
void loop() {
  CalActive =(digitalRead, Cal);
 int TrimVal =(analogRead, TrimIn);

sensorValue1 = analogRead(YIn);
sensorValue2 = analogRead(XIn);


Serial.print("accelerometer Y = " );
Serial.print(sensorValue1);
Serial.print("\t accelerometer X = " );
Serial.print(sensorValue2);
Serial.print("\t output 1 = ");
Serial.print(outputValue1);
Serial.print("\t output 2 = ");
Serial.println(outputValue2);

int MapX =map(sensorValue2, 470, 490, 0, 1023);

if( MapX >= TrimVal){
  digitalWrite(Led, HIGH);
  delayMicroseconds(100);
  digitalWrite(Led, LOW);
  delay(Debounce);
}

  if(CalActive == HIGH){
    RPMCal();
  }
 
}
void RPMCal(){
  digitalWrite(Ind, HIGH);
  attachInterrupt(0, RPM_, FALLING);
  delay(1000);
  noInterrupts();
  Rpm = (60 * RPM);
  Debounce = (0.6*(60 / Rpm));
  digitalWrite(Ind, LOW);

 if(CalActive, LOW){
  digitalWrite(Ind, LOW);
  delay(50);
  digitalWrite(Ind, HIGH);
  delay(50);
  digitalWrite(Ind, LOW);
  delay(50);
  digitalWrite(Ind, HIGH);
  delay(50);
  digitalWrite(Ind, LOW);
  delay(50);
  digitalWrite(Ind, HIGH);
  delay(50);
  digitalWrite(Ind, LOW);
  loop();
  }
}

Look up how to use the digital read function, you are not calling it correctly.

The same goes for the analog read.

Why are you using interrupts. There is no need and you are not doing it right anyway.

nissan20det:
I want to run the loop() all the time, when I press a button "CalActive", making pin 7 go high, I want it to loop RPMCal(). Then in RPMCal() I have an if pin 7 low, blink a couple times then return to loop().

Rather than have a loop in RPMCal() just use loop() like this

void loop() {
   readButton();
   RPMCal();
}

that way you only need the button reading code in one place. Ensure that the code in RPMCal only takes a few microsecs or millisecs to complete.

Use millis() to manage your timing rather than delay(). The Arduino can do nothing during a delay(). See the demos several things at a time and planning and implementing a program

...R

Okay that doesn't make since to me.... If I put RPMCAL() in loop() it will only run one time if the if button state is true, I want it to repeat over and over till I release the button....Well I guess I can use while(..
and the delay is for debounce is for a reason. I need it to stop... although the led blinking part doesn't but for this it doesn't bother me.. I am one to usually never use the delay() command.

Thanks for the response!

Nope even this wont work????

int Led = 10;
int Led2 = 11;
int YIn = A0;
int XIn = A1;
int TrimIn = A2;
int Debounce = 0;
int Rpm = 0;
int RPM = 0;
int TrimVal = 0;
int Cal = A4;
unsigned CAL;
const int AnalogInPin1 = YIn;
const int analogInPin2 = XIn;
const int analogOutPin1= Led; 
const int analogOutPin2= Led2; 
int sensorValue1 = 0; 
int sensorValue2 = 0;
int outputValue1 = 0; 
int outputValue2 = 0;
int PWR = A5;
int Ind = 13;
void RPM_()
{
  RPM++;
}

void setup() {

Serial.begin(9600);
pinMode(Ind,OUTPUT);
pinMode(Led,OUTPUT);
pinMode(Cal,INPUT);
pinMode(PWR,OUTPUT);
digitalWrite(PWR, HIGH);
}
void loop() {
  CAL =(analogRead, Cal);
 int TrimVal =(analogRead, TrimIn);

sensorValue1 = analogRead(YIn);
sensorValue2 = analogRead(XIn);


Serial.print("accelerometer Y = " );
Serial.print(sensorValue1);
Serial.print("\t accelerometer X = " );
Serial.print(sensorValue2);
Serial.print("\t output 1 = ");
Serial.print(outputValue1);
Serial.print("\t output 2 = ");
Serial.println(outputValue2);

int MapX =map(sensorValue2, 470, 490, 0, 1023);

if( MapX >= TrimVal){
  digitalWrite(Led, HIGH);
  delayMicroseconds(100);
  digitalWrite(Led, LOW);
  delay(Debounce);
}
int On = HIGH;

while(CAL >= 500){
  digitalWrite(Ind, HIGH);
  attachInterrupt(0, RPM_, FALLING);
  delay(1000);
  noInterrupts();
  Rpm = (60 * RPM);
  Debounce = (0.6*(60 / Rpm));
  digitalWrite(Ind, LOW);
  delay(100);
 if(CAL <= 500){
  digitalWrite(Ind, LOW);
  delay(50);
  digitalWrite(Ind, HIGH);
  delay(50);
  digitalWrite(Ind, LOW);
  delay(50);
  digitalWrite(Ind, HIGH);
  delay(50);
  digitalWrite(Ind, LOW);
  delay(50);
  digitalWrite(Ind, HIGH);
  delay(50);
  digitalWrite(Ind, LOW);
  }
 }
}

nissan20det:
Okay that doesn't make since to me.... If I put RPMCAL() in loop() it will only run one time if the if button state is true, I want it to repeat over and over till I release the button.

That depends on how the code is written.

Did you look at the links I posted ?

...R

int TrimVal =(analogRead, TrimIn);

Is a totally rubbish line. What do you think it does?

Is this what you were hoping to write:-

int TrimVal =analogRead(TrimIn);