0% found this document useful (0 votes)
3 views

coding robot lengan

The document is a C source code file for a microcontroller application that initializes and controls a robotic arm using servo motors and I2C communication with an LCD display. It includes functions for sensor detection, servo movement, and LCD updates based on sensor inputs. The main loop continuously checks sensor states to update the count of detected objects and controls the servo movements accordingly.

Uploaded by

Majid Mumtaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

coding robot lengan

The document is a C source code file for a microcontroller application that initializes and controls a robotic arm using servo motors and I2C communication with an LCD display. It includes functions for sensor detection, servo movement, and LCD updates based on sensor inputs. The main loop continuously checks sensor states to update the count of detected objects and controls the servo movements accordingly.

Uploaded by

Majid Mumtaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

main.

c Tuesday, December 10, 2024, 9:13 AM

1 /* USER CODE BEGIN Header */


2 /**
3 ******************************************************************************
4 * @file : main.c
5 * @brief : Main program body
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2024 STMicroelectronics.
10 * All rights reserved.
11 *
12 * This software is licensed under terms that can be found in the LICENSE file
13 * in the root directory of this software component.
14 * If no LICENSE file comes with this software, it is provided AS-IS.
15 *
16 ******************************************************************************
17 */
18 /* USER CODE END Header */
19 /* Includes ------------------------------------------------------------------*/
20 #include "main.h"
21
22 /* Private includes ----------------------------------------------------------*/
23 /* USER CODE BEGIN Includes */
24 #include "stdio.h"
25 #include "stdlib.h"
26 #include "math.h"
27 #include "pca9685.h"
28 #include "../../ECUAL/I2C_LCD/I2C_LCD.h"
29 /* USER CODE END Includes */
30
31 /* Private typedef -----------------------------------------------------------*/
32 /* USER CODE BEGIN PTD */
33 int readSensor1 = 0;
34 int readSensor2 = 0;
35 int jumlah=0;
36 /* USER CODE END PTD */
37
38 /* Private define ------------------------------------------------------------*/
39 /* USER CODE BEGIN PD */
40 #define MyI2C_LCD I2C_LCD_1
41 /* USER CODE END PD */
42
43 /* Private macro -------------------------------------------------------------*/
44 /* USER CODE BEGIN PM */
45
46 /* USER CODE END PM */
47
48 /* Private variables ---------------------------------------------------------*/
49 I2C_HandleTypeDef hi2c1;
50 I2C_HandleTypeDef hi2c2;
51
52 /* USER CODE BEGIN PV */
53
54 /* USER CODE END PV */
55
56 /* Private function prototypes -----------------------------------------------*/
57 void SystemClock_Config(void);
58 static void MX_GPIO_Init(void);
59 static void MX_I2C1_Init(void);

Page 1
main.c Tuesday, December 10, 2024, 9:13 AM

60 static void MX_I2C2_Init(void);


61 /* USER CODE BEGIN PFP */
62
63 /* USER CODE END PFP */
64
65 /* Private user code ---------------------------------------------------------*/
66 /* USER CODE BEGIN 0 */
67
68 /* USER CODE END 0 */
69
70 /**
71 * @brief The application entry point.
72 * @retval int
73 */
74 int main(void)
75 {
76
77 /* USER CODE BEGIN 1 */
78
79 /* USER CODE END 1 */
80
81 /* MCU Configuration--------------------------------------------------------*/
82
83 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
84 HAL_Init();
85
86 /* USER CODE BEGIN Init */
87
88 /* USER CODE END Init */
89
90 /* Configure the system clock */
91 SystemClock_Config();
92
93 /* USER CODE BEGIN SysInit */
94
95 /* USER CODE END SysInit */
96
97 /* Initialize all configured peripherals */
98 MX_GPIO_Init(); // Inisiasi pin GPIO
99 MX_I2C1_Init(); // Inisiasi antarmuka I2C pertama
100 MX_I2C2_Init(); // Inisiasi antarmuka I2C kedua
101 /* USER CODE BEGIN 2 */
102 // Inisiasi driver servo PCA 9685
103 PCA9685_Init(&hi2c1);
104 // Inisiasi posisi awal motor servo
105 PCA9685_SetServoAngle(0, 0);
106 PCA9685_SetServoAngle(1, 80);
107 PCA9685_SetServoAngle(2, 30);
108 PCA9685_SetServoAngle(3, 180);
109 PCA9685_SetServoAngle(4, 0);
110
111 // Inisiasi tampilan LCD
112 I2C_LCD_Clear(MyI2C_LCD);
113 I2C_LCD_Init(MyI2C_LCD);
114 I2C_LCD_SetCursor(MyI2C_LCD, 2, 0);
115 I2C_LCD_WriteString(MyI2C_LCD, "Robot Lengan");
116 I2C_LCD_SetCursor(MyI2C_LCD, 1, 1);
117 I2C_LCD_WriteString(MyI2C_LCD, "Siap Dijalankan");
118 I2C_LCD_Backlight(MyI2C_LCD);

Page 2
main.c Tuesday, December 10, 2024, 9:13 AM

119 I2C_LCD_Display(MyI2C_LCD);
120 /* USER CODE END 2 */
121
122 /* Infinite loop */
123 /* USER CODE BEGIN WHILE */
124 while (1)
125 {
126 // Sensor pertama mendeteksi objek (mendeteksi objek)
127 readSensor1 = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4);
128
129 // Sensor kedua mendeteksi objek (untuk reset hitungan)
130 readSensor2 = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5);
131
132 if (readSensor1 == 0) // Jika sensor pertama mendeteksi objek
133 {
134 jumlah++; // Kenaikan hitungan
135 I2C_LCD_Clear(MyI2C_LCD);
136 I2C_LCD_SetCursor(MyI2C_LCD, 1, 0);
137 I2C_LCD_WriteString(MyI2C_LCD, " Jumlah Botol: ");
138 I2C_LCD_SetCursor(MyI2C_LCD, 7, 7);
139
140 char buffer[16]; // Membuat array untuk menampung 16 karakter
141 sprintf (buffer, "%d", jumlah); // Konversi nilai integer
jumlah ke string
142 I2C_LCD_WriteString(MyI2C_LCD, buffer); // Menampilkan
hitungan
143
144 // Menggerakkan motor servo pada robot lengan
145 for(int i =45; i<120;i+=2){ // Menggerakkan servo ketiga dari
45 ke 120
146 PCA9685_SetServoAngle(2, i);
147 HAL_Delay(50);
148 }
149 HAL_Delay(1000);
150 for(int i =180; i>90;i-=2){ // Menggerakkan servo keempat
dari 180 ke 90
151 PCA9685_SetServoAngle(3, i);
152 HAL_Delay(50);
153 }
154 HAL_Delay(1000);
155 for(int i =90; i>45;i-=2){ // Menggerakkan servo ketiga dari
90 ke 45
156 PCA9685_SetServoAngle(2, i);
157 HAL_Delay(50);
158 }
159 HAL_Delay(1000);
160 for(int i =80; i>45;i-=2){ // Menggerakkan servo kedua dari 80
ke 45
161 PCA9685_SetServoAngle(1, i);
162 HAL_Delay(75);
163 }
164 HAL_Delay(1000);
165 PCA9685_SetServoAngle(4, 30); // Menggerakkan servo
kelima dari 0 ke 30
166 HAL_Delay(3000);
167 for(int i =45; i>90;i-=2){ // Menggerakkan servo kedua dari 45
ke 90
168 PCA9685_SetServoAngle(1, i);
169 HAL_Delay(75);

Page 3
main.c Tuesday, December 10, 2024, 9:13 AM

170 }
171 HAL_Delay(1000);
172 for(int i =45; i<140;i+=2){ // Menggerakkan servo servo ketiga
dari 45 ke 140
173 PCA9685_SetServoAngle(2, i);
174 HAL_Delay(50);
175 }
176 HAL_Delay(1000);
177 for(int i =90; i<180;i+=2){ // Menggerakkan servo keempat dari
90 ke 180
178 PCA9685_SetServoAngle(3, i);
179 HAL_Delay(25);
180 }
181 HAL_Delay(1000);
182 for(int i =0; i<120;i+=2){ // Menggerakkan servo pertama dari
0 ke 120
183 PCA9685_SetServoAngle(0, i);
184 HAL_Delay(50);
185 }
186 HAL_Delay(3000);
187 PCA9685_SetServoAngle(4, 0); // Menggerakkan servo kelima
dari 30 ke 0
188 HAL_Delay(3000);
189 for(int i =120; i>0;i-=2){ // Menggerakkan servo pertama dari
120 ke 0
190 PCA9685_SetServoAngle(0, i);
191 HAL_Delay(50);
192 }
193 HAL_Delay(3000);
194 for(int i =140; i>45;i-=2){ // Menggerakkan servo ketiga dari
140 ke 45
195 PCA9685_SetServoAngle(2, i);
196 HAL_Delay(25);
197 }
198 HAL_Delay(1000);
199 }
200 else if (readSensor2 == 0) // Jika sensor kedua mendeteksi objek
(untuk reset hitungan)
201 {
202 jumlah = 0; // Reset hitungan
203 I2C_LCD_Clear(MyI2C_LCD);
204 I2C_LCD_SetCursor(MyI2C_LCD, 5, 0);
205 I2C_LCD_WriteString(MyI2C_LCD, "Wadah"); // Menampilkan pesan
wadah sudah penuh
206 I2C_LCD_SetCursor(MyI2C_LCD, 2, 1);
207 I2C_LCD_WriteString(MyI2C_LCD, "Sudah Penuh");
208 HAL_Delay(2000); // Delay untuk memperlihatkan pesan reset
209 I2C_LCD_Clear(MyI2C_LCD); // Kosongkan layar LCD setelah reset
210 I2C_LCD_SetCursor(MyI2C_LCD, 1, 0);
211 I2C_LCD_WriteString(MyI2C_LCD, " Jumlah Botol: ");
212 I2C_LCD_SetCursor(MyI2C_LCD, 7, 7);
213 I2C_LCD_WriteString(MyI2C_LCD, "0 ");
214
215 }
216 else{
217 // Kembali ke posisi awal
218 PCA9685_SetServoAngle(0, 0);
219 PCA9685_SetServoAngle(1, 80);
220 PCA9685_SetServoAngle(2, 30);

Page 4
main.c Tuesday, December 10, 2024, 9:13 AM

221 PCA9685_SetServoAngle(3, 0);


222 PCA9685_SetServoAngle(4, 0);
223 }
224 }
225 /* USER CODE END WHILE */
226
227 /* USER CODE BEGIN 3 */
228
229 /* USER CODE END 3 */
230 }
231
232 /**
233 * @brief System Clock Configuration
234 * @retval None
235 */
236 void SystemClock_Config(void)
237 {
238 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
239 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
240
241 /** Configure the main internal regulator output voltage
242 */
243 __HAL_RCC_PWR_CLK_ENABLE();
244 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
245
246 /** Initializes the RCC Oscillators according to the specified parameters
247 * in the RCC_OscInitTypeDef structure.
248 */
249 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
250 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
251 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
252 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
253 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
254 RCC_OscInitStruct.PLL.PLLM = 8;
255 RCC_OscInitStruct.PLL.PLLN = 100;
256 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
257 RCC_OscInitStruct.PLL.PLLQ = 4;
258 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
259 {
260 Error_Handler();
261 }
262
263 /** Initializes the CPU, AHB and APB buses clocks
264 */
265 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
266 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
267 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
268 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
269 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
270 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
271
272 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
273 {
274 Error_Handler();
275 }
276 }
277
278 /**
279 * @brief I2C1 Initialization Function

Page 5
main.c Tuesday, December 10, 2024, 9:13 AM

280 * @param None


281 * @retval None
282 */
283 static void MX_I2C1_Init(void)
284 {
285
286 /* USER CODE BEGIN I2C1_Init 0 */
287
288 /* USER CODE END I2C1_Init 0 */
289
290 /* USER CODE BEGIN I2C1_Init 1 */
291
292 /* USER CODE END I2C1_Init 1 */
293 hi2c1.Instance = I2C1;
294 hi2c1.Init.ClockSpeed = 100000;
295 hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
296 hi2c1.Init.OwnAddress1 = 0;
297 hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
298 hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
299 hi2c1.Init.OwnAddress2 = 0;
300 hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
301 hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
302 if (HAL_I2C_Init(&hi2c1) != HAL_OK)
303 {
304 Error_Handler();
305 }
306 /* USER CODE BEGIN I2C1_Init 2 */
307
308 /* USER CODE END I2C1_Init 2 */
309
310 }
311
312 /**
313 * @brief I2C2 Initialization Function
314 * @param None
315 * @retval None
316 */
317 static void MX_I2C2_Init(void)
318 {
319
320 /* USER CODE BEGIN I2C2_Init 0 */
321
322 /* USER CODE END I2C2_Init 0 */
323
324 /* USER CODE BEGIN I2C2_Init 1 */
325
326 /* USER CODE END I2C2_Init 1 */
327 hi2c2.Instance = I2C2;
328 hi2c2.Init.ClockSpeed = 100000;
329 hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
330 hi2c2.Init.OwnAddress1 = 0;
331 hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
332 hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
333 hi2c2.Init.OwnAddress2 = 0;
334 hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
335 hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
336 if (HAL_I2C_Init(&hi2c2) != HAL_OK)
337 {
338 Error_Handler();

Page 6
main.c Tuesday, December 10, 2024, 9:13 AM

339 }
340 /* USER CODE BEGIN I2C2_Init 2 */
341
342 /* USER CODE END I2C2_Init 2 */
343
344 }
345
346 /**
347 * @brief GPIO Initialization Function
348 * @param None
349 * @retval None
350 */
351 static void MX_GPIO_Init(void)
352 {
353 GPIO_InitTypeDef GPIO_InitStruct = {0};
354 /* USER CODE BEGIN MX_GPIO_Init_1 */
355 /* USER CODE END MX_GPIO_Init_1 */
356
357 /* GPIO Ports Clock Enable */
358 __HAL_RCC_GPIOC_CLK_ENABLE();
359 __HAL_RCC_GPIOH_CLK_ENABLE();
360 __HAL_RCC_GPIOB_CLK_ENABLE();
361
362 /*Configure GPIO pin Output Level */
363 HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
364
365 /*Configure GPIO pin : PC13 */
366 GPIO_InitStruct.Pin = GPIO_PIN_13;
367 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
368 GPIO_InitStruct.Pull = GPIO_NOPULL;
369 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
370 HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
371
372 /*Configure GPIO pins : PB4 PB5 */
373 GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
374 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
375 GPIO_InitStruct.Pull = GPIO_NOPULL;
376 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
377
378 /* USER CODE BEGIN MX_GPIO_Init_2 */
379 /* USER CODE END MX_GPIO_Init_2 */
380 }
381
382 /* USER CODE BEGIN 4 */
383
384 /* USER CODE END 4 */
385
386 /**
387 * @brief This function is executed in case of error occurrence.
388 * @retval None
389 */
390 void Error_Handler(void)
391 {
392 /* USER CODE BEGIN Error_Handler_Debug */
393 /* User can add his own implementation to report the HAL error return state */
394 __disable_irq();
395 while (1)
396 {
397 }

Page 7
main.c Tuesday, December 10, 2024, 9:13 AM

398 /* USER CODE END Error_Handler_Debug */


399 }
400
401 #ifdef USE_FULL_ASSERT
402 /**
403 * @brief Reports the name of the source file and the source line number
404 * where the assert_param error has occurred.
405 * @param file: pointer to the source file name
406 * @param line: assert_param error line source number
407 * @retval None
408 */
409 void assert_failed(uint8_t *file, uint32_t line)
410 {
411 /* USER CODE BEGIN 6 */
412 /* User can add his own implementation to report the file name and line number,
413 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
414 /* USER CODE END 6 */
415 }
416 #endif /* USE_FULL_ASSERT */
417

Page 8

You might also like