Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Gyusik Song
Published © GPL3+

DIY RC Car

Web-based car control with PHPoC Shield for Arduino.

BeginnerShowcase (no instructions)2 hours10,677
DIY RC Car

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
PHPoC Shield for Arduino
PHPoC Shield for Arduino
×1
2A Motor Shield For Arduino
×1
RC Car kit
×1
9V Battery Holder
×1

Story

Read more

Code

RC_Car.ino

C/C++
#include "SPI.h"
#include "Phpoc.h"

PhpocServer server(80);

char slideName;
int slideValue;

int pwm_a = 5;
int pwm_b = 6;
int dir_a = 4;
int dir_b = 7;

void setup() {
  
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  
  analogWrite(pwm_a, 0);  //set both motors to run at (100/255 = 39)% duty cycle (slow)
  analogWrite(pwm_b, 0);  
  
  Serial.begin(9600);
  while(!Serial)
    ;

  Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
  //Phpoc.begin();

  server.beginWebSocket("remote_slide");

  Serial.print("WebSocket server address : ");
  Serial.println(Phpoc.localIP());  
}

void loop() {
  // wait for a new client:
 PhpocClient client = server.available();

  if (client) {
    String slideStr = client.readLine();

    if(slideStr)
    {
      slideName = slideStr.charAt(0);
      slideValue = slideStr.substring(1).toInt();

      Serial.print(slideName);
      Serial.print('/');
      Serial.println(slideValue);
      
     if(slideName == 'A'){
        
          Serial.println(slideValue);
          
          if(slideValue < 0) {
            digitalWrite(dir_a, HIGH);            
            slideValue = slideValue * -1 * 255 / 100;
          } else {
            digitalWrite(dir_a, LOW);    
             slideValue = slideValue * 255 / 100;      
          }
              Serial.println(slideValue);          
              analogWrite(pwm_a, slideValue);          
      }    
      
     if(slideName == 'B'){
        
          Serial.println(slideValue);
          
          if(slideValue < 0) {
            digitalWrite(dir_b, HIGH);            
            slideValue = slideValue * -1 * 255 / 100;
          } else {
            digitalWrite(dir_b, LOW);    
             slideValue = slideValue * 255 / 100;      
          }          
             Serial.println(slideValue);          
             analogWrite(pwm_b, slideValue);            
      }    
    }
  } 

}

Credits

Gyusik Song
5 projects • 9 followers

Comments