A very simple Arduino board test... LOL
Here is some very easy code to test a cheap Arduino board I just got from China via Aliexpress. I am still waiting on a about 30 more orders from Aliexpress for more Arduino stuff. This was the first order which made it here.
/*
Arduino test-code
This code is intended to be used for an easy introduction to the Arduino board.
The program reads data from 2 digital and 2 analog ports and prints the values to the monitor with a sampling speed of 1 Hz.
*/
int counter = 0, analog0, analog1; //Creates variables as integers
float analog0_volt, analog1_volt; //Creates variables as floating numbers.
boolean digital2, digital3; //Creates variables as boolean (HIGH or LOW, TRUE or FALSE)
void setup()
{ //Setup. This section runs only once.
Serial.begin(9600); //Sets the communication speed between PC and Arduino Uno to 9600 baud.
pinMode(2, INPUT); //Sets the digital 2 pin to input mode (High impedance).
pinMode(3, INPUT_PULLUP); //Sets the digital 2 pin to input mode with "Pull Up" (High impedance and Normal High).
delay(500); //0.5 seconds break.
} //end of setup
void loop()
{ //Loop. This section runs in an unending loop.
//Collect data from all the inputs
analog0 = analogRead(A0); //Reads data from analog port 0 and stores it in the variable "analog0".
analog1 = analogRead(A1); //Reads data from analog port 1 and stores it in the variable "analog1".
digital2 = digitalRead(2); //Reads data from digital port 2 and stores it in the variable "digital2".
digital3 = digitalRead(3); //Reads data from digital port 3 and stores it in the variable "digital3".
//Note the difference between pin numbering in analog (Ax) and digital (x).
//Convertion from digital value to voltage value
analog0_volt = ((float)analog0 / 1023) * 5; //Scales from 0-1023 (integers) to 0.0-5.0 (decimal numbers). 0=0V 1023=5V.
analog1_volt = (analog1 / 1023.0) * 5; //Does the same as the previous line. Notice the difference in the syntax.
//Print out data to the monitor
Serial.print(counter); //Prints out the variable "counter".
Serial.print(" - Analog0: "); //Prints the text inside the brackets "__".
Serial.print(analog0_volt); //Prints out the variable "analog0_volt".
Serial.print(" Analog1: "); //Prints the text.
Serial.print(analog1_volt); //Prints out the variable "analog1_volt".
Serial.print(" Digital2: "); //Prints the text.
Serial.print(digital2); //Prints out the variable "digital2".
Serial.print(" Digital3: "); //Prints the text.
Serial.println(digital3); //Prints out the variable "digital3" and ends with a line shift.
delay(1000); //A 1000ms delay.
counter++; //Increments the variable "counter".
} //end of function (Returns to start of loop).
//The program inside the loop function will continue forever and ever, or until power down.
Using the Arduino IDE on the mac I uploaded the code above into my new cheap Chinese Arduino board and set up a simple test before I get into some more interesting stuff.
Well, at least the board works, the Arduino IDE on my mac works, etc.....
This is gonna be fun, I can tell
I think I am going to build something for my motorcycle (not sure yet), using a 3G module like this one:
https://www.aliexpress.com/item/32814966264.html
Or maybe I'll do some home automation for fun; so I can control things with SMS messages; or both.