极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3915|回复: 1

关于火花机程序的改动

[复制链接]
发表于 2014-6-30 13:45:45 | 显示全部楼层 |阅读模式
   我下了个火花机程序,是以前用的现在1.56的IDE ,但无法上传,谁可以帮忙改下,谢谢!!!
//Sparking machine
//Arduino Duemilanove + LCD Keyboard shield
//A5 -- Auto/Manual Mode, Up Limit and Low Limit (A1 pin is used as digital pin)
//A4 -- Motor Speed Adjustment
//A2 -- Low Threshold voltage
//A3 -- High Threshold voltage
//A1 -- Voltage from Spark generator through the voltage divider (5V max input)
//D11 - Motor speed control(PWM pin; contect to EA pin of the motor control board)
//D12 - Motor direction control (contect to I1 pin of the motor control board)
//D13 - Motor direction control (contect to I2 pin of the motor control board)
//D3 -- Motor up in Manual Mode
//D2 -- Motor down in Manual Mode

#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);
//LCD related
int A_spark_pin = 1;    //Spark voltage input pin
int A_speed_pin = 4;    //Motor speed adjustment input pin
int A_low_pin   = 2;    //Low threshold voltage input pin
int A_high_pin  = 3;    //High threshold voltage input pin
float A_spark_val = 0;   //Spark voltage input
float A_speed_val = 0;   //Motor speed adjustment
float A_low_val   = 0;   //Low threshold voltage
float A_high_val  = 0;   //High threshold voltage
float t=0;
int A_speed_val_int = 0;
//
//Motor related
int pinI1=12;//定义I1接口. D12
int pinI2=13;//定义I2接口. D13
int speedpin=11;//定义EA(PWM调速)接口. D11
//
//Auto/Manual Mode related
int mode_pin=19;      //Define Pin for Auto/Manual, Up Limit and Low Limit. Use A5 as D19
int manual_up=3;  //Define Manual mode motor up. D2
int manual_dn=2;  //Define Manual mode motor down. D3
///////

void setup()
{
//Serial.begin(9600); //debugging ///////////////
//LCD related
lcd.init();
lcd.clear();
//
//Motor related
pinMode(pinI1,OUTPUT);//定义该接口为输出接口
pinMode(pinI2,OUTPUT);
pinMode(speedpin,OUTPUT);
//
//Auto/Manual Mode related
pinMode(mode_pin,INPUT);
pinMode(manual_up,INPUT);
pinMode(manual_dn,INPUT);
//////
}


///////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
lcd.cursorTo(1, 0);  //line=1, x=0
lcd.printIn("Sk:");
A_spark_val = analogRead(A_spark_pin)/18.29;    // read the value from the spark machine and convert it to real spark voltage
display1(A_spark_val,0x03);
//////
lcd.cursorTo(1, 9);   
lcd.printIn("Sp:");
t=1024.0/(200-1);
A_speed_val = analogRead(A_speed_pin)/t +1+0.5; // read the Speed; 1 - 200  
A_speed_val_int = A_speed_val ;         //Convert into integer
display2(A_speed_val,0x0c);
//////
lcd.cursorTo(2, 0);  //line=2, x=0
lcd.printIn("L:");
t=1024.0/(4-0.8);
A_low_val = analogRead(A_low_pin)/t + 0.8+0.05;    // read the Low threshold; 0.8v - 4v  
display3(A_low_val,0x2a);
if (A_low_val > A_spark_val)
   {
    lcd.cursorTo(2, 6);   
    lcd.printIn("#");
   }
else
   {
    lcd.cursorTo(2, 6);  
    lcd.printIn("-");
   }  
//////
lcd.cursorTo(2, 8);   
lcd.printIn("H:");
t=1024.0/(20-6);
A_high_val = analogRead(A_high_pin)/t + 6+0.05;    // read the High threshold; 6v - 20v
display4(A_high_val,0x4a);
if (A_high_val > A_spark_val)
   {
    lcd.cursorTo(2, 15);   
    lcd.printIn("#");
   }
else
   {
    lcd.cursorTo(2, 15);  
    lcd.printIn("-");
   }
//////
//////

if (!digitalRead(mode_pin))         //Check whether it is Auto mode or (Manual mode, Up Limit and Low Limit)
   {                                 //Auto mode
    if (A_spark_val > A_high_val)    //Spark voltage > High Threshold
      {
       analogWrite(speedpin,220);    //High speed down
       digitalWrite(pinI1,HIGH);
       digitalWrite(pinI2,LOW);
       delay(50);
       digitalWrite(pinI1,LOW);
       digitalWrite(pinI2,LOW);
       delay(50);
      }
    else
      {
       if (A_spark_val > A_low_val)      //Low Threshold < Spark voltage < High Treshold
         {
          analogWrite(speedpin,180);    //High speed down
          digitalWrite(pinI1,HIGH);
          digitalWrite(pinI2,LOW);
          delay(50);
          digitalWrite(pinI1,LOW);
          digitalWrite(pinI2,LOW);
          delay(A_speed_val_int);
         }
       else                             //Spark voltage < Low Threshold
         {
          analogWrite(speedpin,255);    //Quick up 0.5 sec
          digitalWrite(pinI1,LOW);
          digitalWrite(pinI2,HIGH);
          delay(50);
         }  
      } //if (A_spark_val > A_high_val)
   }
else //If (!digitalRead(mode_pin))
   {                                 //Manual mode, Up Limit or Low Limit
    if (digitalRead(manual_up))      //Manual up
      {
       analogWrite(speedpin,255);    //Max speed
       digitalWrite(pinI1,LOW);
       digitalWrite(pinI2,HIGH);
      }
    else                        
      {
       if (digitalRead(manual_dn))   //Manual down
         {
          analogWrite(speedpin,255); //Max speed
          digitalWrite(pinI1,HIGH);
          digitalWrite(pinI2,LOW);
         }
       else
         {                            //Manual stop  
          digitalWrite(pinI1,LOW);
          digitalWrite(pinI2,LOW);
         }
      }//if (digitalRead(manual_up))      
   }//If (digitalRead(mode_pin))
}//loop
/////////////////////////////////////////////////////////////////////////////////////////////


void display1(float number,unsigned char address)//显示数字 XX.Xv
{
  unsigned char c,d,e;
  int n;
  n=number*10;   //avoid the unit digit becoming 0.
  c=n/100;
  d=(n/10)%10;
  e=n%10;
  lcd.commandWrite(0x80+address);
  if (c==0)
    {
     lcd.print(32);
    }
  else
    {
     lcd.print(c+48);
    }
  lcd.print(d+48);
  lcd.print(46); //"."的ASCII
  lcd.print(e+48);
  lcd.print(118);//"v"的ASCII
}

void display2(float number,unsigned char address)//显示数字 XXX
{
  unsigned char c,d,e;
  int n;
  n=number;
  c=n/100;
  d=(n/10)%10;
  e=n%10;
  lcd.commandWrite(0x80+address);
  if (c==0)
    {
     lcd.print(32);
    }
  else
    {
     lcd.print(c+48);
    }
  lcd.print(d+48);
  lcd.print(e+48);
}

void display3(float number,unsigned char address)//显示数字 X.Xv
{
  unsigned char d,e;
  int n;
  n=number*10;      //avoid the unit digit becoming 0.
  d=n/10;
  e=n%10;
  lcd.commandWrite(0x80+address);
  lcd.print(d+48);
  lcd.print(46); //"."的ASCII
  lcd.print(e+48);
  lcd.print(118);//"v"的ASCII
}

void display4(float number,unsigned char address)//显示数字 XX.Xv
{
  unsigned char c,d,e;
  int n;
  n=number*10;      //avoid the unit digit becoming 0.
  c=n/100;
  d=(n/10)%10;
  e=n%10;
  lcd.commandWrite(0x80+address);
  if (c==0)
    {
     lcd.print(32);
    }
  else
    {
     lcd.print(c+48);
    }
  lcd.print(d+48);
  lcd.print(46); //"."的ASCII
  lcd.print(e+48);
  lcd.print(118);//"v"的ASCII
}
回复

使用道具 举报

 楼主| 发表于 2014-6-30 13:48:59 | 显示全部楼层
在此感谢原做者的共享,谢谢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 16:18 , Processed in 0.111790 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表