cattledog,
Well I thought I would add 2 switches to toggle the code for rising or falling pulse timing. Cannot get it to compile, Thoughts please??
// revision 7 10/19/2016 ttesty for code choice bu button push
// CDI Tester Pulse Generator Serial Output Arduino Code
// Advance is 12° at idle and 29° at over xxxx RPM
int pot1 = A3; // select the input pin for the pot (changed for this version because of lcd)
int potValue = 0; // variable to store the value coming from the sensor
int pulseValue = 0; // 2 variables from one pot
int timerTopValue = 0; // changed from timerTopValue
int outputPin = 8; // select the pin for the output for trigger pulse
int chargePin = 7; // select the pin for the output for charge pulses
volatile boolean trigger = false;
volatile unsigned long delayPeriod;
unsigned long copy_delayPeriod;
volatile unsigned long delayPeriodStart;
float delayDegrees; // changed from int for decimal place display
int RPM; //new
char risefall; //assign char name to be used for display of code choice********
volatile boolean interruptFlag;
unsigned long analogReadInterval = 1000; //read pots and map
unsigned long lastAnalogRead;
byte setupAswitch = 5;// define pins to be used //input pins for code choice************
byte setupBswitch = 6;
byte mode; // 0 = waiting to start, 1 = setupA, 2 = setupB
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// set the LCD address to 0x3f for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
void setup() {
Serial.begin(9600);
lcd.begin(20, 4); // initialize the lcd for 20 chars 4 lines, turn on backlight
lcd.setCursor(1, 0); // lcd display setup of unchanging headings
lcd.print("RPM:"); // print fixed characters
lcd.setCursor(11, 0);
lcd.print("Mode:"); //to display code choice R or F
lcd.setCursor(1, 1);
lcd.print("Ms:");
lcd.setCursor(1, 2);
lcd.print("Us Delay:");
lcd.setCursor(1, 3);
lcd.print("Deg Advance:");
pinMode(outputPin, OUTPUT); // declare the outputPin as an OUTPUT
pinMode(chargePin, OUTPUT); // declare the chargePin as an OUTPUT
delay (2000);
pinMode (setupAswitch, INPUT);
digitalWrite (setupAswitch, HIGH); // turn on internl pullup - check for a LOW input to indicate switch is closed to ground
pinMode (setupBswitch, INPUT);
digitalWrite (setupBswitch, HIGH);
//Timer1 set up 1ms pulse every 50 ms
TCCR1A = 0;;
TCCR1B = (1 << WGM12);//CTC mode to OCR1A
OCR1A = 12500; //50 ms to top value
TIMSK1 |= (1 << OCIE1A);//interrupt enable compareA
TIMSK1 |= (1 << OCIE1B);//interrupt enable compareB
OCR1B = 125; //sets pulse out width, 500 = 2Ms, 250 = 1Ms, 125 = .5Ms
TCCR1B |= (1 << CS11) | (1 << CS10); //prescaler 64 4us/tick
attachInterrupt(digitalPinToInterrupt(3), delayPeriodTiming, RISING);
}
void loop()
{
{
if (millis() - lastAnalogRead >= analogReadInterval)
{
lastAnalogRead += analogReadInterval;
potValue = analogRead(pot1);
pulseValue = analogRead(pot1);
RPM = map(potValue, 0, 1023, 500, 3800);
pulseValue = map(pulseValue, 0, 1023, 120, 16); // to show ms between pulse out on lcd
timerTopValue = 15000000UL/RPM;
if (mode == 0 && digitalRead(setupAswitch) == LOW){ mode = 1;} //read if switchs pushed*************
if (mode == 0 && digitalRead(setupBswitch) == LOW){ mode = 2;}
switch (mode)
case 0:
// do nothing
break;
lcd.setCursor(6, 0);
lcd.print(" "); // print blank spaces to clear old data
lcd.setCursor(6, 0);
lcd.print(RPM);
lcd.setCursor(17, 0);
lcd.print(" ");
lcd.setCursor(17, 0);
//lcd.print(risefall); //print R or F at upper right side
lcd.setCursor(5, 1);
lcd.print(" ");
lcd.setCursor(5, 1);
lcd.print(pulseValue);
lcd.setCursor(11, 2);
lcd.print(" ");
lcd.setCursor(11, 2);
lcd.print(copy_delayPeriod);
lcd.setCursor(14, 3);
lcd.print(" ");
lcd.setCursor(14, 3);
lcd.print(delayDegrees,1);
}
if (trigger == true && interruptFlag == true )
{
trigger = false;
interruptFlag = false;
noInterrupts();
copy_delayPeriod = delayPeriod;
interrupts();
delayDegrees = 360.0 * (copy_delayPeriod) / (timerTopValue * 4.0); // for decimal place in deg display
//copy_delayPeriod = copy_delayPeriod/1000; //value for display to 3 places in us
}
}
ISR(TIMER1_COMPA_vect) {
OCR1A = (timerTopValue); // value to set delay between pulses to trigger cdi
digitalWrite(outputPin, HIGH); //turn on pin 8
digitalWrite(chargePin, HIGH); //turn on pin 7
switch (mode) { // choose code************
case 1:
// A setup code
delayPeriodStart = micros(); //start looking for response as pulse rises
trigger = true;
//risefall = R
mode = 0;
break;
}
}
ISR(TIMER1_COMPB_vect) {
digitalWrite(outputPin, LOW); //turn off pin 8
digitalWrite(chargePin, LOW); //turn off pin 7
switch (mode) { //or choose this code*************
case 2:
// B setup code
delayPeriodStart = micros(); //start looking for response as pulse falls
trigger = true;
//risefall = F
mode = 0;
break;
}
}
}
void delayPeriodTiming()
{
delayPeriod = micros() - delayPeriodStart;
interruptFlag = true;
}
Tom