// declare section.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
//
int myInts[6];
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[5] = {2, 4, -8, 3, 2};
int myArray [5][4] =
{
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
{0, 0, 0, 1},
};
int incomingByte = 0; // for incoming serial data
char message[6] = "hello";
int led_pin_no;
int input1;
// input
# define z1 1
// memory
int read_z1 = 2;
// initial section.
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
void setup() {
// put your setup code here, to run once:
//Serial.begin(115200);
myservo.attach(13); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
for (led_pin_no = 13; led_pin_no < 6 ; led_pin_no--) {
pinMode(led_pin_no, OUTPUT);
};
}
// main program.
void loop() {
motor_servo();
Temperature();
// put your main code here, to run repeatedly:
Serial.println("Start program");
Serial.println("turn on all");
turn_on_led();
turn_off_led();
Serial.println("turn on L-R");
turn_on_led_L();
turn_off_led();
Serial.println("turn on R-L");
turn_on_led_R();
turn_off_led();
Serial.println("Start Print character");
print_char(10);
Serial.println("Start Print array");
print_myArray();
/*
Serial.println("Start input a");
if (Serial.available() > 0) {
Serial.println("Start input b1");
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
} else {
Serial.println("Start input b2");
Serial.available();
}
*/
Serial.println("Start input c");
Serial.println(z1); // from memory
delay(1000);
Serial.println(read_z1); // from define
delay(1000);
// test_serial_read();
// input_k();
}
/* function turn on led */
void turn_on_led() {
led_pin_no = 13;
while (led_pin_no > 7) {
digitalWrite(led_pin_no, 1); //turn light
led_pin_no--;
}
delay(500);
}
/* function turn on led */
void turn_on_led_R() {
led_pin_no = 10;
while (led_pin_no <= 13) {
digitalWrite(led_pin_no, 1); //turn light
delay(500);
led_pin_no++;
}
}
/* function turn off led */
void turn_off_led() {
led_pin_no = 8;
for (led_pin_no = 8; led_pin_no <= 13; led_pin_no++)
{
digitalWrite(led_pin_no, 0); //turn off
}
delay(500);
}
void turn_on_led_L() {
led_pin_no = 13;
while (led_pin_no > 7) {
digitalWrite(led_pin_no, 1); //turn light
delay(500);
led_pin_no--;
}
}
void print_char(int input1) {
for (input1; (20 >= input1); input1++) {
Serial.println(input1);
};
Serial.println("end parameter");
}
void print_myArray() {
for (int i = 0; 4 >= i; i++) {
Serial.println(myPins[i]);
delay(1000);
};
Serial.println("end array");
/*
Serial.println(myPins[0]);
Serial.println(myPins[1]);
Serial.println(myPins[2]);
Serial.println(myPins[4]);
*/
}
void test_serial_read() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
void input_k() {
Serial.println("Enter data:");
while (Serial.available() == 0) {} //wait for data available
String teststr = Serial.readString(); //read until timeout
teststr.trim(); // remove any \r \n whitespace at the end of the String
if (teststr == "red") {
Serial.println("A primary color");
} else {
Serial.println("Something else");
}
}
void motor_servo() {
Serial.print(pos);
Serial.println("start servo ");
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
Serial.println("start servo ");
Serial.println("mid servo");
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
Serial.println("start servo ");
Serial.println("end servo");
}
void Temperature() {
int analogValue = analogRead(A0);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
if (celsius < 26.00) {
Serial.println("cool");
digitalWrite(7, HIGH);
} else {
Serial.println("hot");
digitalWrite(7, HIGH);
}
delay(1000);
}