chipKIT® Development Platform

Inspired by Arduino™

Convert uno code to work on chipkit lenny

Created Tue, 21 Nov 2017 18:33:13 +0000 by sternfox


sternfox

Tue, 21 Nov 2017 18:33:13 +0000

Hi Guys, I have the following code working on my uno but need the extra pulse rate from the Lenny. Could someone please help me port this code to work on the lenny?

#include <AccelStepper.h>
#include <SoftwareSerial.h> // TX RX software library for bluetooth
int bluetoothTx = 0; // bluetooth TX to pin 0
int bluetoothRx = 1; // bluetooth rx to pin 1
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

AccelStepper stepper(1, 5, 6);  // 1 = Easy Driver interface, Pin 4 connected to STEP pin of Easy Driver, Pin 5 connected to DIR pin of Easy Driver

int change = 0;
int stepper_distance; 

void setup() {
  Serial.begin(9600);
  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(10,OUTPUT);
  digitalWrite(9, HIGH);
  digitalWrite(10, HIGH);  
}//--(end setup )---

void loop() 
{
  if(bluetooth.available()> 0) // receive number from bluetooth
  {
    start:  
    int command = bluetooth.read(); // save the received number to command
    Serial.println(command);  //Printing on the Serial Monitor

    if (command >1 && command <=60)
    {
      int stepper_speed = map(command, 1, 60, 0, 3000);
      stepper.setMaxSpeed(stepper_speed);
    }

    else if (command >60 && command <=120)
    {
      int stepper_acceleration = map(command, 61, 120, 0, 30000);
      stepper.setAcceleration(stepper_acceleration);
    }
    
    else if (command >121 && command <=180)
    {
      stepper_distance = map(command, 121, 180, 0, 200);
    }
    
    else if (command == 1)
    {
      stepper.moveTo(0);
      while (stepper.currentPosition() != 0) // Full speed basck to 0
      {
       stepper.run();
       }
      digitalWrite(9, LOW);
      digitalWrite(10, LOW);
      while(bluetooth.available() == 0)
        {
        digitalWrite(9, HIGH);
        digitalWrite(10, HIGH);
        }
    }
  }
   
  stepper.moveTo(stepper_distance);
  while (stepper.currentPosition() != stepper_distance) 
    {
      stepper.run();
      if(bluetooth.available()> 0) // receive number from bluetooth
    {
      goto start;
    }
    }  

  stepper.moveTo(0);
  while (stepper.currentPosition() != 0) // Full speed basck to 0
   {
    stepper.run();
    
   }
}

majenko

Tue, 21 Nov 2017 22:39:04 +0000

Which part doesn't work?

The main thing I would certainly change is to drop SoftwareSerial and use Serial0 instead - it's the hardware UART on the TX and RX pins.