Skip to content

Commit ecdd7e6

Browse files
commit
0 parents  commit ecdd7e6

File tree

5 files changed

+371
-0
lines changed

5 files changed

+371
-0
lines changed

README.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
= Attiny84_IO_basic Library for robot Cing =
2+
3+
This library allows robot Cing to be programmed easily.
4+
5+
For more information about this library please visit us at
6+
http://robotcing.wz.sk
7+
8+
== License ==
9+
10+
Copyright © 2019 RobotCing Team. All right reserved.
11+
12+
This library is free software; you can redistribute it and/or
13+
modify it under the terms of the GNU Lesser General Public
14+
15+
This library is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
Lesser General Public License for more details.

keywords.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Created by RobotCing Team
3+
*/
4+
###########################################
5+
# Syntax Coloring Map For Attiny84_IO_basic
6+
###########################################
7+
8+
###########################################
9+
# Datatypes (KEYWORD1)
10+
###########################################
11+
Attiny84_IO_basic KEYWORD1
12+
###########################################
13+
# Methods and Functions (KEYWORD2)
14+
###########################################
15+
Robot KEYWORD2
16+
RunMotor KEYWORD2
17+
ReadLightSensor KEYWORD2
18+
ReadUltrasonicSensor KEYWORD2
19+
ReadShineSensor KEYWORD2
20+
ReadButtonExternal KEYWORD2
21+
ReadPotentiometerExternal KEYWORD2
22+
23+
###########################################
24+
# Constants (LITERAL1)
25+
###########################################

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Attiny84_IO_basic
2+
version=3.0.0
3+
author=RobotCing Team
4+
maintainer=RobotCing Team <robotcing@gmail.com>
5+
sentence=Library for robot Cing with Attiny84.
6+
paragraph=This library simplifies programming of robot Cing.
7+
category=Uncategorized
8+
url=http://robotcing.wz.sk
9+
architectures=*

src/Attiny84_IO_basic.cpp

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
/*
2+
Created by RobotCing Team
3+
*/
4+
5+
6+
//--------------------------------------------
7+
// Library import
8+
//--------------------------------------------
9+
#include "Arduino.h"
10+
#include "Attiny84_IO_basic.h"
11+
//--------------------------------------------
12+
Cing::Cing(){}
13+
//--------------------------------------------
14+
// Motors
15+
//--------------------------------------------
16+
void Cing::RunMotor(String motor,int speed,String mode)
17+
{
18+
#define motorA 5
19+
#define motorB 6
20+
#define INA1 7
21+
#define INA2 8
22+
#define INB1 9
23+
#define INB2 10
24+
pinMode(motorA,OUTPUT);
25+
pinMode(motorB,OUTPUT);
26+
//---------------------
27+
pinMode(INA1,OUTPUT);
28+
pinMode(INA2,OUTPUT);
29+
pinMode(INB1,OUTPUT);
30+
pinMode(INB2,OUTPUT);
31+
if(mode=="analog")
32+
{
33+
int speed_set = map(speed,-100,100,-255,255);
34+
if(motor=="A")
35+
{
36+
if (speed > 0)
37+
{
38+
digitalWrite(INA1,HIGH);
39+
digitalWrite(INA2,LOW);
40+
analogWrite(motorA,speed_set);
41+
}
42+
else if (speed < 0)
43+
{
44+
digitalWrite(INA1,LOW);
45+
digitalWrite(INA2,HIGH);
46+
analogWrite(motorA,abs(speed_set));
47+
}
48+
else
49+
{
50+
analogWrite(motorA,LOW);
51+
}
52+
}
53+
else if(motor=="B")
54+
{
55+
if (speed > 0)
56+
{
57+
digitalWrite(INB1,HIGH);
58+
digitalWrite(INB2,LOW);
59+
analogWrite(motorB,speed_set);
60+
}
61+
else if (speed < 0)
62+
{
63+
digitalWrite(INB1,LOW);
64+
digitalWrite(INB2,HIGH);
65+
analogWrite(motorB,abs(speed_set));
66+
}
67+
else
68+
{
69+
analogWrite(motorB,LOW);
70+
}
71+
}
72+
else if(motor=="AB")
73+
{
74+
if (speed > 0)
75+
{
76+
digitalWrite(INA1,HIGH);
77+
digitalWrite(INA2,LOW);
78+
digitalWrite(INB1,HIGH);
79+
digitalWrite(INB2,LOW);
80+
analogWrite(motorA,speed_set);
81+
analogWrite(motorB,speed_set);
82+
}
83+
else if (speed < 0)
84+
{
85+
digitalWrite(INA1,LOW);
86+
digitalWrite(INA2,HIGH);
87+
digitalWrite(INB1,LOW);
88+
digitalWrite(INB2,HIGH);
89+
analogWrite(motorA,abs(speed_set));
90+
analogWrite(motorB,abs(speed_set));
91+
}
92+
else
93+
{
94+
analogWrite(motorA,LOW);
95+
analogWrite(motorB,LOW);
96+
}
97+
}
98+
else
99+
{
100+
analogWrite(motorA,0);
101+
analogWrite(motorB,0);
102+
}
103+
}
104+
else if(mode=="digital")
105+
{
106+
int speed_set;
107+
if (speed == 1 || speed == -1)
108+
{
109+
speed_set = HIGH;
110+
}
111+
else if (speed == 0)
112+
{
113+
speed_set = LOW;
114+
}
115+
//--------------------------
116+
// A
117+
//--------------------------
118+
if(motor=="A")
119+
{
120+
if (speed == 1)
121+
{
122+
digitalWrite(INA1,HIGH);
123+
digitalWrite(INA2,LOW);
124+
digitalWrite(motorA,speed_set);
125+
}
126+
else if (speed == -1)
127+
{
128+
digitalWrite(INA1,LOW);
129+
digitalWrite(INA2,HIGH);
130+
digitalWrite(motorA,speed_set);
131+
}
132+
else if (speed == 0)
133+
{
134+
digitalWrite(motorA,speed_set);
135+
}
136+
}
137+
//--------------------------
138+
// B
139+
//--------------------------
140+
else if(motor=="B")
141+
{
142+
if (speed == 1)
143+
{
144+
digitalWrite(INB1,HIGH);
145+
digitalWrite(INB2,LOW);
146+
digitalWrite(motorB,speed_set);
147+
}
148+
else if (speed == -1)
149+
{
150+
digitalWrite(INB1,LOW);
151+
digitalWrite(INB2,HIGH);
152+
digitalWrite(motorB,speed_set);
153+
}
154+
else if (speed == 0)
155+
{
156+
digitalWrite(motorB,speed_set);
157+
}
158+
}
159+
//--------------------------
160+
// AB
161+
//--------------------------
162+
else if(motor=="AB")
163+
{
164+
if (speed == 1)
165+
{
166+
digitalWrite(INA1,HIGH);
167+
digitalWrite(INA2,LOW);
168+
digitalWrite(INB1,HIGH);
169+
digitalWrite(INB2,LOW);
170+
digitalWrite(motorA,speed_set);
171+
digitalWrite(motorB,speed_set);
172+
}
173+
else if (speed == -1)
174+
{
175+
digitalWrite(INA1,LOW);
176+
digitalWrite(INA2,HIGH);
177+
digitalWrite(INB1,LOW);
178+
digitalWrite(INB2,HIGH);
179+
digitalWrite(motorA,speed_set);
180+
digitalWrite(motorB,speed_set);
181+
}
182+
else if (speed == 0)
183+
{
184+
digitalWrite(motorA,speed_set);
185+
digitalWrite(motorB,speed_set);
186+
}
187+
}
188+
else
189+
{
190+
digitalWrite(motorA,LOW);
191+
digitalWrite(motorB,LOW);
192+
}
193+
}
194+
}
195+
//--------------------------------------------
196+
// Sensors
197+
//--------------------------------------------
198+
199+
//--------------------------------------------
200+
// LightSensor
201+
//--------------------------------------------
202+
203+
int Cing::ReadLightSensor(int sensor,String mode)
204+
{
205+
#define LightSensor1 A1
206+
#define LightSensor2 A0
207+
pinMode(LightSensor1,INPUT);//1
208+
pinMode(LightSensor2,INPUT);//2
209+
int value;
210+
if (mode=="analog")
211+
{
212+
if (sensor == 1)
213+
{
214+
value = map(analogRead(LightSensor1),0,1023,100,0);
215+
return value;
216+
}
217+
else if (sensor == 2)
218+
{
219+
value = map(analogRead(LightSensor2),0,1023,100,0);
220+
return value;
221+
}
222+
}
223+
else if(mode=="digital")
224+
{
225+
if (sensor == 1)
226+
{
227+
value = digitalRead(LightSensor1);
228+
return value;
229+
}
230+
else if (sensor == 2)
231+
{
232+
value = digitalRead(LightSensor2);
233+
return value;
234+
}
235+
else
236+
{
237+
value = digitalRead(LightSensor1);
238+
return value;
239+
}
240+
}
241+
}
242+
//--------------------------------------------
243+
// UltrasonicSensor
244+
//--------------------------------------------
245+
246+
int Cing::ReadUltrasonicSensor()
247+
{
248+
#define UltrasonicSensor 4
249+
int duration;
250+
int distance;
251+
pinMode(UltrasonicSensor, OUTPUT);
252+
digitalWrite(UltrasonicSensor, LOW);
253+
delayMicroseconds(2);
254+
digitalWrite(UltrasonicSensor, HIGH);
255+
delayMicroseconds(10);
256+
digitalWrite(UltrasonicSensor, LOW);
257+
delayMicroseconds(10);
258+
pinMode(UltrasonicSensor, INPUT);
259+
duration = pulseIn(UltrasonicSensor, HIGH);
260+
distance = duration/58.2;
261+
return distance;
262+
}
263+
//--------------------------------------------
264+
// ShineSensor
265+
//--------------------------------------------
266+
int Cing::ReadShineSensor()
267+
{
268+
#define ShineSensor A4
269+
int shine_value;
270+
pinMode(ShineSensor,INPUT);
271+
shine_value = map(analogRead(ShineSensor),50,700,0,100);
272+
return shine_value;
273+
}
274+
//--------------------------------------------
275+
// ButtonExternal
276+
//--------------------------------------------
277+
bool Cing::ReadButtonExternal()
278+
{
279+
#define button_external A4
280+
pinMode(button_external,INPUT);
281+
bool button_external_value = digitalRead(button_external);
282+
return button_external_value;
283+
}
284+
//--------------------------------------------
285+
// PotentiometerExternal
286+
//--------------------------------------------
287+
int Cing::ReadPotentiometerExternal()
288+
{
289+
#define Potentiometer A4
290+
pinMode(Potentiometer,INPUT);
291+
int Potentiometer_value = map(analogRead(Potentiometer),0,1023,0,100);
292+
return Potentiometer_value;
293+
}

src/Attiny84_IO_basic.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Created by RobotCing Team
3+
*/
4+
#ifndef Attiny84_IO_basic
5+
#define Attiny84_IO_basic
6+
#include "Arduino.h"
7+
//--------------------------------------------
8+
// Creating Class
9+
//--------------------------------------------
10+
class Cing
11+
{
12+
// public variables
13+
public:
14+
//constructor
15+
Cing();
16+
void RunMotor(String motor,int speed= 100,String mode = "digital");
17+
int ReadLightSensor(int sensor = 1,String mode = "digital");
18+
int ReadUltrasonicSensor();
19+
int ReadShineSensor();
20+
bool ReadButtonExternal()
21+
int ReadPotentiometerExternal();
22+
// local variables
23+
private:
24+
25+
};
26+
#endif

0 commit comments

Comments
 (0)