博客來網路書店查詢

書名

博客來網路書店查詢

星期日, 5月 29, 2016

組裝的第二個問題-電池供電給Arduino板子




Arjin Chen 老師說:
 本例由電池供電給Arduino板子有2種方式
1. 使用圖一的DC快速接頭(公頭), 接至Arduino板子的DC母插座, 正極是接至L298N的12V-GND-5V插座中的5V
2. 直接由L298N 5V拉一條線(杜邦公公線即可)接至Arduino板子的VIN供電
1是之前的接法, 2是簡化後的接法, 都可以
--
L298N的GND一條是接到電池負極, 一條是接到Arduino板的GND
--
因掃地機有多個馬達, 故電池採用4節+2節(原2馬達輪子用的話, 4節就夠了), 所以請將4+2電池盒串聯(4節的黑接到2節的紅)即可
--
快速接頭使用較彈性, 一個可用在掃地機, 另一個可搭配9V電池扣, 使用9V電池供電給離線的Arduino板使用(例機械手臂、LED/LCD)

另外一個配線的補充

星期五, 5月 27, 2016

C++與SQLite--資料列表

void getTableData()
{
    sqlite3_stmt *statement;
    char *query = "select * from student";
    if ( sqlite3_prepare(dbfile, query, -1, &statement, 0 ) == SQLITE_OK )
    {
        int ctotal = sqlite3_column_count(statement);
        int res = 0;
        while ( 1 )
        {
            res = sqlite3_step(statement);
            if ( res == SQLITE_ROW )
            {
                for ( int i = 0; i < ctotal; i++ )
                {
                    string s = (char*)sqlite3_column_text(statement, i);
                    cout << s << " " ;
                }
                cout << endl;
            }
            if ( res == SQLITE_DONE )
            {
                cout << "done " << endl;
                break;
            }
        }
    }
}

C++與SQLite--刪除資料

int deleteRow()
{
    int rollno;
    cout << " 輸入學號 :" ;
    cin >> rollno ;
    if(cin.fail( ))
    {
      cin.clear( );
      cout<<"學號格式錯了";
      return 0;
    }
    std::stringstream strm;
    strm << "delete from student " << " where roll=" << rollno ;
    string s = strm.str();
    char *str = &s[0];
    //cout << str << endl;
    sqlite3_stmt *statement;
    int result;
    char *query = str;
    {
        if(sqlite3_prepare(dbfile,query,-1,&statement,0)==SQLITE_OK)
        {
            int res=sqlite3_step(statement);
            result=res;
            sqlite3_finalize(statement);
        }
        return result;
    }
    return 0;
}

C++與SQLite--更新資料

int updateRow()
{
    int rollno;
    float gpa;
    cout << " 輸入學號 :" ;
    cin >> rollno ;
    if(cin.fail( ))
    {
      cin.clear( );
      cout<<"學號格式錯了";
      return 0;
    }
    cout << " 輸入新的平均成績 : " ;
    cin >> gpa;
    if(cin.fail( ))
    {
      cin.clear( );
      cout<<"成績格式錯了";
      return 0;
    }
    std::stringstream strm;
    strm << "update student set cgpa=" << gpa << " where roll=" << rollno ;
    string s = strm.str();
    char *str = &s[0];
    //cout << str << endl;
    sqlite3_stmt *statement;
    int result;
    //char *query="update student set cgpa=3.66 where roll=11";
    char *query = str;
    {
        if(sqlite3_prepare(dbfile,query,-1,&statement,0)==SQLITE_OK)
        {
            int res=sqlite3_step(statement);
            result=res;
            sqlite3_finalize(statement);
        }
        return result;
    }
    return 0;
}

C++與SQLite--新增資料

student getStudent()
{
    student stud;
    int id;
    string name;
    float gpa;
    cout << " 輸入學號 :" ;
    cin >> id;
    if(cin.fail( ))
    {
      cin.clear( );
      cout<<"學號格式錯了";
    }
    cout << " 輸入姓名 : " ;
    cin >> name;
    cout << " 輸入平均成績 : " ;
    cin >> gpa;
    if(cin.fail( ))
    {
      cin.clear( );
      cout<<"成績格式錯了";
    }
    stud.setid(id);
    stud.setName(name);
    stud.setcgpa(gpa);
    return stud;
}
int addDataRow()
{
    student stud = getStudent();
    //cout << stud.getName() << endl;
    std::stringstream strm;
    strm << "insert into student(roll,name,cgpa) values(" << stud.getid() << ",'" << stud.getName() << "'," << stud.getcgpa() << ")";
    string s = strm.str();
    char *str = &s[0];
    //cout << str << endl;
    sqlite3_stmt *statement;
    int result;
    //char *query="insert into student(roll,name,cgpa)values(4,'uuu',6.6)";
    char *query = str;
    {
        if(sqlite3_prepare(dbfile,query,-1,&statement,0)==SQLITE_OK)
        {
            int res=sqlite3_step(statement);
            result=res;
            sqlite3_finalize(statement);
        }
        return result;
    }
    return 0;
}