You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -635,12 +635,134 @@ _Register callback when touch button RIGHT is pressed_
635
635
636
636
637
637
## Examples
638
+
These examples demonstrate practical implementations on how to use the Arduino Alvik API. Whether you're working with MicroPython or C++, these simple examples will help you understand how to implement various features in your projects, making it easy to get started with the Alvik in the language you're most comfortable with.
639
+
640
+
### Simple Color Sensing Example
641
+
642
+
This example demonstrates how to implement basic color sensing. The Alvik's color sensor reads the color of an object placed under it, and the detected color is printed to the console.
643
+
644
+
-**Reference for MicroPython**
645
+
646
+
```python
647
+
from arduino_alvik import ArduinoAlvik
648
+
from time import sleep_ms
649
+
650
+
alvik = ArduinoAlvik()
651
+
alvik.begin()
652
+
653
+
print("Alvik initialized for color sensing")
654
+
655
+
whileTrue:
656
+
color = alvik.get_color_label()
657
+
print(f"Detected color: {color}")
658
+
sleep_ms(500)
659
+
```
660
+
661
+
-**Reference for C++**
662
+
663
+
```c
664
+
#include"Arduino_Alvik.h"
665
+
666
+
Arduino_Alvik alvik;
667
+
668
+
voidsetup() {
669
+
Serial.begin(9600);
670
+
alvik.begin();
671
+
Serial.println("Alvik initialized for color sensing");
672
+
}
673
+
674
+
voidloop() {
675
+
char* color = alvik.get_color_label();
676
+
Serial.print("Detected color: ");
677
+
Serial.println(color);
678
+
delay(500);
679
+
}
680
+
```
681
+
682
+
683
+
684
+
685
+
### Simple Directional Control
686
+
687
+
This example demonstrates very basic control over the robot's movement based on directional arrow buttons. The Alvik will drive in the direction corresponding to the arrow button pressed (up, down, left, right).
688
+
689
+
-**Reference for MicroPython**
690
+
```python
691
+
from arduino_alvik import ArduinoAlvik
692
+
from time import sleep_ms
693
+
694
+
alvik = ArduinoAlvik()
695
+
alvik.begin()
696
+
697
+
print("Alvik initialized")
698
+
699
+
whileTrue:
700
+
if alvik.get_touch_up():
701
+
print("Moving Up")
702
+
alvik.drive(100, 0, linear_unit='cm/s')
703
+
sleep_ms(1000)
704
+
alvik.brake()
705
+
elif alvik.get_touch_down():
706
+
print("Moving Down")
707
+
alvik.drive(-100, 0, linear_unit='cm/s')
708
+
sleep_ms(1000)
709
+
alvik.brake()
710
+
elif alvik.get_touch_left():
711
+
print("Turning Left")
712
+
alvik.drive(0, 100, angular_unit='deg/s')
713
+
sleep_ms(1000)
714
+
alvik.brake()
715
+
elif alvik.get_touch_right():
716
+
print("Turning Right")
717
+
alvik.drive(0, -100, angular_unit='deg/s')
718
+
sleep_ms(1000)
719
+
alvik.brake()
720
+
sleep_ms(100)
721
+
722
+
```
723
+
-**Reference for C++**
724
+
```C
725
+
#include"Arduino_Alvik.h"
726
+
727
+
Arduino_Alvik alvik;
728
+
729
+
voidsetup() {
730
+
Serial.begin(9600);
731
+
alvik.begin();
732
+
Serial.println("Alvik initialized");
733
+
}
734
+
735
+
voidloop() {
736
+
if (alvik.get_touch_up()) {
737
+
Serial.println("Moving Up");
738
+
alvik.drive(100, 0, "cm/s");
739
+
delay(1000);
740
+
alvik.brake();
741
+
} else if (alvik.get_touch_down()) {
742
+
Serial.println("Moving Down");
743
+
alvik.drive(-100, 0, "cm/s");
744
+
delay(1000);
745
+
alvik.brake();
746
+
} else if (alvik.get_touch_left()) {
747
+
Serial.println("Turning Left");
748
+
alvik.drive(0, 100, "deg/s");
749
+
delay(1000);
750
+
alvik.brake();
751
+
} else if (alvik.get_touch_right()) {
752
+
Serial.println("Turning Right");
753
+
alvik.drive(0, -100, "deg/s");
754
+
delay(1000);
755
+
alvik.brake();
756
+
}
757
+
delay(100);
758
+
}
759
+
```
638
760
639
761
### Line Following
640
762
641
-
This example demonstrates how to create a simple line-following robot. The code initializes the robot, reads sensor data to detect the line, calculates the error from the center of the line, and adjusts the robot's wheel speeds to follow the line. It also uses LEDs to indicate the direction the robot is turning.
763
+
This example demonstrates how to create a simple line-following robot. The code initializes the Alvik, reads sensor data to detect the line, calculates the error from the center of the line, and adjusts the Alvik's wheel speeds to follow the line. It also uses LEDs to indicate the direction the robot is turning.
642
764
643
-
The robot starts when the OK button is pressed and stops when the Cancel button is pressed. The robot continuously reads the line sensors, calculates the error, and adjusts the wheel speeds to correct its path.
765
+
The Alvik starts when the OK button is pressed and stops when the Cancel button is pressed. The Alvik continuously reads the line sensors, calculates the error, and adjusts the wheel speeds to correct its path.
644
766
645
767
-**Reference for MicroPython**
646
768
@@ -799,35 +921,7 @@ float calculate_center(const int left, const int center, const int right){
799
921
}
800
922
```
801
923
802
-
### Drive
803
-
804
-
```C
805
-
/*
806
-
This file is part of the Arduino_Alvik library.
807
-
808
-
Copyright (c) 2024 Arduino SA
809
924
810
-
This Source Code Form is subject to the terms of the Mozilla Public
811
-
License, v. 2.0. If a copy of the MPL was not distributed with this
812
-
file, You can obtain one at http://mozilla.org/MPL/2.0/.
0 commit comments