Doing math on your computer before sending it to the Arduino

OT, but here's an awkward way to use it:

long apples = 0, oranges = 0;
void setup() {
  Serial.begin(115200);
}
void loop() {
  Serial.print("apples:");
  Serial.print(apples);
  Serial.print(" oranges:");
  Serial.println(oranges);
  Serial.print("        ");
  // Use the conditional operator for LHS assignment
  // See https://en.wikipedia.org/wiki/Ternary_conditional_operator#C++
  //Serial.println((random(2) == 1 ? apples : oranges) += 1);
  Serial.println(++(random(2) == 1 ? apples : oranges));
  delay(500);
}
apples:0 oranges:0
        1
apples:1 oranges:0
        2
apples:2 oranges:0
        3
apples:3 oranges:0
        1
apples:3 oranges:1
        2
apples:3 oranges:2
        3
apples:3 oranges:3
        4
apples:3 oranges:4
        5
1 Like

I didn't know that was I thing you could do. Although maybe I should mention the "data" being entered into the PC is a couple of simple measurements, like the diameter of the torus and the width of the wire being used to wrap, so its not too bad to enter it by hand.

Now that you do know, has it changed your view as to how your project should be implemented ?

Here is a sample of what you can do with a PC connected to the Arduino via tcpip. It could be an android tablet or even a phone. It gives you and HID Human Interface Device. All of the data, set points are stored in in a sqlight database on the PC and then uploaded to the Arduino for processing. The PC polls the Arduino every few seconds to update the measurements and processes occurring in the Arduino device displayed by the HID.

This may be more than you need but the possibilities are practically endless.

Wow that's really helpful to know! Probably won't need this for the current project, but for future use it does open a lot up.

I don't think so. Since there is only a couple quantities we really need to enter in by hand, it seems like it would be easier to just keep plugging it into the computer by hand vs implementing something new to input into the Arduino directly. Or I could be misunderstanding how difficult it would be to implement something like that.

The fewer values that there are to enter the more it makes sense to enter them directly into the Arduino rather than plugging it into a PC. You would also need to run software on the PC to communicate with the Arduino and to write software on the Arduino to receive, parse and validate the input

Another factor is how often the values need to be entered. If it is say once a month then using the PC would be acceptable but if it were say twice a day in different locations then using the Arduino for input makes more sense. As has been suggested the data entry could be done using a browser running on any compatible device

The values really only need to be changed once a week and in the same location each time, so I think it should be good. However, if we needed to scale up production, I could see why reconnecting to the computer ever time would get tedious.

In Windows you can use a script to make the calculations, then send values to the Arduino using the SendKeys function. The script below sends the computer's timestamp to the Arduino via the serial monitor. It works with Version 1 of the IDE where the serial monitor is a separate program. I don't know if it works with IDE v2. If not, you can use any terminal program instead, and change the AppActivate value to something like TERMITE. It will work if the terminal program is in the background, but not if it is minimized.

' Timestamp.vbs
' VBScript for Windows

' This script sends the current system date/time to the keyboard input of the
' Arduino Serial Monitor. If more than one Serial Monitor is open, change the
' "COM" entry below to the specific COM port used for the RTC sketch (i.e. "COM3").

' The Serial Monitor line-end setting must be set to Newline or Carriage Return.

' The "Weekday(dDate,1)" entry is for Sunday being day 1.
' Change to "Weekday(dDate,2)" to make Monday day 1.


Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.AppActivate "COM"

oldTime = Time
While oldTime = Time
Wend

WshShell.SendKeys TimeStamp(Now)

Function TimeStamp(dDate)
    TimeStamp = "S"&right("0"&second(dDate),2)&"{ENTER}"&"X"&right("0"&Minute(dDate),2) _
	&right("0"&Hour(dDate),2)&Weekday(dDate,1)&right("0"&Day(dDate),2) _
	&right("0"&Month(dDate),2)&right(Year(dDate),2)&"{ENTER}"
End Function
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.