0
Follow
0
View

How does arduino temporarily store the result of each scan and press the button to print it

deathgod555 注册会员
2023-02-28 12:12

If your scan function void scan() already gets the data it needs correctly and stores it in an array, Then you can use the array directly in the loop() function without having to call analogRead() again to get the data.

You can declare the scanResults array as a global variable so that it can be accessed in the loop() function. In the loop() function, when the counter scanCounter reaches 3, you can do subsequent operations directly with the data in the scanResults array instead of calling the void scan() function again.

Here is an example of the modified code:

// 定义存储扫描结果的数组和计数器
int scanResults[3];
int scanCounter = 0;

void setup() {
  Serial.begin(9600); // 初始化串口通信
}

void loop() {
  // 扫描数据并存储到数组中
  scan(); // 假设您的扫描函数为 void scan()
  scanCounter++;
  
  // 检查是否已经扫描了3次
  if (scanCounter == 3) {
    scanCounter = 0; // 重置计数器
    
    // 等待按键按下
    while (digitalRead(2) == HIGH) {
      delay(10);
    }
    
    // 按键按下后打印数据
    Serial.print("Scan results: ");
    for (int i = 0; i < 3; i++) {
      Serial.print(scanResults[i]);
      Serial.print(" ");
    }
    Serial.println();
    
    // 等待按键释放
    while (digitalRead(2) == LOW) {
      delay(10);
    }
  }
}

Note that in the code example above, the void scan() function needs to be defined and implemented before the loop() function. Also, because the scanResults and scanCounter arrays are global variables, they need to be declared outside of the setup() function.

dzg8619 注册会员
2023-02-28 12:12

This answer quotes ChatGPT

Here is a complete code example of a simple C# console application for storing and printing scan data:

using System;
using System.Collections.Generic;

namespace ScanDataStorage
{
    class Program
    {
        static List<string> scannedData = new List<string>();

        static void Main(string[] args)
        {
            Console.WriteLine("请扫描数据,按q退出程序:");

            while (true)
            {
                string input = Console.ReadLine();

                if (input == "q") break;

                scannedData.Add(input);
                Console.WriteLine($"已存储 {scannedData.Count} 条数据");
            }

            Console.WriteLine("按p打印存储的数据:");
            while (Console.ReadKey().Key == ConsoleKey.P)
            {
                Console.WriteLine("\n--- 开始打印数据 ---");
                foreach (string data in scannedData)
                {
                    Console.WriteLine(data);
                }
                Console.WriteLine("--- 数据打印完成 ---\n");
                Console.WriteLine("按p继续打印,按其他键退出");
            }
        }
    }
}


dongsx1989 注册会员
2023-02-28 12:12

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > you can use List(List) or Array(Array) to store the scan data, The exact code implementation may vary depending on the programming language and hardware device you are using. Here is an example of Python code, assuming you are using the Python programming language, using Arduino as an example:

import serial

# 初始化串口
ser = serial.Serial('COM3', 9600)  # 串口号和波特率需要根据实际情况修改

# 存储扫描结果数据的列表
data_list = []

# 连续扫描并存储数据
while True:
    if ser.in_waiting:
        data = ser.readline().strip().decode('utf-8')
        data_list.append(data)
        print(f"Data {data} saved.")

    # 按下按键打印数据
    if button_pressed():
        for data in data_list:
            print(data)
        data_list = []  # 打印完成后清空数据列表

In this code, the serial port connection is first initialized and an empty list data_list is created to store the scan result data. Then, an infinite loop is used to continuously scan the serial port and store the data into the list. When the key is pressed, a loop is used to walk through the data in the list and print it out one by one, finally emptying the list for the next scan. Note that the button_pressed() function here is a hypothetical function that detects whether a key is pressed, and you need to implement it as needed.

guwanglai 注册会员
2023-02-28 12:12

You can use Arduino arrays and loops to do this using GPT and your own ideas. Here is a simple example code:

// 定义存储扫描结果的数组和计数器
int scanResults[3];
int scanCounter = 0;

void setup() {
  Serial.begin(9600); // 初始化串口通信
}

void loop() {
  // 扫描数据并存储到数组中
  int scanData = analogRead(A0); // 假设这里是模拟输入
  scanResults[scanCounter] = scanData;
  scanCounter++;
  
  // 检查是否已经扫描了3次
  if (scanCounter == 3) {
    scanCounter = 0; // 重置计数器
    
    // 等待按键按下
    while (digitalRead(2) == HIGH) {
      delay(10);
    }
    
    // 按键按下后打印数据
    Serial.print("Scan results: ");
    for (int i = 0; i < 3; i++) {
      Serial.print(scanResults[i]);
      Serial.print(" ");
    }
    Serial.println();
    
    // 等待按键释放
    while (digitalRead(2) == LOW) {
      delay(10);
    }
  }
}

This example code assumes that port A0 is used for analog scanning and key is used for digital port 2. Each scan stores the results into an array of length 3. After scanning for 3 times, wait for the key to be pressed.

About the Author

Question Info

Publish Time
2023-02-28 12:12
Update Time
2023-02-28 12:12