|
楼主 |
发表于 2019-12-29 20:03:11
|
显示全部楼层
代码:
- // Arduino Battery Spot Welder蓄电池点焊机
- // Project Page: http://www.instructles.com/id/DIY-Arduino-Battery-Spot-Welder/
- int max_pulse_time = 20; // Sets the maximum pulse time to 20mS (do not use higher than 99 because the display only works up to 99)将最大脉冲时间设置为20MS(不使用高于99,因为显示器只工作到99)
- int mosfetPin = 5; // Pin that controls the mosfet drivers 控制mosfet的引脚
- int ledPin = 4; // Pin to control the status led 控制状态led的引脚
- int switchPin = 2; // Pin to trigger a pulse with the foot switch用脚踏开关触发脉冲的引脚
- int sensorPin = A7; // Pin where the potentiometer value is read读取电位计值的引脚
- int previous_pulse_time; // Previous pulse time so display can skip to ones digit以前的脉冲时间显示可以跳过的数字
- int pulse_time; // Variable to store the pulse time 存储脉冲时间的变量
- int pulse_short; // Variable to store the short pulse time 存储短脉冲时间的变量
- int displayStep = 0; // The step the display function should complete next 下一步显示功能应完成的步骤
- unsigned long previousMillis = millis(); // The milliseconds where the timer starts counting from 计时器开始计数的毫秒数
- int tens = 0; // Tens place digit 十位数字
- int ones = 0; // Ones place digit 个位数字
- float pulse_calc = 1023 / max_pulse_time;
- // Defines the possible characters to be displayed on the 7-segment display in an array 定义数组中7段显示器上可能显示的字符
- // 0 1 2 3 4 5 6 7 8 9 . None All
- byte y[13] = {B11111100, B01100000, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110, B00000001, B00000000, B11111111}; // Ones
- byte z[13] = {B11111100, B00001100, B11011010, B11110010, B01100110, B10110110, B10111110, B11100000, B11111110, B11110110, B00000001, B00000000, B11111111}; // Tens
- //-------------------------------------------------------------------------------------------
- void setup() {
- pinMode(switchPin, INPUT); // Set switchPin as Input 将开关脚设置为输入
- pinMode(mosfetPin, OUTPUT); // Set mosfetPin as Output 将mosfet脚设置为输出
- pinMode(ledPin, OUTPUT); // Set ledPin as Output将led引脚设置为输出
- pinMode(sensorPin, INPUT); // Set sensorPin as Input 将传感器引脚设置为输入
- // Set up 7 Segment Pins设置7段引脚
- for (int i = 6; i <= 13; i++) // Pins 6-13 are set as Outputs脚6-13设置为输出
- pinMode(i, OUTPUT);
- // Light up the led and the display 2 seconds to verify correct placement and function点亮led和显示屏2秒钟,以验证正确的位置和功能
- digitalWrite(ledPin, HIGH);
- segments(z[12]);
- delay(2000);
- digitalWrite(ledPin, LOW);
- segments(z[11]);
- }
- //-------------------------------------------------------------------------------------------
- void loop() {
- // Read pulse time from analog input从模拟输入读取脉冲时间
- read_pulse_time();
- // Display time on 7-segment display在7段显示器显示时间
- time_display();
- // Impulse activated by switch脉冲激活开关
- if (digitalRead(switchPin) == HIGH && pulse_time > 0) {
- double_pulse();
- // Wait until switch has been released等到开关已经发布
- do {
- digitalWrite(mosfetPin, LOW);
- digitalWrite(ledPin, LOW);
- } while (digitalRead(switchPin) == HIGH);
- // Delay before impulse can be triggered again再次延迟脉冲触发之前
- delay(500);
- }
- else
- {
- digitalWrite(mosfetPin, LOW);
- digitalWrite(ledPin, LOW);
- }
- }
- //-------------------------------------------------------------------------------------------
- void segments(byte n) {
- // controll all 7 Segments控制所有7段
- for (int k = 6; k <= 13; k++) {
- if ((n & B10000000) > 0)
- digitalWrite(k, HIGH);
- else
- digitalWrite(k, LOW);
- n = n << 1;
- }
- }
- //-------------------------------------------------------------------------------------------
- void read_pulse_time() {
- // Read the potentiometer and shift the value to correct the midpoint读取电位计并移动该值以校正中点
- // This allows 20 to be selected without the value having to be exactly 1023 这允许选择20,而不必精确地选择1023
- int sensorValue = analogRead(sensorPin) + pulse_calc/2;
- // Add a small amount of hysteresis添加少量滞后
- if ((sensorValue - 3) / pulse_calc > pulse_time) {
- // Makes a pulse of max 20 mS because SensorValue can max become 1023 使脉冲最大为20毫秒,因为传感器最大值可以变为1023
- pulse_time = sensorValue / pulse_calc;
- }
- if ((sensorValue + 3) / pulse_calc < pulse_time) {
- pulse_time = sensorValue / pulse_calc;
- }
- }
- //-------------------------------------------------------------------------------------------
- void reset_timer() {
- // Set timer to start counting from current time设置计时器以从当前时间开始计数
- previousMillis = millis();
- }
- //-------------------------------------------------------------------------------------------
- bool delay_done(unsigned long delay) {
- // If difference between current time and previous time is more than the delay如果当前时间和上一时间之间的差异大于延迟
- if ((unsigned long)(millis() - previousMillis) >= delay) {
- return true;
- }
- return false;
- }
- //-------------------------------------------------------------------------------------------
- void time_display() { // 7 segment pulse time displaying
- if (pulse_time != previous_pulse_time) {
- previous_pulse_time = pulse_time;
- // Skip to ones digit跳到个位数
- displayStep = 2;
- }
- if (tens == 0) {
- // Skip to ones digit跳到个位数
- displayStep = 2;
- }
- tens = (pulse_time % 100) / 10; // 十位
- ones = pulse_time % 10; // Ones place个位
- //delay(500);
- switch (displayStep) {
- // Display tens digit显示十位数
- case 0:
- // Time to clear display after displaying both digits显示两个数字后清除显示的时间
- if (delay_done(400)) {
- // Display tens digit显示十位数
- segments(z[tens]);
- reset_timer();
- displayStep = 1;
- }
- break;
- // Delay after showing tens digit延迟后显示十位数字
- case 1:
- if (delay_done(300)) {
- // Clear display清屏
- segments(z[11]);
- displayStep = 2;
- }
- break;
- // Display ones digit显示个位数字
- case 2:
- segments(y[ones]);
- reset_timer();
- displayStep = 3;
- break;
- // Delay after showing ones digit 延迟最后显示的数字
- case 3:
- if (delay_done(300)) {
- // Clear display 清屏
- segments(z[11]);
- reset_timer();
- displayStep = 0;
- }
- break;
- }
- }
- //-------------------------------------------------------------------------------------------
- void double_pulse() {
- pulse_short = (pulse_time / 8); // 设置短脉冲为1/8脉冲
- if (pulse_short < 1) // 使短脉冲至少一个毫秒
- pulse_short = 1;
- digitalWrite(ledPin, HIGH);
- digitalWrite(mosfetPin, HIGH);
- delay(pulse_short);
- digitalWrite(mosfetPin, LOW);
- delay(pulse_short);
- digitalWrite(mosfetPin, HIGH);
- delay(pulse_time);
- digitalWrite(mosfetPin, LOW);
- //保持额外的50ms
- delay(50);
- digitalWrite(ledPin, LOW);
- }
复制代码 |
打赏
-
查看全部打赏
|