malloc() in C

#include <stdio.h>
#include <stdlib.h>

int main(){
 int *ptr = malloc(sizeof(int));
 
 printf("position : %X\n", ptr);
 printf("value : %d\n", *ptr);
 
 *ptr = 100;
 
 printf("position : %X\n", ptr);
 printf("value : %d\n", *ptr);

 free(ptr); 
 int size = 10;
 // like array[10]
 int *arr = malloc(size * sizeof(int));
 int i;
 
 // set value in element of array.
 for (i=0;i<size;i++){
 *(arr+i) = i*2;
 }
 
 // print all value in element of array.
 for (i=0;i<size;i++){
 printf("arr[%d] = %d\n", i, *(arr+i));
 }
 free(arr);
}

enum, struct, typedef, pointer struct use in C

#include <stdio.h>
#include <string.h>

struct Ball {
char color[10];
double radius;
};

struct Car {
struct Ball ball;
int count;
};

struct Fruit {
int apple;
} fruit;

//typedef like alise
typedef int foo;
typedef struct Car car2;

enum color {
red, green, yellow
};

enum color get_color(){
return green;
}

union int_or_float {

int int_member;
float float_member;
};

struct Student {
char name[20];
int roll;
};

main(){
struct Ball ball;
strcpy(ball.color, "green");
ball.radius = 3.14159;
printf("%s %.5f\n", ball.color, ball.radius);

struct Car car;
strcpy(car.ball.color, "red");
car.count = 10;

printf("%s %.5f\n", car.ball.color, car.count);

struct Ball ball2 = {"yellow", 1.234};
printf("%s %.5f\n", ball2.color, ball2.radius);

printf("%d\n", ++fruit.apple);

foo f = 10;

printf("%d\n", f);

car2 car3;

strcpy(car3.ball.color, "black");
car3.count = 5;

printf("%s %d\n", car3.ball.color, car3.count);

enum color mycolor;
mycolor = get_color();

if(mycolor == green)
printf("match color green.\n");

union int_or_float myuni1, myuni2;
myuni2.float_member = 1.234;

struct Student *student, stu;
student=&stu;

strcpy(student->name, "Huang");
student->roll = 2;

printf("%s %d\n", student->name, student->roll);
printf("%s\n", (*student).name);
}

copy array function

#include <stdio.h>

void copy(char from[], char to[]);
char from[]="ABCDEFG";
char to[10];

int main(){
 //char mychar[] = "ABCDEFG";
 //printf("%s\n", mychar);

 copy(from, to);
 printf("from = %s\n", from);
 printf("to = %s\n", to);
}

void copy(char f[], char t[]){
 int i;
 for (i=0;i<=sizeof(to);i++){
 to[i] = f[i];
 }
}

Output
from = ABCDEFG
to = ABCDEFG

C get length of array

#include <stdio.h>

#define get_len_way(x) (sizeof(x)/sizeof(*x)) //get length of array way [Micro]

// You can not use the function way for C, it's will return 1, not size of array.
int get_len(int myarray[]){
 int len = (sizeof(myarray) / sizeof(*myarray) );
 return len;
}

main(){
 int ndigit[]={5,4,3,2,1,0};
 int len2 = get_len(ndigit);
 int len3 = get_len_way(ndigit);

 // get length of Array way
 int len = (sizeof(ndigit) / sizeof(*ndigit) );

 printf("%d\n", len);
 printf("%d\n", len2);
 printf("%d\n", len3);
}

Output:
6
1
6

cpp reference

http://www.cplusplus.com/doc/tutorial/pointers/

http://en.cppreference.com/w/cpp/language/operator_incdec

Click to access C-introduction.pdf

 

http://www.cplusplus.com/reference/cstring/strtok/

#include <stdio.h>
#include <string.h>

typedef struct {
int first, second;
} pair;

int main(){
char str[] = “This’s, a sample string”;
char *pch;
pair p;
p.first = 0;
p.second = 1;

printf(“%d %d\n”, p.first, p.second);
printf(“splitting string: %s”, str);

pch = strtok(str, “,”);
while(pch != NULL){
printf(“%s\n”, pch);

pch = strtok(NULL, “,”);
}

return 0;
}

 

Arduino with L239D to control motor rotation

The L293D is a monolithic integrated, high voltage, high current, 4-channel driver.

Basically this means using this chip you can use DC motors and power supplies of up to 36 Volts, thats some pretty big motors and the chip can supply a maximum current of 600mA per channel, the L293D chip is also what’s known as a type of H-Bridge. The H-Bridge is typically an electrical circuit that enables a voltage to be applied across a load in either direction to an output, e.g. motor.

2014-05-16_135057

wpid-img_20140512_230627.jpg

2014-05-16_134519

You can see from my photos how I’ve placed the L293D and wired it according to the above pins.

2014-05-16_135159

int motor_pin1 = 3;   //L239D INPUT2 -> MAPPING TO OUTPUT2 MOTOR(-)
int motor_pin2 = 4;   //L239D INPUT1 -> MAPPING TO OUTPUT1 MOTOR(+)
int enable_pin = 6;   //L239D ENABLE1

void setup(){
 pinMode(motor_pin1, OUTPUT);
 pinMode(motor_pin2, OUTPUT);
 pinMode(enable_pin, OUTPUT);

 // When enable_pin to HIGH, it's will be let motor to rotate.
 digitalWrite(enable_pin, HIGH);
}

void loop(){
 // turn motor rotate to postive direction <--
 digitalWrite(motor_pin1, LOW);
 digitalWrite(motor_pin2, HIGH);

 // turn motor rotate to opposite direction -->
 //digitalWrite(motor_pin1, HIGH);
 //digitalWrite(motor_pin2, LOW);
}

ST-L239D datasheet